add an internal lint that catches misordered paths

This commit is contained in:
Oliver Schneider 2016-07-18 11:19:33 +02:00
parent fc54a91916
commit 02c46f057f
No known key found for this signature in database
GPG Key ID: 56D6EEA0FC67AC46
5 changed files with 61 additions and 6 deletions

View File

@ -169,6 +169,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
// end deprecated lints, do not remove this comment, its used in `update_lints`
reg.register_late_lint_pass(box serde::Serde);
reg.register_early_lint_pass(box utils::internal_lints::Clippy);
reg.register_late_lint_pass(box types::TypePass);
reg.register_late_lint_pass(box booleans::NonminimalBool);
reg.register_late_lint_pass(box misc::TopLevelRefPass);

View File

@ -0,0 +1,53 @@
use rustc::lint::*;
use utils::span_lint;
use syntax::parse::token::InternedString;
use syntax::ast::*;
/// **What it does:** This lint checks for various things we like to keep tidy in clippy
///
/// **Why is this bad?** ???
///
/// **Known problems:** None.
///
/// **Example:** wrong ordering of the util::paths constants
declare_lint! {
pub CLIPPY_LINTS_INTERNAL, Allow,
"Various things that will negatively affect your clippy experience"
}
#[derive(Copy, Clone)]
pub struct Clippy;
impl LintPass for Clippy {
fn get_lints(&self) -> LintArray {
lint_array!(CLIPPY_LINTS_INTERNAL)
}
}
impl EarlyLintPass for Clippy {
fn check_crate(&mut self, cx: &EarlyContext, krate: &Crate) {
if let Some(utils) = krate.module.items.iter().find(|item| item.ident.name.as_str() == "utils") {
if let ItemKind::Mod(ref utils_mod) = utils.node {
if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name.as_str() == "paths") {
if let ItemKind::Mod(ref paths_mod) = paths.node {
let mut last_name: Option<InternedString> = None;
for item in &paths_mod.items {
let name = item.ident.name.as_str();
if let Some(ref last_name) = last_name {
if **last_name > *name {
span_lint(cx,
CLIPPY_LINTS_INTERNAL,
item.span,
"this constant should be before the previous constant due to lexical ordering",
);
}
}
last_name = Some(name);
}
}
}
}
}
}
}

View File

@ -25,6 +25,7 @@ pub mod conf;
mod hir;
pub mod paths;
pub mod sugg;
pub mod internal_lints;
pub use self::hir::{SpanlessEq, SpanlessHash};
pub type MethodArgs = HirVec<P<Expr>>;

View File

@ -17,7 +17,7 @@ fn dogfood() {
let mut s = String::new();
s.push_str(" -L target/debug/");
s.push_str(" -L target/debug/deps");
s.push_str(" -Zextra-plugins=clippy -Ltarget_recur/debug -Dclippy_pedantic -Dclippy");
s.push_str(" -Zextra-plugins=clippy -Ltarget_recur/debug -Dclippy_pedantic -Dclippy -Dclippy_lints_internal");
config.target_rustcflags = Some(s);
if let Ok(name) = var("TESTNAME") {
config.filter = Some(name.to_owned())
@ -29,6 +29,7 @@ fn dogfood() {
}
config.mode = cfg_mode;
config.verbose = true;
let files = ["src/main.rs", "src/lib.rs", "clippy_lints/src/lib.rs"];

View File

@ -150,11 +150,10 @@ def main(print_only=False, check=False):
return
# collect all lints from source files
for root, _, files in os.walk('clippy_lints/src'):
for fn in files:
if fn.endswith('.rs'):
collect(lints, deprecated_lints, restriction_lints,
os.path.join(root, fn))
for fn in os.listdir('clippy_lints/src'):
if fn.endswith('.rs'):
collect(lints, deprecated_lints, restriction_lints,
os.path.join('clippy_lints', 'src', fn))
# determine version
with open('Cargo.toml') as fp: