Skip to content

Commit 3e42c35

Browse files
committed
Auto merge of rust-lang#6981 - matthiaskrgr:6803_take_2, r=flip1995
disable upper_case_acronyms for pub items - enum edition Fixes rust-lang/rust-clippy#6803 (again... 😅 ) My previous fix did not work for enums because enum variants were checked separately in the `check_variant` function but it looks like we can't use that because we can't tell if the enum the variants belong to is declared as public or not (it always said `Inherited` for me) I went and special-cased enums and iterated over all the variants "manually", but only, if the enums is not public. --- changelog: fix upper_case_acronyms still firing on public enums (rust-lang#6803)
2 parents 2e33bf6 + ca7e955 commit 3e42c35

File tree

5 files changed

+62
-15
lines changed

5 files changed

+62
-15
lines changed

clippy_lints/src/upper_case_acronyms.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use if_chain::if_chain;
32
use itertools::Itertools;
4-
use rustc_ast::ast::{Item, ItemKind, Variant, VisibilityKind};
3+
use rustc_ast::ast::{Item, ItemKind, VisibilityKind};
54
use rustc_errors::Applicability;
65
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
76
use rustc_middle::lint::in_external_macro;
@@ -99,21 +98,21 @@ fn check_ident(cx: &EarlyContext<'_>, ident: &Ident, be_aggressive: bool) {
9998

10099
impl EarlyLintPass for UpperCaseAcronyms {
101100
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &Item) {
102-
if_chain! {
103-
if !in_external_macro(cx.sess(), it.span);
101+
// do not lint public items or in macros
102+
if !in_external_macro(cx.sess(), it.span) && !matches!(it.vis.kind, VisibilityKind::Public) {
104103
if matches!(
105104
it.kind,
106-
ItemKind::TyAlias(..) | ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Trait(..)
107-
);
108-
// do not lint public items
109-
if !matches!(it.vis.kind, VisibilityKind::Public);
110-
then {
105+
ItemKind::TyAlias(..) | ItemKind::Struct(..) | ItemKind::Trait(..)
106+
) {
111107
check_ident(cx, &it.ident, self.upper_case_acronyms_aggressive);
108+
} else if let ItemKind::Enum(ref enumdef, _) = it.kind {
109+
// check enum variants seperately because again we only want to lint on private enums and
110+
// the fn check_variant does not know about the vis of the enum of its variants
111+
enumdef
112+
.variants
113+
.iter()
114+
.for_each(|variant| check_ident(cx, &variant.ident, self.upper_case_acronyms_aggressive));
112115
}
113116
}
114117
}
115-
116-
fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &Variant) {
117-
check_ident(cx, &v.ident, self.upper_case_acronyms_aggressive);
118-
}
119118
}

tests/ui-toml/upper_case_acronyms_aggressive/upper_case_acronyms.rs

+16
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,20 @@ pub struct MIXEDCapital;
2525

2626
pub struct FULLCAPITAL;
2727

28+
// enum variants should not be linted if the num is pub
29+
pub enum ParseError<T> {
30+
FULLCAPITAL(u8),
31+
MIXEDCapital(String),
32+
Utf8(std::string::FromUtf8Error),
33+
Parse(T, String),
34+
}
35+
36+
// private, do lint here
37+
enum ParseErrorPrivate<T> {
38+
WASD(u8),
39+
WASDMixed(String),
40+
Utf8(std::string::FromUtf8Error),
41+
Parse(T, String),
42+
}
43+
2844
fn main() {}

tests/ui-toml/upper_case_acronyms_aggressive/upper_case_acronyms.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,17 @@ error: name `GCCLLVMSomething` contains a capitalized acronym
6666
LL | struct GCCLLVMSomething;
6767
| ^^^^^^^^^^^^^^^^ help: consider making the acronym lowercase, except the initial letter: `GccllvmSomething`
6868

69-
error: aborting due to 11 previous errors
69+
error: name `WASD` contains a capitalized acronym
70+
--> $DIR/upper_case_acronyms.rs:38:5
71+
|
72+
LL | WASD(u8),
73+
| ^^^^ help: consider making the acronym lowercase, except the initial letter: `Wasd`
74+
75+
error: name `WASDMixed` contains a capitalized acronym
76+
--> $DIR/upper_case_acronyms.rs:39:5
77+
|
78+
LL | WASDMixed(String),
79+
| ^^^^^^^^^ help: consider making the acronym lowercase, except the initial letter: `WasdMixed`
80+
81+
error: aborting due to 13 previous errors
7082

tests/ui/upper_case_acronyms.rs

+14
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,18 @@ struct GCCLLVMSomething;
2424
pub struct NOWARNINGHERE;
2525
pub struct ALSONoWarningHERE;
2626

27+
// enum variants should not be linted if the num is pub
28+
pub enum ParseError<T> {
29+
YDB(u8),
30+
Utf8(std::string::FromUtf8Error),
31+
Parse(T, String),
32+
}
33+
34+
// private, do lint here
35+
enum ParseErrorPrivate<T> {
36+
WASD(u8),
37+
Utf8(std::string::FromUtf8Error),
38+
Parse(T, String),
39+
}
40+
2741
fn main() {}

tests/ui/upper_case_acronyms.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ error: name `FIN` contains a capitalized acronym
4848
LL | FIN,
4949
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Fin`
5050

51-
error: aborting due to 8 previous errors
51+
error: name `WASD` contains a capitalized acronym
52+
--> $DIR/upper_case_acronyms.rs:36:5
53+
|
54+
LL | WASD(u8),
55+
| ^^^^ help: consider making the acronym lowercase, except the initial letter: `Wasd`
56+
57+
error: aborting due to 9 previous errors
5258

0 commit comments

Comments
 (0)