]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #1206 from Manishearth/pub_enum_variant_names
authorOliver Schneider <oli-obk@users.noreply.github.com>
Wed, 21 Dec 2016 12:37:27 +0000 (13:37 +0100)
committerGitHub <noreply@github.com>
Wed, 21 Dec 2016 12:37:27 +0000 (13:37 +0100)
split pub_enum_variant_names to new lint

CHANGELOG.md
README.md
clippy_lints/src/enum_variants.rs
clippy_lints/src/lib.rs
tests/compile-fail/enum_variants.rs

index 54f10f30fd2cdd7719a4adcb3e79dc87a2bb81a7..51381a6eb0248191dbe47da2adca0301705c4b8a 100644 (file)
@@ -357,6 +357,7 @@ All notable changes to this project will be documented in this file.
 [`print_stdout`]: https://github.com/Manishearth/rust-clippy/wiki#print_stdout
 [`print_with_newline`]: https://github.com/Manishearth/rust-clippy/wiki#print_with_newline
 [`ptr_arg`]: https://github.com/Manishearth/rust-clippy/wiki#ptr_arg
+[`pub_enum_variant_names`]: https://github.com/Manishearth/rust-clippy/wiki#pub_enum_variant_names
 [`range_step_by_zero`]: https://github.com/Manishearth/rust-clippy/wiki#range_step_by_zero
 [`range_zip_with_len`]: https://github.com/Manishearth/rust-clippy/wiki#range_zip_with_len
 [`redundant_closure`]: https://github.com/Manishearth/rust-clippy/wiki#redundant_closure
index 24b03e2027555f711fec0f79493296680fa10334..15751a0d3d0ba8de31ad0dbcbdb4666f3f52a30c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -179,7 +179,7 @@ transparently:
 
 ## Lints
 
