1 //! lint on enum variants that are prefixed or suffixed by the same characters
3 use crate::utils::{camel_case, is_present_in_source};
4 use crate::utils::{span_help_and_lint, span_lint};
5 use rustc::lint::{EarlyContext, EarlyLintPass, Lint, LintArray, LintPass};
6 use rustc::{declare_tool_lint, impl_lint_pass};
8 use syntax::source_map::Span;
9 use syntax::symbol::Symbol;
11 declare_clippy_lint! {
12 /// **What it does:** Detects enumeration variants that are prefixed or suffixed
13 /// by the same characters.
15 /// **Why is this bad?** Enumeration variant names should specify their variant,
16 /// not repeat the enumeration name.
18 /// **Known problems:** None.
28 pub ENUM_VARIANT_NAMES,
30 "enums where all variants share a prefix/postfix"
33 declare_clippy_lint! {
34 /// **What it does:** Detects enumeration variants that are prefixed or suffixed
35 /// by the same characters.
37 /// **Why is this bad?** Enumeration variant names should specify their variant,
38 /// not repeat the enumeration name.
40 /// **Known problems:** None.
50 pub PUB_ENUM_VARIANT_NAMES,
52 "enums where all variants share a prefix/postfix"
55 declare_clippy_lint! {
56 /// **What it does:** Detects type names that are prefixed or suffixed by the
57 /// containing module's name.
59 /// **Why is this bad?** It requires the user to type the module name twice.
61 /// **Known problems:** None.
66 /// struct BlackForestCake;
69 pub MODULE_NAME_REPETITIONS,
71 "type names prefixed/postfixed with their containing module's name"
74 declare_clippy_lint! {
75 /// **What it does:** Checks for modules that have the same name as their
78 /// **Why is this bad?** A typical beginner mistake is to have `mod foo;` and
79 /// again `mod foo { ..
81 /// The expectation is that items inside the inner `mod foo { .. }` are then
83 /// through `foo::x`, but they are only available through
85 /// If this is done on purpose, it would be better to choose a more
86 /// representative module name.
88 /// **Known problems:** None.
101 "modules that have the same name as their parent module"
104 pub struct EnumVariantNames {
105 modules: Vec<(Symbol, String)>,
109 impl EnumVariantNames {
110 pub fn new(threshold: u64) -> Self {
118 impl_lint_pass!(EnumVariantNames => [
120 PUB_ENUM_VARIANT_NAMES,
121 MODULE_NAME_REPETITIONS,
125 /// Returns the number of chars that match from the start
126 fn partial_match(pre: &str, name: &str) -> usize {
127 let mut name_iter = name.chars();
128 let _ = name_iter.next_back(); // make sure the name is never fully matched
129 pre.chars().zip(name_iter).take_while(|&(l, r)| l == r).count()
132 /// Returns the number of chars that match from the end
133 fn partial_rmatch(post: &str, name: &str) -> usize {
134 let mut name_iter = name.chars();
135 let _ = name_iter.next(); // make sure the name is never fully matched
138 .zip(name_iter.rev())
139 .take_while(|&(l, r)| l == r)
144 cx: &EarlyContext<'_>,
148 item_name_chars: usize,
152 if (def.variants.len() as u64) < threshold {
155 for var in &def.variants {
156 let name = var.ident.name.as_str();
157 if partial_match(item_name, &name) == item_name_chars
158 && name.chars().nth(item_name_chars).map_or(false, |c| !c.is_lowercase())
159 && name.chars().nth(item_name_chars + 1).map_or(false, |c| !c.is_numeric())
161 span_lint(cx, lint, var.span, "Variant name starts with the enum's name");
163 if partial_rmatch(item_name, &name) == item_name_chars {
164 span_lint(cx, lint, var.span, "Variant name ends with the enum's name");
167 let first = &def.variants[0].ident.name.as_str();
168 let mut pre = &first[..camel_case::until(&*first)];
169 let mut post = &first[camel_case::from(&*first)..];
170 for var in &def.variants {
171 let name = var.ident.name.as_str();
173 let pre_match = partial_match(pre, &name);
174 pre = &pre[..pre_match];
175 let pre_camel = camel_case::until(pre);
176 pre = &pre[..pre_camel];
177 while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() {
178 if next.is_numeric() {
181 if next.is_lowercase() {
182 let last = pre.len() - last.len_utf8();
183 let last_camel = camel_case::until(&pre[..last]);
184 pre = &pre[..last_camel];
190 let post_match = partial_rmatch(post, &name);
191 let post_end = post.len() - post_match;
192 post = &post[post_end..];
193 let post_camel = camel_case::from(post);
194 post = &post[post_camel..];
196 let (what, value) = match (pre.is_empty(), post.is_empty()) {
197 (true, true) => return,
198 (false, _) => ("pre", pre),
199 (true, false) => ("post", post),
205 &format!("All variants have the same {}fix: `{}`", what, value),
207 "remove the {}fixes and use full paths to \
208 the variants instead of glob imports",
214 fn to_camel_case(item_name: &str) -> String {
215 let mut s = String::new();
217 for c in item_name.chars() {
218 if c.is_uppercase() {
219 // we only turn snake case text into CamelCase
220 return item_name.to_string();
228 s.extend(c.to_uppercase());
236 impl EarlyLintPass for EnumVariantNames {
237 fn check_item_post(&mut self, _cx: &EarlyContext<'_>, _item: &Item) {
238 let last = self.modules.pop();
239 assert!(last.is_some());
242 #[allow(clippy::similar_names)]
243 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
244 let item_name = item.ident.name.as_str();
245 let item_name_chars = item_name.chars().count();
246 let item_camel = to_camel_case(&item_name);
247 if !item.span.from_expansion() && is_present_in_source(cx, item.span) {
248 if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
249 // constants don't have surrounding modules
250 if !mod_camel.is_empty() {
251 if mod_name == &item.ident.name {
252 if let ItemKind::Mod(..) = item.node {
257 "module has the same name as its containing module",
261 if item.vis.node.is_pub() {
262 let matching = partial_match(mod_camel, &item_camel);
263 let rmatching = partial_rmatch(mod_camel, &item_camel);
264 let nchars = mod_camel.chars().count();
266 let is_word_beginning = |c: char| c == '_' || c.is_uppercase() || c.is_numeric();
268 if matching == nchars {
269 match item_camel.chars().nth(nchars) {
270 Some(c) if is_word_beginning(c) => span_lint(
272 MODULE_NAME_REPETITIONS,
274 "item name starts with its containing module's name",
279 if rmatching == nchars {
282 MODULE_NAME_REPETITIONS,
284 "item name ends with its containing module's name",
291 if let ItemKind::Enum(ref def, _) = item.node {
292 let lint = match item.vis.node {
293 VisibilityKind::Public => PUB_ENUM_VARIANT_NAMES,
294 _ => ENUM_VARIANT_NAMES,
296 check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span, lint);
298 self.modules.push((item.ident.name, item_camel));