2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2020-12-19 13:50:45 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use itertools::Itertools;
|
2021-02-26 20:22:30 +00:00
|
|
|
use rustc_ast::ast::{Item, ItemKind, Variant, VisibilityKind};
|
2020-12-19 13:50:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
2021-02-24 22:20:49 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2020-12-19 13:50:45 +00:00
|
|
|
use rustc_span::symbol::Ident;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-02-24 22:20:49 +00:00
|
|
|
/// **What it does:** Checks for fully capitalized names and optionally names containing a capitalized acronym.
|
2020-12-19 13:50:45 +00:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** In CamelCase, acronyms count as one word.
|
|
|
|
/// See [naming conventions](https://rust-lang.github.io/api-guidelines/naming.html#casing-conforms-to-rfc-430-c-case)
|
|
|
|
/// for more.
|
|
|
|
///
|
2021-02-24 22:20:49 +00:00
|
|
|
/// By default, the lint only triggers on fully-capitalized names.
|
2021-02-24 22:41:32 +00:00
|
|
|
/// You can use the `upper-case-acronyms-aggressive: true` config option to enable linting
|
2021-02-24 22:20:49 +00:00
|
|
|
/// on all camel case names
|
|
|
|
///
|
2020-12-19 13:50:45 +00:00
|
|
|
/// **Known problems:** When two acronyms are contiguous, the lint can't tell where
|
|
|
|
/// the first acronym ends and the second starts, so it suggests to lowercase all of
|
|
|
|
/// the letters in the second acronym.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct HTTPResponse;
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// struct HttpResponse;
|
|
|
|
/// ```
|
|
|
|
pub UPPER_CASE_ACRONYMS,
|
2021-02-24 22:20:49 +00:00
|
|
|
style,
|
2020-12-19 13:50:45 +00:00
|
|
|
"capitalized acronyms are against the naming convention"
|
|
|
|
}
|
|
|
|
|
2021-02-24 22:20:49 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct UpperCaseAcronyms {
|
|
|
|
upper_case_acronyms_aggressive: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpperCaseAcronyms {
|
|
|
|
pub fn new(aggressive: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
upper_case_acronyms_aggressive: aggressive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(UpperCaseAcronyms => [UPPER_CASE_ACRONYMS]);
|
2020-12-19 13:50:45 +00: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() {
|
|
|
|
if [prev, curr]
|
|
|
|
.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
|
|
|
|
}
|
|
|
|
|
2021-02-24 22:20:49 +00:00
|
|
|
fn check_ident(cx: &EarlyContext<'_>, ident: &Ident, be_aggressive: bool) {
|
2020-12-19 13:50:45 +00:00
|
|
|
let span = ident.span;
|
|
|
|
let ident = &ident.as_str();
|
|
|
|
let corrected = correct_ident(ident);
|
2021-02-24 22:20:49 +00: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-16 00:55:45 +00:00
|
|
|
// otherwise, warn if we have SOmeTHING lIKE THIs but only warn with the aggressive
|
2021-02-24 22:41:32 +00:00
|
|
|
// upper-case-acronyms-aggressive config option enabled
|
2021-02-24 22:20:49 +00:00
|
|
|
|| (be_aggressive && ident != &corrected)
|
|
|
|
{
|
2020-12-19 13:50:45 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
UPPER_CASE_ACRONYMS,
|
|
|
|
span,
|
|
|
|
&format!("name `{}` contains a capitalized acronym", ident),
|
|
|
|
"consider making the acronym lowercase, except the initial letter",
|
|
|
|
corrected,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for UpperCaseAcronyms {
|
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &Item) {
|
|
|
|
if_chain! {
|
|
|
|
if !in_external_macro(cx.sess(), it.span);
|
|
|
|
if matches!(
|
|
|
|
it.kind,
|
|
|
|
ItemKind::TyAlias(..) | ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Trait(..)
|
|
|
|
);
|
2021-02-26 20:22:30 +00:00
|
|
|
// do not lint public items
|
|
|
|
if !matches!(it.vis.kind, VisibilityKind::Public);
|
2020-12-19 13:50:45 +00:00
|
|
|
then {
|
2021-02-24 22:20:49 +00:00
|
|
|
check_ident(cx, &it.ident, self.upper_case_acronyms_aggressive);
|
2020-12-19 13:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &Variant) {
|
2021-02-24 22:20:49 +00:00
|
|
|
check_ident(cx, &v.ident, self.upper_case_acronyms_aggressive);
|
2020-12-19 13:50:45 +00:00
|
|
|
}
|
|
|
|
}
|