Skip to content

Commit 02c46f0

Browse files
committed
add an internal lint that catches misordered paths
1 parent fc54a91 commit 02c46f0

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
169169
// end deprecated lints, do not remove this comment, it’s used in `update_lints`
170170

171171
reg.register_late_lint_pass(box serde::Serde);
172+
reg.register_early_lint_pass(box utils::internal_lints::Clippy);
172173
reg.register_late_lint_pass(box types::TypePass);
173174
reg.register_late_lint_pass(box booleans::NonminimalBool);
174175
reg.register_late_lint_pass(box misc::TopLevelRefPass);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use rustc::lint::*;
2+
use utils::span_lint;
3+
use syntax::parse::token::InternedString;
4+
use syntax::ast::*;
5+
6+
/// **What it does:** This lint checks for various things we like to keep tidy in clippy
7+
///
8+
/// **Why is this bad?** ???
9+
///
10+
/// **Known problems:** None.
11+
///
12+
/// **Example:** wrong ordering of the util::paths constants
13+
declare_lint! {
14+
pub CLIPPY_LINTS_INTERNAL, Allow,
15+
"Various things that will negatively affect your clippy experience"
16+
}
17+
18+
19+
#[derive(Copy, Clone)]
20+
pub struct Clippy;
21+
22+
impl LintPass for Clippy {
23+
fn get_lints(&self) -> LintArray {
24+
lint_array!(CLIPPY_LINTS_INTERNAL)
25+
}
26+
}
27+
28+
impl EarlyLintPass for Clippy {
29+
fn check_crate(&mut self, cx: &EarlyContext, krate: &Crate) {
30+
if let Some(utils) = krate.module.items.iter().find(|item| item.ident.name.as_str() == "utils") {
31+
if let ItemKind::Mod(ref utils_mod) = utils.node {
32+
if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name.as_str() == "paths") {
33+
if let ItemKind::Mod(ref paths_mod) = paths.node {
34+
let mut last_name: Option<InternedString> = None;
35+
for item in &paths_mod.items {
36+
let name = item.ident.name.as_str();
37+
if let Some(ref last_name) = last_name {
38+
if **last_name > *name {
39+
span_lint(cx,
40+
CLIPPY_LINTS_INTERNAL,
41+
item.span,
42+
"this constant should be before the previous constant due to lexical ordering",
43+
);
44+
}
45+
}
46+
last_name = Some(name);
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod conf;
2525
mod hir;
2626
pub mod paths;
2727
pub mod sugg;
28+
pub mod internal_lints;
2829
pub use self::hir::{SpanlessEq, SpanlessHash};
2930

3031
pub type MethodArgs = HirVec<P<Expr>>;

tests/dogfood.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn dogfood() {
1717
let mut s = String::new();
1818
s.push_str(" -L target/debug/");
1919
s.push_str(" -L target/debug/deps");
20-
s.push_str(" -Zextra-plugins=clippy -Ltarget_recur/debug -Dclippy_pedantic -Dclippy");
20+
s.push_str(" -Zextra-plugins=clippy -Ltarget_recur/debug -Dclippy_pedantic -Dclippy -Dclippy_lints_internal");
2121
config.target_rustcflags = Some(s);
2222
if let Ok(name) = var("TESTNAME") {
2323
config.filter = Some(name.to_owned())
@@ -29,6 +29,7 @@ fn dogfood() {
2929
}
3030

3131
config.mode = cfg_mode;
32+
config.verbose = true;
3233

3334
let files = ["src/main.rs", "src/lib.rs", "clippy_lints/src/lib.rs"];
3435

util/update_lints.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ def main(print_only=False, check=False):
150150
return
151151

152152
# collect all lints from source files
153-
for root, _, files in os.walk('clippy_lints/src'):
154-
for fn in files:
155-
if fn.endswith('.rs'):
156-
collect(lints, deprecated_lints, restriction_lints,
157-
os.path.join(root, fn))
153+
for fn in os.listdir('clippy_lints/src'):
154+
if fn.endswith('.rs'):
155+
collect(lints, deprecated_lints, restriction_lints,
156+
os.path.join('clippy_lints', 'src', fn))
158157

159158
# determine version
160159
with open('Cargo.toml') as fp:

0 commit comments

Comments
 (0)