Skip to content

Commit ef41f2b

Browse files
committed
Auto merge of #6788 - matthiaskrgr:upper_case_acronyms, r=flip1995
move upper_case_acronyms back to style, but make the default behaviour less aggressive by default (can be unleashed via config option) Previous discussion in the bi-weekly clippy meeting for reference: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Meeting.202021-02-23/near/227458019 Move the `upper_case_acronyms` lint back to the style group. Only warn on fully-capitalized names by default. Add add a clippy-config option `upper-case-acronyms-aggressive: true/false` to enabled more aggressive linting on all substrings that could be capitalized acronyms. --- changelog: reenable upper_case_acronyms by default but make the more aggressive linting opt-in via config option
2 parents 76a689d + 2a6b061 commit ef41f2b

File tree

9 files changed

+139
-35
lines changed

9 files changed

+139
-35
lines changed

clippy_lints/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12161216
let enum_variant_name_threshold = conf.enum_variant_name_threshold;
12171217
store.register_early_pass(move || box enum_variants::EnumVariantNames::new(enum_variant_name_threshold));
12181218
store.register_early_pass(|| box tabs_in_doc_comments::TabsInDocComments);
1219-
store.register_early_pass(|| box upper_case_acronyms::UpperCaseAcronyms);
1219+
let upper_case_acronyms_aggressive = conf.upper_case_acronyms_aggressive;
1220+
store.register_early_pass(move || box upper_case_acronyms::UpperCaseAcronyms::new(upper_case_acronyms_aggressive));
12201221
store.register_late_pass(|| box default::Default::default());
12211222
store.register_late_pass(|| box unused_self::UnusedSelf);
12221223
store.register_late_pass(|| box mutable_debug_assertion::DebugAssertWithMutCall);
@@ -1416,7 +1417,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14161417
LintId::of(&unnecessary_wraps::UNNECESSARY_WRAPS),
14171418
LintId::of(&unnested_or_patterns::UNNESTED_OR_PATTERNS),
14181419
LintId::of(&unused_self::UNUSED_SELF),
1419-
LintId::of(&upper_case_acronyms::UPPER_CASE_ACRONYMS),
14201420
LintId::of(&wildcard_imports::ENUM_GLOB_USE),
14211421
LintId::of(&wildcard_imports::WILDCARD_IMPORTS),
14221422
LintId::of(&zero_sized_map_values::ZERO_SIZED_MAP_VALUES),
@@ -1716,6 +1716,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
17161716
LintId::of(&unused_unit::UNUSED_UNIT),
17171717
LintId::of(&unwrap::PANICKING_UNWRAP),
17181718
LintId::of(&unwrap::UNNECESSARY_UNWRAP),
1719+
LintId::of(&upper_case_acronyms::UPPER_CASE_ACRONYMS),
17191720
LintId::of(&useless_conversion::USELESS_CONVERSION),
17201721
LintId::of(&vec::USELESS_VEC),
17211722
LintId::of(&vec_init_then_push::VEC_INIT_THEN_PUSH),
@@ -1835,6 +1836,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18351836
LintId::of(&types::FN_TO_NUMERIC_CAST_WITH_TRUNCATION),
18361837
LintId::of(&unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME),
18371838
LintId::of(&unused_unit::UNUSED_UNIT),
1839+
LintId::of(&upper_case_acronyms::UPPER_CASE_ACRONYMS),
18381840
LintId::of(&write::PRINTLN_EMPTY_STRING),
18391841
LintId::of(&write::PRINT_LITERAL),
18401842
LintId::of(&write::PRINT_WITH_NEWLINE),

clippy_lints/src/upper_case_acronyms.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ use rustc_ast::ast::{Item, ItemKind, Variant};
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
77
use rustc_middle::lint::in_external_macro;
8-
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::symbol::Ident;
1010

