1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
2
use itertools:: Itertools ;
3
- use rustc_ast:: ast:: { Item , ItemKind , VisibilityKind } ;
4
3
use rustc_errors:: Applicability ;
5
- use rustc_lint:: { EarlyContext , EarlyLintPass , LintContext } ;
4
+ use rustc_hir:: { Item , ItemKind } ;
5
+ use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
6
6
use rustc_middle:: lint:: in_external_macro;
7
7
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
8
8
use rustc_span:: symbol:: Ident ;
@@ -38,12 +38,14 @@ declare_clippy_lint! {
38
38
39
39
#[ derive( Default ) ]
40
40
pub struct UpperCaseAcronyms {
41
+ avoid_breaking_exported_api : bool ,
41
42
upper_case_acronyms_aggressive : bool ,
42
43
}
43
44
44
45
impl UpperCaseAcronyms {
45
- pub fn new ( aggressive : bool ) -> Self {
46
+ pub fn new ( avoid_breaking_exported_api : bool , aggressive : bool ) -> Self {
46
47
Self {
48
+ avoid_breaking_exported_api,
47
49
upper_case_acronyms_aggressive : aggressive,
48
50
}
49
51
}
@@ -72,7 +74,7 @@ fn correct_ident(ident: &str) -> String {
72
74
ident
73
75
}
74
76
75
- fn check_ident ( cx : & EarlyContext < ' _ > , ident : & Ident , be_aggressive : bool ) {
77
+ fn check_ident ( cx : & LateContext < ' _ > , ident : & Ident , be_aggressive : bool ) {
76
78
let span = ident. span ;
77
79
let ident = & ident. as_str ( ) ;
78
80
let corrected = correct_ident ( ident) ;
@@ -96,23 +98,27 @@ fn check_ident(cx: &EarlyContext<'_>, ident: &Ident, be_aggressive: bool) {
96
98
}
97
99
}
98
100
99
- impl EarlyLintPass for UpperCaseAcronyms {
100
- fn check_item ( & mut self , cx : & EarlyContext < ' _ > , it : & Item ) {
101
+ impl LateLintPass < ' _ > for UpperCaseAcronyms {
102
+ fn check_item ( & mut self , cx : & LateContext < ' _ > , it : & Item < ' _ > ) {
101
103
// do not lint public items or in macros
102
- if !in_external_macro ( cx. sess ( ) , it. span ) && !matches ! ( it. vis. kind, VisibilityKind :: Public ) {
103
- if matches ! (
104
- it. kind,
105
- ItemKind :: TyAlias ( ..) | ItemKind :: Struct ( ..) | ItemKind :: Trait ( ..)
106
- ) {
104
+ if in_external_macro ( cx. sess ( ) , it. span )
105
+ || ( self . avoid_breaking_exported_api && cx. access_levels . is_exported ( it. hir_id ( ) ) )
106
+ {
107
+ return ;
108
+ }
109
+ match it. kind {
110
+ ItemKind :: TyAlias ( ..) | ItemKind :: Struct ( ..) | ItemKind :: Trait ( ..) => {
107
111
check_ident ( cx, & it. ident , self . upper_case_acronyms_aggressive ) ;
108
- } else if let ItemKind :: Enum ( ref enumdef, _) = it. kind {
112
+ } ,
113
+ ItemKind :: Enum ( ref enumdef, _) => {
109
114
// check enum variants seperately because again we only want to lint on private enums and
110
115
// the fn check_variant does not know about the vis of the enum of its variants
111
116
enumdef
112
117
. variants
113
118
. iter ( )
114
119
. for_each ( |variant| check_ident ( cx, & variant. ident , self . upper_case_acronyms_aggressive ) ) ;
115
- }
120
+ } ,
121
+ _ => { } ,
116
122
}
117
123
}
118
124
}
0 commit comments