1 //! lint on enum variants that are prefixed or suffixed by the same characters
3 use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
4 use clippy_utils::source::is_present_in_source;
5 use clippy_utils::str_utils::{self, count_match_end, count_match_start};
6 use rustc_hir::{EnumDef, Item, ItemKind};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 use rustc_span::source_map::Span;
10 use rustc_span::symbol::Symbol;
12 declare_clippy_lint! {
14 /// Detects enumeration variants that are prefixed or suffixed
15 /// by the same characters.
17 /// ### Why is this bad?
18 /// Enumeration variant names should specify their variant,
19 /// not repeat the enumeration name.
29 /// Could be written as:
37 pub ENUM_VARIANT_NAMES,
39 "enums where all variants share a prefix/postfix"
42 declare_clippy_lint! {
44 /// Detects type names that are prefixed or suffixed by the
45 /// containing module's name.
47 /// ### Why is this bad?
48 /// It requires the user to type the module name twice.
53 /// struct BlackForestCake;
56 /// Could be written as:
59 /// struct BlackForest;
62 pub MODULE_NAME_REPETITIONS,
64 "type names prefixed/postfixed with their containing module's name"
67 declare_clippy_lint! {
69 /// Checks for modules that have the same name as their
72 /// ### Why is this bad?
73 /// A typical beginner mistake is to have `mod foo;` and
74 /// again `mod foo { ..
76 /// The expectation is that items inside the inner `mod foo { .. }` are then
78 /// through `foo::x`, but they are only available through
80 /// If this is done on purpose, it would be better to choose a more
81 /// representative module name.
94 "modules that have the same name as their parent module"
97 pub struct EnumVariantNames {
98 modules: Vec<(Symbol, String)>,
100 avoid_breaking_exported_api: bool,
103 impl EnumVariantNames {
105 pub fn new(threshold: u64, avoid_breaking_exported_api: bool) -> Self {
109 avoid_breaking_exported_api,
114 impl_lint_pass!(EnumVariantNames => [
116 MODULE_NAME_REPETITIONS,
121 cx: &LateContext<'_>,
125 item_name_chars: usize,
128 if (def.variants.len() as u64) < threshold {
131 for var in def.variants {
132 let name = var.ident.name.as_str();
133 if count_match_start(item_name, &name).char_count == item_name_chars
134 && name.chars().nth(item_name_chars).map_or(false, |c| !c.is_lowercase())
135 && name.chars().nth(item_name_chars + 1).map_or(false, |c| !c.is_numeric())
141 "variant name starts with the enum's name",
144 if count_match_end(item_name, &name).char_count == item_name_chars {
149 "variant name ends with the enum's name",
153 let first = &def.variants[0].ident.name.as_str();
154 let mut pre = &first[..str_utils::camel_case_until(&*first).byte_index];
155 let mut post = &first[str_utils::camel_case_start(&*first).byte_index..];
156 for var in def.variants {
157 let name = var.ident.name.as_str();
159 let pre_match = count_match_start(pre, &name).byte_count;
160 pre = &pre[..pre_match];
161 let pre_camel = str_utils::camel_case_until(pre).byte_index;
162 pre = &pre[..pre_camel];
163 while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() {
164 if next.is_numeric() {
167 if next.is_lowercase() {
168 let last = pre.len() - last.len_utf8();
169 let last_camel = str_utils::camel_case_until(&pre[..last]);
170 pre = &pre[..last_camel.byte_index];
176 let post_match = count_match_end(post, &name);
177 let post_end = post.len() - post_match.byte_count;
178 post = &post[post_end..];
179 let post_camel = str_utils::camel_case_start(post);
180 post = &post[post_camel.byte_index..];
182 let (what, value) = match (pre.is_empty(), post.is_empty()) {
183 (true, true) => return,
184 (false, _) => ("pre", pre),
185 (true, false) => ("post", post),
191 &format!("all variants have the same {}fix: `{}`", what, value),
194 "remove the {}fixes and use full paths to \
195 the variants instead of glob imports",
202 fn to_camel_case(item_name: &str) -> String {
203 let mut s = String::new();
205 for c in item_name.chars() {
206 if c.is_uppercase() {
207 // we only turn snake case text into CamelCase
208 return item_name.to_string();
216 s.extend(c.to_uppercase());
224 impl LateLintPass<'_> for EnumVariantNames {
225 fn check_item_post(&mut self, _cx: &LateContext<'_>, _item: &Item<'_>) {
226 let last = self.modules.pop();
227 assert!(last.is_some());
230 #[allow(clippy::similar_names)]
231 fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
232 let item_name = item.ident.name.as_str();
233 let item_name_chars = item_name.chars().count();
234 let item_camel = to_camel_case(&item_name);
235 if !item.span.from_expansion() && is_present_in_source(cx, item.span) {
236 if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
237 // constants don't have surrounding modules
238 if !mod_camel.is_empty() {
239 if mod_name == &item.ident.name {
240 if let ItemKind::Mod(..) = item.kind {
245 "module has the same name as its containing module",
249 // The `module_name_repetitions` lint should only trigger if the item has the module in its
250 // name. Having the same name is accepted.
251 if item.vis.node.is_pub() && item_camel.len() > mod_camel.len() {
252 let matching = count_match_start(mod_camel, &item_camel);
253 let rmatching = count_match_end(mod_camel, &item_camel);
254 let nchars = mod_camel.chars().count();
256 let is_word_beginning = |c: char| c == '_' || c.is_uppercase() || c.is_numeric();
258 if matching.char_count == nchars {
259 match item_camel.chars().nth(nchars) {
260 Some(c) if is_word_beginning(c) => span_lint(
262 MODULE_NAME_REPETITIONS,
264 "item name starts with its containing module's name",
269 if rmatching.char_count == nchars {
272 MODULE_NAME_REPETITIONS,
274 "item name ends with its containing module's name",
281 if let ItemKind::Enum(ref def, _) = item.kind {
282 if !(self.avoid_breaking_exported_api && cx.access_levels.is_exported(item.def_id)) {
283 check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span);
286 self.modules.push((item.ident.name, item_camel));