1 //! lint on enum variants that are prefixed or suffixed by the same characters
5 use syntax::codemap::Span;
6 use syntax::parse::token::InternedString;
7 use utils::{span_help_and_lint, span_lint};
8 use utils::{camel_case_from, camel_case_until, in_macro};
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"
23 pub struct EnumVariantNames {
27 impl LintPass for EnumVariantNames {
28 fn get_lints(&self) -> LintArray {
29 lint_array!(ENUM_VARIANT_NAMES)
33 fn var2str(var: &Variant) -> InternedString {
34 var.node.name.name.as_str()
37 /// Returns the number of chars that match from the start
38 fn partial_match(pre: &str, name: &str) -> usize {
39 let mut name_iter = name.chars();
40 let _ = name_iter.next_back(); // make sure the name is never fully matched
41 pre.chars().zip(name_iter).take_while(|&(l, r)| l == r).count()
44 /// Returns the number of chars that match from the end
45 fn partial_rmatch(post: &str, name: &str) -> usize {
46 let mut name_iter = name.chars();
47 let _ = name_iter.next(); // make sure the name is never fully matched
48 post.chars().rev().zip(name_iter.rev()).take_while(|&(l, r)| l == r).count()
52 #[allow(while_let_on_iterator)]
53 fn check_variant(cx: &EarlyContext, def: &EnumDef, item_name: &str, item_name_chars: usize, span: Span) {
54 for var in &def.variants {
55 let name = var2str(var);
56 if partial_match(item_name, &name) == item_name_chars {
57 span_lint(cx, ENUM_VARIANT_NAMES, var.span, "Variant name starts with the enum's name");
59 if partial_rmatch(item_name, &name) == item_name_chars {
60 span_lint(cx, ENUM_VARIANT_NAMES, var.span, "Variant name ends with the enum's name");
63 if def.variants.len() < 2 {
66 let first = var2str(&def.variants[0]);
67 let mut pre = &first[..camel_case_until(&*first)];
68 let mut post = &first[camel_case_from(&*first)..];
69 for var in &def.variants {
70 let name = var2str(var);
72 let pre_match = partial_match(pre, &name);
73 pre = &pre[..pre_match];
74 let pre_camel = camel_case_until(pre);
75 pre = &pre[..pre_camel];
76 while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() {
77 if next.is_lowercase() {
78 let last = pre.len() - last.len_utf8();
79 let last_camel = camel_case_until(&pre[..last]);
80 pre = &pre[..last_camel];
86 let post_match = partial_rmatch(post, &name);
87 let post_end = post.len() - post_match;
88 post = &post[post_end..];
89 let post_camel = camel_case_from(post);
90 post = &post[post_camel..];
92 let (what, value) = match (pre.is_empty(), post.is_empty()) {
93 (true, true) => return,
94 (false, _) => ("pre", pre),
95 (true, false) => ("post", post),
97 span_help_and_lint(cx,
100 &format!("All variants have the same {}fix: `{}`", what, value),
101 &format!("remove the {}fixes and use full paths to \
102 the variants instead of glob imports",
106 fn to_camel_case(item_name: &str) -> String {
107 let mut s = String::new();
109 for c in item_name.chars() {
110 if c.is_uppercase() {
111 // we only turn snake case text into CamelCase
112 return item_name.to_string();
120 s.extend(c.to_uppercase());
128 impl EarlyLintPass for EnumVariantNames {
129 fn check_item_post(&mut self, _cx: &EarlyContext, _item: &Item) {
130 let last = self.modules.pop();
131 assert!(last.is_some());
134 fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
135 let item_name = item.ident.name.as_str();
136 let item_name_chars = item_name.chars().count();
137 let item_camel = to_camel_case(&item_name);
138 if item.vis == Visibility::Public && !in_macro(cx, item.span) {
139 if let Some(mod_camel) = self.modules.last() {
140 // constants don't have surrounding modules
141 if !mod_camel.is_empty() {
142 let matching = partial_match(mod_camel, &item_camel);
143 let rmatching = partial_rmatch(mod_camel, &item_camel);
144 let nchars = mod_camel.chars().count();
145 if matching == nchars {
146 span_lint(cx, ENUM_VARIANT_NAMES, item.span, &format!("Item name ({}) starts with its containing module's name ({})", item_camel, mod_camel));
148 if rmatching == nchars {
149 span_lint(cx, ENUM_VARIANT_NAMES, item.span, &format!("Item name ({}) ends with its containing module's name ({})", item_camel, mod_camel));
154 if let ItemKind::Enum(ref def, _) = item.node {
155 check_variant(cx, def, &item_name, item_name_chars, item.span);
157 self.modules.push(item_camel);