Skip to content

Commit d166fab

Browse files
committed
Auto merge of #11898 - clubby789:upper_case_acronyms_variants, r=Manishearth
Allow `allow`ing `upper_case_acronyms` on enum variants Fixes #7708 changelog: [`upper_case_acronyms`]: allow `allow`ing on enum variants
2 parents 646b28f + 2cda044 commit d166fab

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

clippy_lints/src/upper_case_acronyms.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use itertools::Itertools;
33
use rustc_errors::Applicability;
4-
use rustc_hir::{Item, ItemKind};
4+
use rustc_hir::{HirId, Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass, LintContext};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_session::impl_lint_pass;
@@ -77,7 +77,7 @@ fn correct_ident(ident: &str) -> String {
7777
ident
7878
}
7979

80-
fn check_ident(cx: &LateContext<'_>, ident: &Ident, be_aggressive: bool) {
80+
fn check_ident(cx: &LateContext<'_>, ident: &Ident, hir_id: HirId, be_aggressive: bool) {
8181
let span = ident.span;
8282
let ident = ident.as_str();
8383
let corrected = correct_ident(ident);
@@ -89,14 +89,20 @@ fn check_ident(cx: &LateContext<'_>, ident: &Ident, be_aggressive: bool) {
8989
// upper-case-acronyms-aggressive config option enabled
9090
|| (be_aggressive && ident != corrected)
9191
{
92-
span_lint_and_sugg(
92+
span_lint_hir_and_then(
9393
cx,
9494
UPPER_CASE_ACRONYMS,
95+
hir_id,
9596
span,
9697
&format!("name `{ident}` contains a capitalized acronym"),
97-
"consider making the acronym lowercase, except the initial letter",
98-
corrected,
99-
Applicability::MaybeIncorrect,
98+
|diag| {
99+
diag.span_suggestion(
100+
span,
101+
"consider making the acronym lowercase, except the initial letter",
102+
corrected,
103+
Applicability::MaybeIncorrect,
104+
);
105+
},
100106
);
101107
}
102108
}
@@ -111,16 +117,15 @@ impl LateLintPass<'_> for UpperCaseAcronyms {
111117
}
112118
match it.kind {
113119
ItemKind::TyAlias(..) | ItemKind::Struct(..) | ItemKind::Trait(..) => {
114-
check_ident(cx, &it.ident, self.upper_case_acronyms_aggressive);
120+
check_ident(cx, &it.ident, it.hir_id(), self.upper_case_acronyms_aggressive);
115121
},
116122
ItemKind::Enum(ref enumdef, _) => {
117-
check_ident(cx, &it.ident, self.upper_case_acronyms_aggressive);
123+
check_ident(cx, &it.ident, it.hir_id(), self.upper_case_acronyms_aggressive);
118124
// check enum variants separately because again we only want to lint on private enums and
119125
// the fn check_variant does not know about the vis of the enum of its variants
120-
enumdef
121-
.variants
122-
.iter()
123-
.for_each(|variant| check_ident(cx, &variant.ident, self.upper_case_acronyms_aggressive));
126+
enumdef.variants.iter().for_each(|variant| {
127+
check_ident(cx, &variant.ident, variant.hir_id, self.upper_case_acronyms_aggressive);
128+
});
124129
},
125130
_ => {},
126131
}

tests/ui/upper_case_acronyms.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ enum Yaml {
5959
Str(String),
6060
}
6161

62+
// test for issue #7708
63+
enum AllowOnField {
64+
Disallow,
65+
//~^ ERROR: name `DISALLOW` contains a capitalized acronym
66+
#[allow(clippy::upper_case_acronyms)]
67+
ALLOW,
68+
}
69+
6270
fn main() {}

tests/ui/upper_case_acronyms.rs

+8
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ enum YAML {
5959
Str(String),
6060
}
6161

62+
// test for issue #7708
63+
enum AllowOnField {
64+
DISALLOW,
65+
//~^ ERROR: name `DISALLOW` contains a capitalized acronym
66+
#[allow(clippy::upper_case_acronyms)]
67+
ALLOW,
68+
}
69+
6270
fn main() {}

tests/ui/upper_case_acronyms.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,11 @@ error: name `YAML` contains a capitalized acronym
6767
LL | enum YAML {
6868
| ^^^^ help: consider making the acronym lowercase, except the initial letter: `Yaml`
6969

70-
error: aborting due to 11 previous errors
70+
error: name `DISALLOW` contains a capitalized acronym
71+
--> $DIR/upper_case_acronyms.rs:64:5
72+
|
73+
LL | DISALLOW,
74+
| ^^^^^^^^ help: consider making the acronym lowercase, except the initial letter: `Disallow`
75+
76+
error: aborting due to 12 previous errors
7177

0 commit comments

Comments
 (0)