1 //! lint on enum variants that are prefixed or suffixed by the same characters
5 use syntax::codemap::Span;
6 use syntax::symbol::LocalInternedString;
7 use crate::utils::{span_help_and_lint, span_lint};
8 use crate::utils::{camel_case_from, camel_case_until, in_macro};
10 /// **What it does:** Detects enumeration variants that are prefixed or suffixed
11 /// by the same characters.
13 /// **Why is this bad?** Enumeration variant names should specify their variant,
14 /// not repeat the enumeration name.
16 /// **Known problems:** None.
26 declare_clippy_lint! {
27 pub ENUM_VARIANT_NAMES,
29 "enums where all variants share a prefix/postfix"
32 /// **What it does:** Detects enumeration variants that are prefixed or suffixed
33 /// by the same characters.
35 /// **Why is this bad?** Enumeration variant names should specify their variant,
36 /// not repeat the enumeration name.
38 /// **Known problems:** None.
48 declare_clippy_lint! {
49 pub PUB_ENUM_VARIANT_NAMES,
51 "enums where all variants share a prefix/postfix"
54 /// **What it does:** Detects type names that are prefixed or suffixed by the
55 /// containing module's name.
57 /// **Why is this bad?** It requires the user to type the module name twice.
59 /// **Known problems:** None.
64 /// struct BlackForestCake;
67 declare_clippy_lint! {
70 "type names prefixed/postfixed with their containing module's name"
73 /// **What it does:** Checks for modules that have the same name as their
76 /// **Why is this bad?** A typical beginner mistake is to have `mod foo;` and
77 /// again `mod foo { ..
79 /// The expectation is that items inside the inner `mod foo { .. }` are then
81 /// through `foo::x`, but they are only available through
83 /// If this is done on purpose, it would be better to choose a more
84 /// representative module name.
86 /// **Known problems:** None.
97 declare_clippy_lint! {
100 "modules that have the same name as their parent module"
103 pub struct EnumVariantNames {
104 modules: Vec<(LocalInternedString, String)>,
108 impl EnumVariantNames {
109 pub fn new(threshold: u64) -> Self {
117 impl LintPass for EnumVariantNames {
118 fn get_lints(&self) -> LintArray {
119 lint_array!(ENUM_VARIANT_NAMES, PUB_ENUM_VARIANT_NAMES, STUTTER, MODULE_INCEPTION)
123 fn var2str(var: &Variant) -> LocalInternedString {
124 var.node.ident.as_str()
127 /// Returns the number of chars that match from the start
128 fn partial_match(pre: &str, name: &str) -> usize {
129 let mut name_iter = name.chars();
130 let _ = name_iter.next_back(); // make sure the name is never fully matched
133 .take_while(|&(l, r)| l == r)
137 /// Returns the number of chars that match from the end
138 fn partial_rmatch(post: &str, name: &str) -> usize {
139 let mut name_iter = name.chars();
140 let _ = name_iter.next(); // make sure the name is never fully matched
143 .zip(name_iter.rev())
144 .take_while(|&(l, r)| l == r)
149 #[allow(while_let_on_iterator)]
155 item_name_chars: usize,
159 if (def.variants.len() as u64) < threshold {
162 for var in &def.variants {
163 let name = var2str(var);
164 if partial_match(item_name, &name) == item_name_chars
166 .nth(item_name_chars)
167 .map_or(false, |c| !c.is_lowercase())
169 span_lint(cx, lint, var.span, "Variant name starts with the enum's name");
171 if partial_rmatch(item_name, &name) == item_name_chars {
172 span_lint(cx, lint, var.span, "Variant name ends with the enum's name");
175 let first = var2str(&def.variants[0]);
176 let mut pre = &first[..camel_case_until(&*first)];
177 let mut post = &first[camel_case_from(&*first)..];
178 for var in &def.variants {
179 let name = var2str(var);
181 let pre_match = partial_match(pre, &name);
182 pre = &pre[..pre_match];
183 let pre_camel = camel_case_until(pre);
184 pre = &pre[..pre_camel];
185 while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() {
186 if next.is_lowercase() {
187 let last = pre.len() - last.len_utf8();
188 let last_camel = camel_case_until(&pre[..last]);
189 pre = &pre[..last_camel];
195 let post_match = partial_rmatch(post, &name);
196 let post_end = post.len() - post_match;
197 post = &post[post_end..];
198 let post_camel = camel_case_from(post);
199 post = &post[post_camel..];
201 let (what, value) = match (pre.is_empty(), post.is_empty()) {
202 (true, true) => return,
203 (false, _) => ("pre", pre),
204 (true, false) => ("post", post),
210 &format!("All variants have the same {}fix: `{}`", what, value),
212 "remove the {}fixes and use full paths to \
213 the variants instead of glob imports",
219 fn to_camel_case(item_name: &str) -> String {
220 let mut s = String::new();
222 for c in item_name.chars() {
223 if c.is_uppercase() {
224 // we only turn snake case text into CamelCase
225 return item_name.to_string();
233 s.extend(c.to_uppercase());
241 impl EarlyLintPass for EnumVariantNames {
242 fn check_item_post(&mut self, _cx: &EarlyContext, _item: &Item) {
243 let last = self.modules.pop();
244 assert!(last.is_some());
247 fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
248 let item_name = item.ident.as_str();
249 let item_name_chars = item_name.chars().count();
250 let item_camel = to_camel_case(&item_name);
251 if !in_macro(item.span) {
252 if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
253 // constants don't have surrounding modules
254 if !mod_camel.is_empty() {
255 if *mod_name == item_name {
256 if let ItemKind::Mod(..) = item.node {
261 "module has the same name as its containing module",
265 if item.vis.node == VisibilityKind::Public {
266 let matching = partial_match(mod_camel, &item_camel);
267 let rmatching = partial_rmatch(mod_camel, &item_camel);
268 let nchars = mod_camel.chars().count();
270 let is_word_beginning = |c: char| {
271 c == '_' || c.is_uppercase() || c.is_numeric()
274 if matching == nchars {
275 match item_camel.chars().nth(nchars) {
276 Some(c) if is_word_beginning(c) =>
277 span_lint(cx, STUTTER, item.span, "item name starts with its containing module's name"),
281 if rmatching == nchars {
282 span_lint(cx, STUTTER, item.span, "item name ends with its containing module's name");
288 if let ItemKind::Enum(ref def, _) = item.node {
289 let lint = match item.vis.node {
290 VisibilityKind::Public => PUB_ENUM_VARIANT_NAMES,
291 _ => ENUM_VARIANT_NAMES,
293 check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span, lint);
295 self.modules.push((item_name, item_camel));