1 //! lint on enum variants that are prefixed or suffixed by the same characters
4 use rustc::{declare_lint, lint_array};
6 use syntax::codemap::Span;
7 use syntax::symbol::LocalInternedString;
8 use crate::utils::{span_help_and_lint, span_lint};
9 use crate::utils::{camel_case_from, camel_case_until, in_macro};
11 /// **What it does:** Detects enumeration variants that are prefixed or suffixed
12 /// by the same characters.
14 /// **Why is this bad?** Enumeration variant names should specify their variant,
15 /// not repeat the enumeration name.
17 /// **Known problems:** None.
27 declare_clippy_lint! {
28 pub ENUM_VARIANT_NAMES,
30 "enums where all variants share a prefix/postfix"
33 /// **What it does:** Detects enumeration variants that are prefixed or suffixed
34 /// by the same characters.
36 /// **Why is this bad?** Enumeration variant names should specify their variant,
37 /// not repeat the enumeration name.
39 /// **Known problems:** None.
49 declare_clippy_lint! {
50 pub PUB_ENUM_VARIANT_NAMES,
52 "enums where all variants share a prefix/postfix"
55 /// **What it does:** Detects type names that are prefixed or suffixed by the
56 /// containing module's name.
58 /// **Why is this bad?** It requires the user to type the module name twice.
60 /// **Known problems:** None.
65 /// struct BlackForestCake;
68 declare_clippy_lint! {
71 "type names prefixed/postfixed with their containing module's name"
74 /// **What it does:** Checks for modules that have the same name as their
77 /// **Why is this bad?** A typical beginner mistake is to have `mod foo;` and
78 /// again `mod foo { ..
80 /// The expectation is that items inside the inner `mod foo { .. }` are then
82 /// through `foo::x`, but they are only available through
84 /// If this is done on purpose, it would be better to choose a more
85 /// representative module name.
87 /// **Known problems:** None.
98 declare_clippy_lint! {
101 "modules that have the same name as their parent module"
104 pub struct EnumVariantNames {
105 modules: Vec<(LocalInternedString, String)>,
109 impl EnumVariantNames {
110 pub fn new(threshold: u64) -> Self {
118 impl LintPass for EnumVariantNames {
119 fn get_lints(&self) -> LintArray {
120 lint_array!(ENUM_VARIANT_NAMES, PUB_ENUM_VARIANT_NAMES, STUTTER, MODULE_INCEPTION)
124 fn var2str(var: &Variant) -> LocalInternedString {
125 var.node.ident.as_str()
128 /// Returns the number of chars that match from the start
129 fn partial_match(pre: &str, name: &str) -> usize {
130 let mut name_iter = name.chars();
131 let _ = name_iter.next_back(); // make sure the name is never fully matched
134 .take_while(|&(l, r)| l == r)
138 /// Returns the number of chars that match from the end
139 fn partial_rmatch(post: &str, name: &str) -> usize {
140 let mut name_iter = name.chars();
141 let _ = name_iter.next(); // make sure the name is never fully matched
144 .zip(name_iter.rev())
145 .take_while(|&(l, r)| l == r)
150 #[allow(while_let_on_iterator)]
156 item_name_chars: usize,
160 if (def.variants.len() as u64) < threshold {
163 for var in &def.variants {
164 let name = var2str(var);
165 if partial_match(item_name, &name) == item_name_chars
167 .nth(item_name_chars)
168 .map_or(false, |c| !c.is_lowercase())
170 span_lint(cx, lint, var.span, "Variant name starts with the enum's name");
172 if partial_rmatch(item_name, &name) == item_name_chars {
173 span_lint(cx, lint, var.span, "Variant name ends with the enum's name");
176 let first = var2str(&def.variants[0]);
177 let mut pre = &first[..camel_case_until(&*first)];
178 let mut post = &first[camel_case_from(&*first)..];
179 for var in &def.variants {
180 let name = var2str(var);
182 let pre_match = partial_match(pre, &name);
183 pre = &pre[..pre_match];
184 let pre_camel = camel_case_until(pre);
185 pre = &pre[..pre_camel];
186 while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() {
187 if next.is_lowercase() {
188 let last = pre.len() - last.len_utf8();
189 let last_camel = camel_case_until(&pre[..last]);
190 pre = &pre[..last_camel];
196 let post_match = partial_rmatch(post, &name);
197 let post_end = post.len() - post_match;
198 post = &post[post_end..];
199 let post_camel = camel_case_from(post);
200 post = &post[post_camel..];
202 let (what, value) = match (pre.is_empty(), post.is_empty()) {
203 (true, true) => return,
204 (false, _) => ("pre", pre),
205 (true, false) => ("post", post),
211 &format!("All variants have the same {}fix: `{}`", what, value),
213 "remove the {}fixes and use full paths to \
214 the variants instead of glob imports",
220 fn to_camel_case(item_name: &str) -> String {
221 let mut s = String::new();
223 for c in item_name.chars() {
224 if c.is_uppercase() {
225 // we only turn snake case text into CamelCase
226 return item_name.to_string();
234 s.extend(c.to_uppercase());
242 impl EarlyLintPass for EnumVariantNames {
243 fn check_item_post(&mut self, _cx: &EarlyContext, _item: &Item) {
244 let last = self.modules.pop();
245 assert!(last.is_some());
248 fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
249 let item_name = item.ident.as_str();
250 let item_name_chars = item_name.chars().count();
251 let item_camel = to_camel_case(&item_name);
252 if !in_macro(item.span) {
253 if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
254 // constants don't have surrounding modules
255 if !mod_camel.is_empty() {
256 if *mod_name == item_name {
257 if let ItemKind::Mod(..) = item.node {
262 "module has the same name as its containing module",
266 if item.vis.node.is_pub() {
267 let matching = partial_match(mod_camel, &item_camel);
268 let rmatching = partial_rmatch(mod_camel, &item_camel);
269 let nchars = mod_camel.chars().count();
271 let is_word_beginning = |c: char| {
272 c == '_' || c.is_uppercase() || c.is_numeric()
275 if matching == nchars {
276 match item_camel.chars().nth(nchars) {
277 Some(c) if is_word_beginning(c) =>
278 span_lint(cx, STUTTER, item.span, "item name starts with its containing module's name"),
282 if rmatching == nchars {
283 span_lint(cx, STUTTER, item.span, "item name ends with its containing module's name");
289 if let ItemKind::Enum(ref def, _) = item.node {
290 let lint = match item.vis.node {
291 VisibilityKind::Public => PUB_ENUM_VARIANT_NAMES,
292 _ => ENUM_VARIANT_NAMES,
294 check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span, lint);
296 self.modules.push((item_name, item_camel));