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