2023-11-30 09:48:17 -06:00
|
|
|
use clippy_utils::diagnostics::span_lint_hir_and_then;
|
2021-01-30 11:06:34 -06:00
|
|
|
use itertools::Itertools;
|
|
|
|
use rustc_errors::Applicability;
|
2023-11-30 09:48:17 -06:00
|
|
|
use rustc_hir::{HirId, Item, ItemKind};
|
2021-06-03 01:41:37 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2021-01-30 11:06:34 -06:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2023-11-25 11:45:27 -06:00
|
|
|
use rustc_session::impl_lint_pass;
|
2021-01-30 11:06:34 -06:00
|
|
|
use rustc_span::symbol::Ident;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for fully capitalized names and optionally names containing a capitalized acronym.
|
2021-01-30 11:06:34 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// In CamelCase, acronyms count as one word.
|
2021-01-30 11:06:34 -06:00
|
|
|
/// See [naming conventions](https://rust-lang.github.io/api-guidelines/naming.html#casing-conforms-to-rfc-430-c-case)
|
|
|
|
/// for more.
|
|
|
|
///
|
2021-02-25 04:25:22 -06:00
|
|
|
/// By default, the lint only triggers on fully-capitalized names.
|
|
|
|
/// You can use the `upper-case-acronyms-aggressive: true` config option to enable linting
|
|
|
|
/// on all camel case names
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// When two acronyms are contiguous, the lint can't tell where
|
2021-01-30 11:06:34 -06:00
|
|
|
/// the first acronym ends and the second starts, so it suggests to lowercase all of
|
|
|
|
/// the letters in the second acronym.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2023-10-23 08:49:18 -05:00
|
|
|
/// ```no_run
|
2021-01-30 11:06:34 -06:00
|
|
|
/// struct HTTPResponse;
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-10-23 08:49:18 -05:00
|
|
|
/// ```no_run
|
2021-01-30 11:06:34 -06:00
|
|
|
/// struct HttpResponse;
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.51.0"]
|
2021-01-30 11:06:34 -06:00
|
|
|
pub UPPER_CASE_ACRONYMS,
|
|
|
|
style,
|
|
|
|
"capitalized acronyms are against the naming convention"
|
|
|
|
}
|
|
|
|
|
2021-02-25 04:25:22 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct UpperCaseAcronyms {
|
2021-06-03 01:41:37 -05:00
|
|
|
avoid_breaking_exported_api: bool,
|
2021-02-25 04:25:22 -06:00
|
|
|
upper_case_acronyms_aggressive: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpperCaseAcronyms {
|
2021-06-03 01:41:37 -05:00
|
|
|
pub fn new(avoid_breaking_exported_api: bool, aggressive: bool) -> Self {
|
2021-02-25 04:25:22 -06:00
|
|
|
Self {
|
2021-06-03 01:41:37 -05:00
|
|
|
avoid_breaking_exported_api,
|
2021-02-25 04:25:22 -06:00
|
|
|
upper_case_acronyms_aggressive: aggressive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(UpperCaseAcronyms => [UPPER_CASE_ACRONYMS]);
|
2021-01-30 11:06:34 -06:00
|
|
|
|
|
|
|
fn correct_ident(ident: &str) -> String {
|
|
|
|
let ident = ident.chars().rev().collect::<String>();
|
|
|
|
let fragments = ident
|
|
|
|
.split_inclusive(|x: char| !x.is_ascii_lowercase())
|
|
|
|
.rev()
|
|
|
|
.map(|x| x.chars().rev().collect::<String>());
|
|
|
|
|
|
|
|
let mut ident = fragments.clone().next().unwrap();
|
|
|
|
for (ref prev, ref curr) in fragments.tuple_windows() {
|
2023-06-24 08:24:13 -05:00
|
|
|
if <[&String; 2]>::from((prev, curr))
|
2021-01-30 11:06:34 -06:00
|
|
|
.iter()
|
|
|
|
.all(|s| s.len() == 1 && s.chars().next().unwrap().is_ascii_uppercase())
|
|
|
|
{
|
|
|
|
ident.push_str(&curr.to_ascii_lowercase());
|
|
|
|
} else {
|
|
|
|
ident.push_str(curr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ident
|
|
|
|
}
|
|
|
|
|
2023-11-30 09:48:17 -06:00
|
|
|
fn check_ident(cx: &LateContext<'_>, ident: &Ident, hir_id: HirId, be_aggressive: bool) {
|
2021-01-30 11:06:34 -06:00
|
|
|
let span = ident.span;
|
2021-12-14 23:13:11 -06:00
|
|
|
let ident = ident.as_str();
|
2021-01-30 11:06:34 -06:00
|
|
|
let corrected = correct_ident(ident);
|
2021-02-25 04:25:22 -06:00
|
|
|
// warn if we have pure-uppercase idents
|
|
|
|
// assume that two-letter words are some kind of valid abbreviation like FP for false positive
|
|
|
|
// (and don't warn)
|
|
|
|
if (ident.chars().all(|c| c.is_ascii_uppercase()) && ident.len() > 2)
|
2021-03-25 13:29:11 -05:00
|
|
|
// otherwise, warn if we have SOmeTHING lIKE THIs but only warn with the aggressive
|
2021-02-25 04:25:22 -06:00
|
|
|
// upper-case-acronyms-aggressive config option enabled
|
2021-12-30 08:10:43 -06:00
|
|
|
|| (be_aggressive && ident != corrected)
|
2021-02-25 04:25:22 -06:00
|
|
|
{
|
2023-11-30 09:48:17 -06:00
|
|
|
span_lint_hir_and_then(
|
2021-01-30 11:06:34 -06:00
|
|
|
cx,
|
|
|
|
UPPER_CASE_ACRONYMS,
|
2023-11-30 09:48:17 -06:00
|
|
|
hir_id,
|
2021-01-30 11:06:34 -06:00
|
|
|
span,
|
2024-03-23 00:52:11 -05:00
|
|
|
format!("name `{ident}` contains a capitalized acronym"),
|
2023-11-30 09:48:17 -06:00
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
|
|
|
span,
|
|
|
|
"consider making the acronym lowercase, except the initial letter",
|
|
|
|
corrected,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
},
|
2021-06-03 01:41:37 -05:00
|
|
|
);
|
2021-01-30 11:06:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 01:41:37 -05:00
|
|
|
impl LateLintPass<'_> for UpperCaseAcronyms {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'_>, it: &Item<'_>) {
|
2021-04-08 10:50:13 -05:00
|
|
|
// do not lint public items or in macros
|
2021-06-03 01:41:37 -05:00
|
|
|
if in_external_macro(cx.sess(), it.span)
|
2022-10-26 22:02:18 -05:00
|
|
|
|| (self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(it.owner_id.def_id))
|
2021-06-03 01:41:37 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match it.kind {
|
|
|
|
ItemKind::TyAlias(..) | ItemKind::Struct(..) | ItemKind::Trait(..) => {
|
2023-11-30 09:48:17 -06:00
|
|
|
check_ident(cx, &it.ident, it.hir_id(), self.upper_case_acronyms_aggressive);
|
2021-06-03 01:41:37 -05:00
|
|
|
},
|
|
|
|
ItemKind::Enum(ref enumdef, _) => {
|
2023-11-30 09:48:17 -06:00
|
|
|
check_ident(cx, &it.ident, it.hir_id(), self.upper_case_acronyms_aggressive);
|
2022-03-14 20:00:08 -05:00
|
|
|
// check enum variants separately because again we only want to lint on private enums and
|
2021-04-08 10:50:13 -05:00
|
|
|
// the fn check_variant does not know about the vis of the enum of its variants
|
2023-11-30 09:48:17 -06:00
|
|
|
enumdef.variants.iter().for_each(|variant| {
|
|
|
|
check_ident(cx, &variant.ident, variant.hir_id, self.upper_case_acronyms_aggressive);
|
|
|
|
});
|
2021-06-03 01:41:37 -05:00
|
|
|
},
|
|
|
|
_ => {},
|
2021-01-30 11:06:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|