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