about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Schneider <oli-obk@users.noreply.github.com>2016-09-13 11:35:54 +0200
committerGitHub <noreply@github.com>2016-09-13 11:35:54 +0200
commit05e734b5552ed1a04ab799f06a8dc7db2db73fe7 (patch)
treebd92a6584683835dd31b6a4697d1ffb669775e8d
parentcb49e4e21014c18cde6d90aaa21c936150b90a54 (diff)
parent40ce3a8f1c62ec403ac9790c0d45f964efe2c504 (diff)
downloadrust-05e734b5552ed1a04ab799f06a8dc7db2db73fe7.tar.gz
rust-05e734b5552ed1a04ab799f06a8dc7db2db73fe7.zip
Merge pull request #1223 from oli-obk/use_your_token_to_figure_out_if_you_are_in_reality
Let the submodule `#[allow]` in `module_inception`
-rw-r--r--clippy_lints/src/enum_variants.rs55
-rw-r--r--clippy_lints/src/lib.rs4
-rw-r--r--clippy_lints/src/module_inception.rs50
-rw-r--r--tests/compile-fail/module_inception.rs7
-rw-r--r--tests/compile-fail/stutter.rs14
5 files changed, 65 insertions, 65 deletions
diff --git a/clippy_lints/src/enum_variants.rs b/clippy_lints/src/enum_variants.rs
index 17c3de48048..69ec9911d99 100644
--- a/clippy_lints/src/enum_variants.rs
+++ b/clippy_lints/src/enum_variants.rs
@@ -47,8 +47,32 @@ declare_lint! {
     "type names prefixed/postfixed with their containing module's name"
 }
 
+/// **What it does:** Checks for modules that have the same name as their parent module
+///
+/// **Why is this bad?** A typical beginner mistake is to have `mod foo;` and again `mod foo { .. }` in `foo.rs`.
+///                      The expectation is that items inside the inner `mod foo { .. }` are then available
+///                      through `foo::x`, but they are only available through `foo::foo::x`.
+///                      If this is done on purpose, it would be better to choose a more representative module name.
+///
+/// **Known problems:** None.
+///
+/// **Example:**
+/// ```rust
+/// // lib.rs
+/// mod foo;
+/// // foo.rs
+/// mod foo {
+///     ...
+/// }
+/// ```
+declare_lint! {
+    pub MODULE_INCEPTION,
+    Warn,
+    "modules that have the same name as their parent module"
+}
+
 pub struct EnumVariantNames {
-    modules: Vec<String>,
+    modules: Vec<(InternedString, String)>,
     threshold: u64,
 }
 
