1 //! lint on enum variants that are prefixed or suffixed by the same characters
5 use syntax::parse::token::InternedString;
6 use utils::span_help_and_lint;
7 use utils::{camel_case_from, camel_case_until};
9 /// **What it does:** Warns on enum variants that are prefixed or suffixed by the same characters
11 /// **Why is this bad?** Enum variant names should specify their variant, not the enum, too.
13 /// **Known problems:** None
15 /// **Example:** enum Cake { BlackForestCake, HummingbirdCake }
17 pub ENUM_VARIANT_NAMES, Warn,
18 "finds enums where all variants share a prefix/postfix"
21 pub struct EnumVariantNames;
23 impl LintPass for EnumVariantNames {
24 fn get_lints(&self) -> LintArray {
25 lint_array!(ENUM_VARIANT_NAMES)
29 fn var2str(var: &Variant) -> InternedString {
30 var.node.name.name.as_str()
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()
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()
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()
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()
56 impl EarlyLintPass for EnumVariantNames {
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 {
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);
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];
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..];
90 let (what, value) = match (pre.is_empty(), post.is_empty()) {
91 (true, true) => return,
92 (false, _) => ("pre", pre),
93 (true, false) => ("post", post),
95 span_help_and_lint(cx,
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",