1111
declare_clippy_lint! {
12-
/// **What it does:** Checks for camel case name containing a capitalized acronym.
12+
/// **What it does:** Checks for fully capitalized names and optionally names containing a capitalized acronym.
1313
///
1414
/// **Why is this bad?** In CamelCase, acronyms count as one word.
1515
/// See [naming conventions](https://rust-lang.github.io/api-guidelines/naming.html#casing-conforms-to-rfc-430-c-case)
1616
/// for more.
1717
///
18+
/// By default, the lint only triggers on fully-capitalized names.
19+
/// You can use the `upper-case-acronyms-aggressive: true` config option to enable linting
20+
/// on all camel case names
21+
///
1822
/// **Known problems:** When two acronyms are contiguous, the lint can't tell where
1923
/// the first acronym ends and the second starts, so it suggests to lowercase all of
2024
/// the letters in the second acronym.
@@ -29,11 +33,24 @@ declare_clippy_lint! {
2933
/// struct HttpResponse;
3034
/// ```
3135
pub UPPER_CASE_ACRONYMS,
32-
pedantic,
36+
style,
3337
"capitalized acronyms are against the naming convention"
3438
}
3539

36-
declare_lint_pass!(UpperCaseAcronyms => [UPPER_CASE_ACRONYMS]);
40+
#[derive(Default)]
41+
pub struct UpperCaseAcronyms {
42+
upper_case_acronyms_aggressive: bool,
43+
}
44+
45+
impl UpperCaseAcronyms {
46+
pub fn new(aggressive: bool) -> Self {
47+
Self {
48+
upper_case_acronyms_aggressive: aggressive,
49+
}
50+
}
51+
}
52+
53+
impl_lint_pass!(UpperCaseAcronyms => [UPPER_CASE_ACRONYMS]);
3754

3855
fn correct_ident(ident: &str) -> String {
3956
let ident = ident.chars().rev().collect::<String>();
@@ -56,11 +73,18 @@ fn correct_ident(ident: &str) -> String {
5673
ident
5774
}
5875

59-
fn check_ident(cx: &EarlyContext<'_>, ident: &Ident) {
76+
fn check_ident(cx: &EarlyContext<'_>, ident: &Ident, be_aggressive: bool) {
6077
let span = ident.span;
6178
let ident = &ident.as_str();
6279
let corrected = correct_ident(ident);
63-
if ident != &corrected {
80+
// warn if we have pure-uppercase idents
81+
// assume that two-letter words are some kind of valid abbreviation like FP for false positive
82+
// (and don't warn)
83+
if (ident.chars().all(|c| c.is_ascii_uppercase()) && ident.len() > 2)
84+
// otherwise, warn if we have SOmeTHING lIKE THIs but only warn with the aggressive
85+
// upper-case-acronyms-aggressive config option enabled
86+
|| (be_aggressive && ident != &corrected)
87+
{
6488
span_lint_and_sugg(
6589
cx,
6690
UPPER_CASE_ACRONYMS,
@@ -82,12 +106,12 @@ impl EarlyLintPass for UpperCaseAcronyms {
82106
ItemKind::TyAlias(..) | ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Trait(..)
83107
);
84108
then {
85-
check_ident(cx, &it.ident);
109+
check_ident(cx, &it.ident, self.upper_case_acronyms_aggressive);
86110
}
87111
}
88112
}
89113

90114
fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &Variant) {
91-
check_ident(cx, &v.ident);
115+
check_ident(cx, &v.ident, self.upper_case_acronyms_aggressive);
92116
}
93117
}

clippy_lints/src/utils/conf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ define_Conf! {
173173
(disallowed_methods, "disallowed_methods": Vec<String>, Vec::<String>::new()),
174174
/// Lint: UNREADABLE_LITERAL. Should the fraction of a decimal be linted to include separators.
175175
(unreadable_literal_lint_fractions, "unreadable_literal_lint_fractions": bool, true),
176+
/// Lint: UPPER_CASE_ACRONYMS. Enables verbose mode. Triggers if there is more than one uppercase char next to each other
177+
(upper_case_acronyms_aggressive, "upper_case_acronyms_aggressive": bool, false),
176178
/// Lint: _CARGO_COMMON_METADATA. For internal testing only, ignores the current `publish` settings in the Cargo manifest.
177179
(cargo_ignore_publish, "cargo_ignore_publish": bool, false),
178180
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `unreadable-literal-lint-fractions`, `cargo-ignore-publish`, `third-party` at line 5 column 1
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `third-party` at line 5 column 1
22

33
error: aborting due to previous error
44

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
upper-case-acronyms-aggressive = true
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![warn(clippy::upper_case_acronyms)]
2+
3+
struct HTTPResponse; // not linted by default, but with cfg option
4+
5+
struct CString; // not linted
6+
7+
enum Flags {
8+
NS, // not linted
9+
CWR,
10+
ECE,
11+
URG,
12+
ACK,
13+
PSH,
14+
RST,
15+
SYN,
16+
FIN,
17+
}
18+
19+
struct GCCLLVMSomething; // linted with cfg option, beware that lint suggests `GccllvmSomething` instead of
20+
// `GccLlvmSomething`
21+
22+
fn main() {}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
error: name `HTTPResponse` contains a capitalized acronym
2+
--> $DIR/upper_case_acronyms.rs:3:8
3+
|
4+
LL | struct HTTPResponse; // not linted by default, but with cfg option
5+
| ^^^^^^^^^^^^ help: consider making the acronym lowercase, except the initial letter: `HttpResponse`
6+
|
7+
= note: `-D clippy::upper-case-acronyms` implied by `-D warnings`
8+
9+
error: name `NS` contains a capitalized acronym
10+
--> $DIR/upper_case_acronyms.rs:8:5
11+
|
12+
LL | NS, // not linted
13+
| ^^ help: consider making the acronym lowercase, except the initial letter (notice the capitalization): `Ns`
14+
15+
error: name `CWR` contains a capitalized acronym
16+
--> $DIR/upper_case_acronyms.rs:9:5
17+
|
18+
LL | CWR,
19+
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Cwr`
20+
21+
error: name `ECE` contains a capitalized acronym
22+
--> $DIR/upper_case_acronyms.rs:10:5
23+
|
24+
LL | ECE,
25+
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Ece`
26+
27+
error: name `URG` contains a capitalized acronym
28+
--> $DIR/upper_case_acronyms.rs:11:5
29+
|
30+
LL | URG,
31+
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Urg`
32+
33+
error: name `ACK` contains a capitalized acronym
34+
--> $DIR/upper_case_acronyms.rs:12:5
35+
|
36+
LL | ACK,
37+
| ^^^ help: consider making the acronym lowercase, except the initial letter (notice the capitalization): `Ack`
38+
39+
error: name `PSH` contains a capitalized acronym
40+
--> $DIR/upper_case_acronyms.rs:13:5
41+
|
42+
LL | PSH,
43+
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Psh`
44+
45+
error: name `RST` contains a capitalized acronym
46+
--> $DIR/upper_case_acronyms.rs:14:5
47+
|
48+
LL | RST,
49+
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Rst`
50+
51+
error: name `SYN` contains a capitalized acronym
52+
--> $DIR/upper_case_acronyms.rs:15:5
53+
|
54+
LL | SYN,
55+
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Syn`
56+
57+
error: name `FIN` contains a capitalized acronym
58+
--> $DIR/upper_case_acronyms.rs:16:5
59+
|
60+
LL | FIN,
61+
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Fin`
62+
63+
error: name `GCCLLVMSomething` contains a capitalized acronym
64+
--> $DIR/upper_case_acronyms.rs:19:8
65+
|
66+
LL | struct GCCLLVMSomething; // linted with cfg option, beware that lint suggests `GccllvmSomething` instead of
67+
| ^^^^^^^^^^^^^^^^ help: consider making the acronym lowercase, except the initial letter: `GccllvmSomething`
68+
69+
error: aborting due to 11 previous errors
70+

tests/ui/upper_case_acronyms.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![warn(clippy::upper_case_acronyms)]
22

3-
struct HTTPResponse; // linted
3+
struct HTTPResponse; // not linted by default, but with cfg option
44

55
struct CString; // not linted
66

77
enum Flags {
8-
NS, // linted
8+
NS, // not linted
99
CWR,
1010
ECE,
1111
URG,
@@ -16,6 +16,7 @@ enum Flags {
1616
FIN,
1717
}
1818

19-
struct GCCLLVMSomething; // linted, beware that lint suggests `GccllvmSomething` instead of `GccLlvmSomething`
19+
struct GCCLLVMSomething; // linted with cfg option, beware that lint suggests `GccllvmSomething` instead of
20+
// `GccLlvmSomething`
2021

2122
fn main() {}

tests/ui/upper_case_acronyms.stderr

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
error: name `HTTPResponse` contains a capitalized acronym
2-
--> $DIR/upper_case_acronyms.rs:3:8
3-
|
4-
LL | struct HTTPResponse; // linted
5-
| ^^^^^^^^^^^^ help: consider making the acronym lowercase, except the initial letter: `HttpResponse`
6-
|
7-
= note: `-D clippy::upper-case-acronyms` implied by `-D warnings`
8-
9-
error: name `NS` contains a capitalized acronym
10-
--> $DIR/upper_case_acronyms.rs:8:5
11-
|
12-
LL | NS, // linted
13-
| ^^ help: consider making the acronym lowercase, except the initial letter (notice the capitalization): `Ns`
14-
151
error: name `CWR` contains a capitalized acronym
162
--> $DIR/upper_case_acronyms.rs:9:5
173
|
184
LL | CWR,
195
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Cwr`
6+
|
7+
= note: `-D clippy::upper-case-acronyms` implied by `-D warnings`
208

219
error: name `ECE` contains a capitalized acronym
2210
--> $DIR/upper_case_acronyms.rs:10:5
@@ -60,11 +48,5 @@ error: name `FIN` contains a capitalized acronym
6048
LL | FIN,
6149
| ^^^ help: consider making the acronym lowercase, except the initial letter: `Fin`
6250

63-
error: name `GCCLLVMSomething` contains a capitalized acronym
64-
--> $DIR/upper_case_acronyms.rs:19:8
65-
|
66-
LL | struct GCCLLVMSomething; // linted, beware that lint suggests `GccllvmSomething` instead of `GccLlvmSomething`
67-
| ^^^^^^^^^^^^^^^^ help: consider making the acronym lowercase, except the initial letter: `GccllvmSomething`
68-
69-
error: aborting due to 11 previous errors
51+
error: aborting due to 8 previous errors
7052

0 commit comments

Comments
 (0)