@@ -60,7 +84,7 @@ impl EnumVariantNames {
 
 impl LintPass for EnumVariantNames {
     fn get_lints(&self) -> LintArray {
-        lint_array!(ENUM_VARIANT_NAMES, STUTTER)
+        lint_array!(ENUM_VARIANT_NAMES, STUTTER, MODULE_INCEPTION)
     }
 }
 
@@ -170,18 +194,25 @@ impl EarlyLintPass for EnumVariantNames {
         let item_name = item.ident.name.as_str();
         let item_name_chars = item_name.chars().count();
         let item_camel = to_camel_case(&item_name);
-        if item.vis == Visibility::Public && !in_macro(cx, item.span) {
-            if let Some(mod_camel) = self.modules.last() {
+        if !in_macro(cx, item.span) {
+            if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
                 // constants don't have surrounding modules
                 if !mod_camel.is_empty() {
-                    let matching = partial_match(mod_camel, &item_camel);
-                    let rmatching = partial_rmatch(mod_camel, &item_camel);
-                    let nchars = mod_camel.chars().count();
-                    if matching == nchars {
-                        span_lint(cx, STUTTER, item.span, &format!("Item name ({}) starts with its containing module's name ({})", item_camel, mod_camel));
+                    if mod_name == &item_name {
+                        if let ItemKind::Mod(..) = item.node {
+                            span_lint(cx, MODULE_INCEPTION, item.span, "module has the same name as its containing module");
+                        }
                     }
-                    if rmatching == nchars {
-                        span_lint(cx, STUTTER, item.span, &format!("Item name ({}) ends with its containing module's name ({})", item_camel, mod_camel));
+                    if item.vis == Visibility::Public {
+                        let matching = partial_match(mod_camel, &item_camel);
+                        let rmatching = partial_rmatch(mod_camel, &item_camel);
+                        let nchars = mod_camel.chars().count();
+                        if matching == nchars {
+                            span_lint(cx, STUTTER, item.span, "item name starts with its containing module's name");
+                        }
+                        if rmatching == nchars {
+                            span_lint(cx, STUTTER, item.span, "item name ends with its containing module's name");
+                        }
                     }
                 }
             }
@@ -189,6 +220,6 @@ impl EarlyLintPass for EnumVariantNames {
         if let ItemKind::Enum(ref def, _) = item.node {
             check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span);
         }
-        self.modules.push(item_camel);
+        self.modules.push((item_name, item_camel));
     }
 }
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index 36d466ed9c9..46a2a737d7c 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -96,7 +96,6 @@ pub mod minmax;
 pub mod misc;
 pub mod misc_early;
 pub mod missing_doc;
-pub mod module_inception;
 pub mod mut_mut;
 pub mod mut_reference;
 pub mod mutex_atomic;
@@ -175,7 +174,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
     reg.register_late_lint_pass(box utils::internal_lints::LintWithoutLintPass::default());
     reg.register_late_lint_pass(box types::TypePass);
     reg.register_late_lint_pass(box booleans::NonminimalBool);
-    reg.register_early_lint_pass(box module_inception::Pass);
     reg.register_late_lint_pass(box eq_op::EqOp);
     reg.register_early_lint_pass(box enum_variants::EnumVariantNames::new(conf.enum_variant_name_threshold));
     reg.register_late_lint_pass(box enum_glob_use::EnumGlobUse);
@@ -329,6 +327,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
         entry::MAP_ENTRY,
         enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT,
         enum_variants::ENUM_VARIANT_NAMES,
+        enum_variants::MODULE_INCEPTION,
         eq_op::EQ_OP,
         escape::BOXED_LOCAL,
         eta_reduction::REDUNDANT_CLOSURE,
@@ -391,7 +390,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
         misc_early::REDUNDANT_CLOSURE_CALL,
         misc_early::UNNEEDED_FIELD_PATTERN,
         misc_early::ZERO_PREFIXED_LITERAL,
-        module_inception::MODULE_INCEPTION,
         mut_reference::UNNECESSARY_MUT_PASSED,
         mutex_atomic::MUTEX_ATOMIC,
         needless_bool::BOOL_COMPARISON,
diff --git a/clippy_lints/src/module_inception.rs b/clippy_lints/src/module_inception.rs
deleted file mode 100644
index 10c8154d100..00000000000
--- a/clippy_lints/src/module_inception.rs
+++ /dev/null
@@ -1,50 +0,0 @@
-use rustc::lint::*;
-use syntax::ast::*;
-use utils::span_lint;
-
-/// **What it does:** Checks for modules that have the same name as their parent module
-///
-/// **Why is this bad?** A typical beginner mistake is to have `mod foo;` and again `mod foo { .. }` in `foo.rs`.
-///                      The expectation is that items inside the inner `mod foo { .. }` are then available
-///                      through `foo::x`, but they are only available through `foo::foo::x`.
-///                      If this is done on purpose, it would be better to choose a more representative module name.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// // lib.rs
-/// mod foo;
-/// // foo.rs
-/// mod foo {
-///     ...
-/// }
-/// ```
-declare_lint! {
-    pub MODULE_INCEPTION,
-    Warn,
-    "modules that have the same name as their parent module"
-}
-
-pub struct Pass;
-
-impl LintPass for Pass {
-    fn get_lints(&self) -> LintArray {
-        lint_array![MODULE_INCEPTION]
-    }
-}
-
-impl EarlyLintPass for Pass {
-    fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
-        if let ItemKind::Mod(ref module) = item.node {
-            for sub_item in &module.items {
-                if let ItemKind::Mod(_) = sub_item.node {
-                    if item.ident == sub_item.ident {
-                        span_lint(cx, MODULE_INCEPTION, sub_item.span,
-                                  "module has the same name as its containing module");
-                    }
-                }
-            }
-        }
-    }
-}
diff --git a/tests/compile-fail/module_inception.rs b/tests/compile-fail/module_inception.rs
index 88497c9a6a7..861ed504c86 100644
--- a/tests/compile-fail/module_inception.rs
+++ b/tests/compile-fail/module_inception.rs
@@ -14,4 +14,11 @@ mod foo {
     }
 }
 
+// No warning. See <https://github.com/Manishearth/rust-clippy/issues/1220>.
+mod bar {
+    #[allow(module_inception)]
+    mod bar {
+    }
+}
+
 fn main() {}
diff --git a/tests/compile-fail/stutter.rs b/tests/compile-fail/stutter.rs
new file mode 100644
index 00000000000..0c99859c10d
--- /dev/null
+++ b/tests/compile-fail/stutter.rs
@@ -0,0 +1,14 @@
+#![feature(plugin)]
+#![plugin(clippy)]
+#![deny(stutter)]
+#![allow(dead_code)]
+
+mod foo {
+    pub fn foo() {}
+    pub fn foo_bar() {} //~ ERROR: item name starts with its containing module's name
+    pub fn bar_foo() {} //~ ERROR: item name ends with its containing module's name
+    pub struct FooCake {} //~ ERROR: item name starts with its containing module's name
+    pub enum CakeFoo {} //~ ERROR: item name ends with its containing module's name
+}
+
+fn main() {}