-There are 179 lints included in this crate:
+There are 180 lints included in this crate:
 
 name                                                                                                                   | default | triggers on
 -----------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@@ -305,6 +305,7 @@ name
 [print_stdout](https://github.com/Manishearth/rust-clippy/wiki#print_stdout)                                           | allow   | printing on stdout
 [print_with_newline](https://github.com/Manishearth/rust-clippy/wiki#print_with_newline)                               | warn    | using `print!()` with a format string that ends in a newline
 [ptr_arg](https://github.com/Manishearth/rust-clippy/wiki#ptr_arg)                                                     | warn    | fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` instead, respectively
+[pub_enum_variant_names](https://github.com/Manishearth/rust-clippy/wiki#pub_enum_variant_names)                       | allow   | enums where all variants share a prefix/postfix
 [range_step_by_zero](https://github.com/Manishearth/rust-clippy/wiki#range_step_by_zero)                               | warn    | using `Range::step_by(0)`, which produces an infinite iterator
 [range_zip_with_len](https://github.com/Manishearth/rust-clippy/wiki#range_zip_with_len)                               | warn    | zipping iterator with a range when `enumerate()` would do
 [redundant_closure](https://github.com/Manishearth/rust-clippy/wiki#redundant_closure)                                 | warn    | redundant closures, i.e. `|a| foo(a)` (which can be written as just `foo`)
index 5ae3fcbb2a1e19d8ff51f8522ca79665e94046f3..e7b73809dff267fb15be73e236736d4a1b771385 100644 (file)
     "enums where all variants share a prefix/postfix"
 }
 
+/// **What it does:** Detects enumeration variants that are prefixed or suffixed
+/// by the same characters.
+///
+/// **Why is this bad?** Enumeration variant names should specify their variant,
+/// not repeat the enumeration name.
+///
+/// **Known problems:** None.
+///
+/// **Example:**
+/// ```rust
+/// enum Cake {
+///     BlackForestCake,
+///     HummingbirdCake,
+/// }
+/// ```
+declare_lint! {
+    pub PUB_ENUM_VARIANT_NAMES,
+    Allow,
+    "enums where all variants share a prefix/postfix"
+}
+
 /// **What it does:** Detects type names that are prefixed or suffixed by the
 /// containing module's name.
 ///
@@ -90,7 +111,7 @@ pub fn new(threshold: u64) -> EnumVariantNames {
 
 impl LintPass for EnumVariantNames {
     fn get_lints(&self) -> LintArray {
-        lint_array!(ENUM_VARIANT_NAMES, STUTTER, MODULE_INCEPTION)
+        lint_array!(ENUM_VARIANT_NAMES, PUB_ENUM_VARIANT_NAMES, STUTTER, MODULE_INCEPTION)
     }
 }
 
@@ -120,7 +141,8 @@ fn check_variant(
     def: &EnumDef,
     item_name: &str,
     item_name_chars: usize,
-    span: Span
+    span: Span,
+    lint: &'static Lint
 ) {
     if (def.variants.len() as u64) < threshold {
         return;
@@ -128,10 +150,10 @@ fn check_variant(
     for var in &def.variants {
         let name = var2str(var);
         if partial_match(item_name, &name) == item_name_chars {
-            span_lint(cx, ENUM_VARIANT_NAMES, var.span, "Variant name starts with the enum's name");
+            span_lint(cx, lint, var.span, "Variant name starts with the enum's name");
         }
         if partial_rmatch(item_name, &name) == item_name_chars {
-            span_lint(cx, ENUM_VARIANT_NAMES, var.span, "Variant name ends with the enum's name");
+            span_lint(cx, lint, var.span, "Variant name ends with the enum's name");
         }
     }
     let first = var2str(&def.variants[0]);
@@ -166,7 +188,7 @@ fn check_variant(
         (true, false) => ("post", post),
     };
     span_help_and_lint(cx,
-                       ENUM_VARIANT_NAMES,
+                       lint,
                        span,
                        &format!("All variants have the same {}fix: `{}`", what, value),
                        &format!("remove the {}fixes and use full paths to \
@@ -233,7 +255,11 @@ fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
             }
         }
         if let ItemKind::Enum(ref def, _) = item.node {
-            check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span);
+            let lint = match item.vis {
+                Visibility::Public => PUB_ENUM_VARIANT_NAMES,
+                _ => ENUM_VARIANT_NAMES,
+            };
+            check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span, lint);
         }
         self.modules.push((item_name, item_camel));
     }
index 3641b476c5fb6d76b0eef9a59ea6acee6bd19a5c..06aab286b2fe6effdf60b5724169c8ca0449c33d 100644 (file)
@@ -294,6 +294,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
     reg.register_lint_group("clippy_pedantic", vec![
         booleans::NONMINIMAL_BOOL,
         enum_glob_use::ENUM_GLOB_USE,
+        enum_variants::PUB_ENUM_VARIANT_NAMES,
         enum_variants::STUTTER,
         if_not_else::IF_NOT_ELSE,
         items_after_statements::ITEMS_AFTER_STATEMENTS,
index f0fda37f7f8f6be43c5cd3cb7721772b02175cf8..585535f9d999b83f157656cbe5f98e578d69a8f2 100644 (file)
@@ -1,6 +1,6 @@
 #![feature(plugin, non_ascii_idents)]
 #![plugin(clippy)]
-#![deny(clippy)]
+#![deny(clippy, pub_enum_variant_names)]
 
 enum FakeCallType {
     CALL, CREATE
@@ -87,4 +87,19 @@ enum NonCaps { //~ ERROR: All variants have the same prefix: `Prefix`
     PrefixCake,
 }
 
+pub enum PubSeall { //~ ERROR: All variants have the same prefix:
+    WithOutCake,
+    WithOutTea,
+    WithOut,
+}
+
+#[allow(pub_enum_variant_names)]
+mod allowed {
+    pub enum PubAllowed {
+        SomeThis,
+        SomeThat,
+        SomeOtherWhat,
+    }
+}
+
 fn main() {}