]> git.lizzy.rs Git - rust.git/blob - src/enum_variants.rs
Let cargo-clippy exit with a code > 0 if some error occured
[rust.git] / src / enum_variants.rs
1 //! lint on enum variants that are prefixed or suffixed by the same characters
2
3 use rustc::lint::*;
4 use syntax::ast::*;
5 use syntax::parse::token::InternedString;
6 use utils::span_help_and_lint;
7 use utils::{camel_case_from, camel_case_until};
8
9 /// **What it does:** Warns on enum variants that are prefixed or suffixed by the same characters
10 ///
11 /// **Why is this bad?** Enum variant names should specify their variant, not the enum, too.
12 ///
13 /// **Known problems:** None
14 ///
15 /// **Example:** enum Cake { BlackForestCake, HummingbirdCake }
16 declare_lint! {
17     pub ENUM_VARIANT_NAMES, Warn,
18     "finds enums where all variants share a prefix/postfix"
19 }
20
21 pub struct EnumVariantNames;
22
23 impl LintPass for EnumVariantNames {
24     fn get_lints(&self) -> LintArray {
25         lint_array!(ENUM_VARIANT_NAMES)
26     }
27 }
28
29 fn var2str(var: &Variant) -> InternedString {
30     var.node.name.name.as_str()
31 }
32
33 // FIXME: waiting for https://github.com/rust-lang/rust/pull/31700
34 // fn partial_match(pre: &str, name: &str) -> usize {
35 //    // skip(1) to ensure that the prefix never takes the whole variant name
36 //     pre.chars().zip(name.chars().rev().skip(1).rev()).take_while(|&(l, r)| l == r).count()
37 // }
38 //
39 // fn partial_rmatch(post: &str, name: &str) -> usize {
40 //     // skip(1) to ensure that the postfix never takes the whole variant name
41 //     post.chars().rev().zip(name.chars().skip(1).rev()).take_while(|&(l, r)| l == r).count()
42 // }
43
44 fn partial_match(pre: &str, name: &str) -> usize {
45     let mut name_iter = name.chars();
46     let _ = name_iter.next_back(); // make sure the name is never fully matched
47     pre.chars().zip(name_iter).take_while(|&(l, r)| l == r).count()
48 }
49
50 fn partial_rmatch(post: &str, name: &str) -> usize {
51     let mut name_iter = name.chars();
52     let _ = name_iter.next(); // make sure the name is never fully matched
53     post.chars().rev().zip(name_iter.rev()).take_while(|&(l, r)| l == r).count()
54 }
55
56 impl EarlyLintPass for EnumVariantNames {
57     // FIXME: #600
58     #[allow(while_let_on_iterator)]
59     fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
60         if let ItemKind::Enum(ref def, _) = item.node {
61             if def.variants.len() < 2 {
62                 return;
63             }
64             let first = var2str(&def.variants[0]);
65             let mut pre = &first[..camel_case_until(&*first)];
66             let mut post = &first[camel_case_from(&*first)..];
67             for var in &def.variants {
68                 let name = var2str(var);
69
70                 let pre_match = partial_match(pre, &name);
71                 pre = &pre[..pre_match];
72                 let pre_camel = camel_case_until(pre);
73                 pre = &pre[..pre_camel];
74                 while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() {
75                     if next.is_lowercase() {
76                         let last = pre.len() - last.len_utf8();
77                         let last_camel = camel_case_until(&pre[..last]);
78                         pre = &pre[..last_camel];
79                     } else {
80                         break;
81                     }
82                 }
83
84                 let post_match = partial_rmatch(post, &name);
85                 let post_end = post.len() - post_match;
86                 post = &post[post_end..];
87                 let post_camel = camel_case_from(post);
88                 post = &post[post_camel..];
89             }
90             let (what, value) = match (pre.is_empty(), post.is_empty()) {
91                 (true, true) => return,
92                 (false, _) => ("pre", pre),
93                 (true, false) => ("post", post),
94             };
95             span_help_and_lint(cx,
96                                ENUM_VARIANT_NAMES,
97                                item.span,
98                                &format!("All variants have the same {}fix: `{}`", what, value),
99                                &format!("remove the {}fixes and use full paths to \
100                                          the variants instead of glob imports",
101                                         what));
102         }
103     }
104 }