2016-02-01 05:47:31 -06:00
|
|
|
//! lint on enum variants that are prefixed or suffixed by the same characters
|
|
|
|
|
2023-04-22 09:53:15 -05:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_hir};
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::source::is_present_in_source;
|
2021-12-30 08:10:43 -06:00
|
|
|
use clippy_utils::str_utils::{camel_case_split, count_match_end, count_match_start};
|
2023-06-09 14:27:34 -05:00
|
|
|
use rustc_hir::{EnumDef, Item, ItemKind, OwnerId, Variant};
|
2021-06-03 01:41:37 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2020-01-04 04:00:00 -06:00
|
|
|
use rustc_span::source_map::Span;
|
|
|
|
use rustc_span::symbol::Symbol;
|
2016-02-01 05:47:31 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Detects enumeration variants that are prefixed or suffixed
|
2019-03-05 10:50:33 -06:00
|
|
|
/// by the same characters.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Enumeration variant names should specify their variant,
|
2019-03-05 10:50:33 -06:00
|
|
|
/// not repeat the enumeration name.
|
|
|
|
///
|
2021-12-30 08:10:43 -06:00
|
|
|
/// ### Limitations
|
|
|
|
/// Characters with no casing will be considered when comparing prefixes/suffixes
|
|
|
|
/// This applies to numbers and non-ascii characters without casing
|
|
|
|
/// e.g. `Foo1` and `Foo2` is considered to have different prefixes
|
|
|
|
/// (the prefixes are `Foo1` and `Foo2` respectively), as also `Bar螃`, `Bar蟹`
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
|
|
|
/// enum Cake {
|
|
|
|
/// BlackForestCake,
|
|
|
|
/// HummingbirdCake,
|
|
|
|
/// BattenbergCake,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2022-06-04 06:34:07 -05:00
|
|
|
/// Use instead:
|
2020-06-09 09:36:01 -05:00
|
|
|
/// ```rust
|
|
|
|
/// enum Cake {
|
|
|
|
/// BlackForest,
|
|
|
|
/// Hummingbird,
|
|
|
|
/// Battenberg,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-08-06 03:18:36 -05:00
|
|
|
pub ENUM_VARIANT_NAMES,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2016-08-06 03:18:36 -05:00
|
|
|
"enums where all variants share a prefix/postfix"
|
2016-02-05 17:13:29 -06:00
|
|
|
}
|
2016-02-01 05:47:31 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Detects type names that are prefixed or suffixed by the
|
2019-03-05 10:50:33 -06:00
|
|
|
/// containing module's name.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It requires the user to type the module name twice.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
|
|
|
/// mod cake {
|
|
|
|
/// struct BlackForestCake;
|
|
|
|
/// }
|
|
|
|
/// ```
|
2022-06-16 10:39:06 -05:00
|
|
|
///
|
|
|
|
/// Use instead:
|
2020-06-09 09:36:01 -05:00
|
|
|
/// ```rust
|
|
|
|
/// mod cake {
|
|
|
|
/// struct BlackForest;
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.33.0"]
|
2018-12-17 07:29:19 -06:00
|
|
|
pub MODULE_NAME_REPETITIONS,
|
2018-03-28 08:24:26 -05:00
|
|
|
pedantic,
|
2016-08-06 03:18:36 -05:00
|
|
|
"type names prefixed/postfixed with their containing module's name"
|
2016-06-16 11:37:56 -05:00
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for modules that have the same name as their
|
2019-03-05 10:50:33 -06:00
|
|
|
/// parent module
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// A typical beginner mistake is to have `mod foo;` and
|
2019-03-05 10:50:33 -06:00
|
|
|
/// again `mod foo { ..
|
|
|
|
/// }` in `foo.rs`.
|
|
|
|
/// The expectation is that items inside the inner `mod foo { .. }` are then
|
|
|
|
/// available
|
|
|
|
/// through `foo::x`, but they are only available through
|
|
|
|
/// `foo::foo::x`.
|
|
|
|
/// If this is done on purpose, it would be better to choose a more
|
|
|
|
/// representative module name.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 16:23:50 -06:00
|
|
|
/// ```ignore
|
2019-03-05 10:50:33 -06:00
|
|
|
/// // lib.rs
|
|
|
|
/// mod foo;
|
|
|
|
/// // foo.rs
|
|
|
|
/// mod foo {
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-09-12 03:30:42 -05:00
|
|
|
pub MODULE_INCEPTION,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2016-09-12 03:30:42 -05:00
|
|
|
"modules that have the same name as their parent module"
|
|
|
|
}
|
|
|
|
|
2016-06-08 10:43:27 -05:00
|
|
|
pub struct EnumVariantNames {
|
2023-06-09 14:27:34 -05:00
|
|
|
modules: Vec<(Symbol, String, OwnerId)>,
|
2016-08-06 13:59:27 -05:00
|
|
|
threshold: u64,
|
2021-06-03 01:41:37 -05:00
|
|
|
avoid_breaking_exported_api: bool,
|
2023-06-09 14:27:34 -05:00
|
|
|
allow_private_module_inception: bool,
|
2016-08-06 13:59:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumVariantNames {
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2023-06-09 14:27:34 -05:00
|
|
|
pub fn new(threshold: u64, avoid_breaking_exported_api: bool, allow_private_module_inception: bool) -> Self {
|
2017-08-21 06:32:12 -05:00
|
|
|
Self {
|
2016-12-20 11:21:30 -06:00
|
|
|
modules: Vec::new(),
|
2018-03-15 10:07:15 -05:00
|
|
|
threshold,
|
2021-06-03 01:41:37 -05:00
|
|
|
avoid_breaking_exported_api,
|
2023-06-09 14:27:34 -05:00
|
|
|
allow_private_module_inception,
|
2016-12-20 11:21:30 -06:00
|
|
|
}
|
2016-08-06 13:59:27 -05:00
|
|
|
}
|
2016-06-08 10:43:27 -05:00
|
|
|
}
|
2016-02-01 05:47:31 -06:00
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl_lint_pass!(EnumVariantNames => [
|
|
|
|
ENUM_VARIANT_NAMES,
|
|
|
|
MODULE_NAME_REPETITIONS,
|
|
|
|
MODULE_INCEPTION
|
|
|
|
]);
|
2016-02-01 05:47:31 -06:00
|
|
|
|
2021-12-30 08:10:43 -06:00
|
|
|
fn check_enum_start(cx: &LateContext<'_>, item_name: &str, variant: &Variant<'_>) {
|
|
|
|
let name = variant.ident.name.as_str();
|
|
|
|
let item_name_chars = item_name.chars().count();
|
|
|
|
|
|
|
|
if count_match_start(item_name, name).char_count == item_name_chars
|
|
|
|
&& name.chars().nth(item_name_chars).map_or(false, |c| !c.is_lowercase())
|
|
|
|
&& name.chars().nth(item_name_chars + 1).map_or(false, |c| !c.is_numeric())
|
|
|
|
{
|
2023-04-22 09:53:15 -05:00
|
|
|
span_lint_hir(
|
2021-12-30 08:10:43 -06:00
|
|
|
cx,
|
|
|
|
ENUM_VARIANT_NAMES,
|
2023-04-22 09:53:15 -05:00
|
|
|
variant.hir_id,
|
2021-12-30 08:10:43 -06:00
|
|
|
variant.span,
|
|
|
|
"variant name starts with the enum's name",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_enum_end(cx: &LateContext<'_>, item_name: &str, variant: &Variant<'_>) {
|
|
|
|
let name = variant.ident.name.as_str();
|
|
|
|
let item_name_chars = item_name.chars().count();
|
|
|
|
|
|
|
|
if count_match_end(item_name, name).char_count == item_name_chars {
|
2023-04-22 09:53:15 -05:00
|
|
|
span_lint_hir(
|
2021-12-30 08:10:43 -06:00
|
|
|
cx,
|
|
|
|
ENUM_VARIANT_NAMES,
|
2023-04-22 09:53:15 -05:00
|
|
|
variant.hir_id,
|
2021-12-30 08:10:43 -06:00
|
|
|
variant.span,
|
|
|
|
"variant name ends with the enum's name",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_name: &str, span: Span) {
|
2016-08-06 13:59:27 -05:00
|
|
|
if (def.variants.len() as u64) < threshold {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-30 08:10:43 -06:00
|
|
|
|
|
|
|
let first = &def.variants[0].ident.name.as_str();
|
|
|
|
let mut pre = camel_case_split(first);
|
|
|
|
let mut post = pre.clone();
|
|
|
|
post.reverse();
|
2021-06-03 01:41:37 -05:00
|
|
|
for var in def.variants {
|
2021-12-30 08:10:43 -06:00
|
|
|
check_enum_start(cx, item_name, var);
|
|
|
|
check_enum_end(cx, item_name, var);
|
2019-09-05 01:56:10 -05:00
|
|
|
let name = var.ident.name.as_str();
|
2016-06-08 10:43:27 -05:00
|
|
|
|
2021-12-30 08:10:43 -06:00
|
|
|
let variant_split = camel_case_split(name);
|
2022-01-27 08:12:45 -06:00
|
|
|
if variant_split.len() == 1 {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-08 10:43:27 -05:00
|
|
|
|
2021-12-30 08:10:43 -06:00
|
|
|
pre = pre
|
|
|
|
.iter()
|
|
|
|
.zip(variant_split.iter())
|
|
|
|
.take_while(|(a, b)| a == b)
|
|
|
|
.map(|e| *e.0)
|
|
|
|
.collect();
|
|
|
|
post = post
|
|
|
|
.iter()
|
|
|
|
.zip(variant_split.iter().rev())
|
|
|
|
.take_while(|(a, b)| a == b)
|
|
|
|
.map(|e| *e.0)
|
|
|
|
.collect();
|
2016-06-08 10:43:27 -05:00
|
|
|
}
|
2022-06-30 03:50:09 -05:00
|
|
|
let (what, value) = match (have_no_extra_prefix(&pre), post.is_empty()) {
|
2016-06-08 10:43:27 -05:00
|
|
|
(true, true) => return,
|
2021-12-30 08:10:43 -06:00
|
|
|
(false, _) => ("pre", pre.join("")),
|
|
|
|
(true, false) => {
|
|
|
|
post.reverse();
|
|
|
|
("post", post.join(""))
|
|
|
|
},
|
2016-06-08 10:43:27 -05:00
|
|
|
};
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2017-08-09 02:30:56 -05:00
|
|
|
cx,
|
2021-06-03 01:41:37 -05:00
|
|
|
ENUM_VARIANT_NAMES,
|
2017-08-09 02:30:56 -05:00
|
|
|
span,
|
2022-10-06 02:44:38 -05:00
|
|
|
&format!("all variants have the same {what}fix: `{value}`"),
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2017-08-09 02:30:56 -05:00
|
|
|
&format!(
|
2022-10-06 02:44:38 -05:00
|
|
|
"remove the {what}fixes and use full paths to \
|
|
|
|
the variants instead of glob imports"
|
2017-08-09 02:30:56 -05:00
|
|
|
),
|
|
|
|
);
|
2016-06-08 10:43:27 -05:00
|
|
|
}
|
|
|
|
|
2022-06-30 03:50:09 -05:00
|
|
|
#[must_use]
|
|
|
|
fn have_no_extra_prefix(prefixes: &[&str]) -> bool {
|
|
|
|
prefixes.iter().all(|p| p == &"" || p == &"_")
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2016-06-08 10:43:27 -05:00
|
|
|
fn to_camel_case(item_name: &str) -> String {
|
|
|
|
let mut s = String::new();
|
|
|
|
let mut up = true;
|
|
|
|
for c in item_name.chars() {
|
|
|
|
if c.is_uppercase() {
|
|
|
|
// we only turn snake case text into CamelCase
|
|
|
|
return item_name.to_string();
|
|
|
|
}
|
|
|
|
if c == '_' {
|
|
|
|
up = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if up {
|
|
|
|
up = false;
|
|
|
|
s.extend(c.to_uppercase());
|
|
|
|
} else {
|
|
|
|
s.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2021-06-03 01:41:37 -05:00
|
|
|
impl LateLintPass<'_> for EnumVariantNames {
|
|
|
|
fn check_item_post(&mut self, _cx: &LateContext<'_>, _item: &Item<'_>) {
|
2016-06-08 10:43:27 -05:00
|
|
|
let last = self.modules.pop();
|
|
|
|
assert!(last.is_some());
|
|
|
|
}
|
|
|
|
|
2022-05-21 06:24:00 -05:00
|
|
|
#[expect(clippy::similar_names)]
|
2021-06-03 01:41:37 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
2019-09-05 01:56:10 -05:00
|
|
|
let item_name = item.ident.name.as_str();
|
2021-12-30 08:10:43 -06:00
|
|
|
let item_camel = to_camel_case(item_name);
|
2019-08-19 11:30:32 -05:00
|
|
|
if !item.span.from_expansion() && is_present_in_source(cx, item.span) {
|
2023-06-09 14:27:34 -05:00
|
|
|
if let [.., (mod_name, mod_camel, owner_id)] = &*self.modules {
|
2016-06-10 09:30:39 -05:00
|
|
|
// constants don't have surrounding modules
|
|
|
|
if !mod_camel.is_empty() {
|
2023-06-09 14:27:34 -05:00
|
|
|
if mod_name == &item.ident.name
|
|
|
|
&& let ItemKind::Mod(..) = item.kind
|
|
|
|
&& (!self.allow_private_module_inception || cx.tcx.visibility(owner_id.def_id).is_public())
|
|
|
|
{
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
MODULE_INCEPTION,
|
|
|
|
item.span,
|
|
|
|
"module has the same name as its containing module",
|
|
|
|
);
|
2016-09-12 03:30:42 -05:00
|
|
|
}
|
2021-11-04 07:52:36 -05:00
|
|
|
// The `module_name_repetitions` lint should only trigger if the item has the module in its
|
|
|
|
// name. Having the same name is accepted.
|
2022-10-26 22:02:18 -05:00
|
|
|
if cx.tcx.visibility(item.owner_id).is_public() && item_camel.len() > mod_camel.len() {
|
2021-11-04 07:52:36 -05:00
|
|
|
let matching = count_match_start(mod_camel, &item_camel);
|
|
|
|
let rmatching = count_match_end(mod_camel, &item_camel);
|
2016-09-13 03:19:55 -05:00
|
|
|
let nchars = mod_camel.chars().count();
|
2017-11-26 11:36:12 -06:00
|
|
|
|
2018-11-27 14:14:15 -06:00
|
|
|
let is_word_beginning = |c: char| c == '_' || c.is_uppercase() || c.is_numeric();
|
2017-11-26 11:36:12 -06:00
|
|
|
|
2021-11-04 07:52:36 -05:00
|
|
|
if matching.char_count == nchars {
|
2017-11-26 11:36:12 -06:00
|
|
|
match item_camel.chars().nth(nchars) {
|
2018-11-27 14:14:15 -06:00
|
|
|
Some(c) if is_word_beginning(c) => span_lint(
|
|
|
|
cx,
|
2018-12-17 07:29:19 -06:00
|
|
|
MODULE_NAME_REPETITIONS,
|
2023-01-25 00:07:10 -06:00
|
|
|
item.ident.span,
|
2018-11-27 14:14:15 -06:00
|
|
|
"item name starts with its containing module's name",
|
|
|
|
),
|
|
|
|
_ => (),
|
2017-11-26 11:36:12 -06:00
|
|
|
}
|
2016-09-13 03:19:55 -05:00
|
|
|
}
|
2021-11-04 07:52:36 -05:00
|
|
|
if rmatching.char_count == nchars {
|
2018-11-27 14:14:15 -06:00
|
|
|
span_lint(
|
|
|
|
cx,
|
2018-12-17 07:29:19 -06:00
|
|
|
MODULE_NAME_REPETITIONS,
|
2023-01-25 00:07:10 -06:00
|
|
|
item.ident.span,
|
2018-11-27 14:14:15 -06:00
|
|
|
"item name ends with its containing module's name",
|
|
|
|
);
|
2016-09-13 03:19:55 -05:00
|
|
|
}
|
2016-02-01 05:47:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.kind {
|
2022-10-26 22:02:18 -05:00
|
|
|
if !(self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(item.owner_id.def_id)) {
|
2021-12-30 08:10:43 -06:00
|
|
|
check_variant(cx, self.threshold, def, item_name, item.span);
|
2021-06-03 01:41:37 -05:00
|
|
|
}
|
2016-06-08 10:43:27 -05:00
|
|
|
}
|
2023-06-09 14:27:34 -05:00
|
|
|
self.modules.push((item.ident.name, item_camel, item.owner_id));
|
2016-02-01 05:47:31 -06:00
|
|
|
}
|
|
|
|
}
|