11use clippy_utils:: diagnostics:: span_lint_and_sugg;
22use itertools:: Itertools ;
3- use rustc_ast:: ast:: { Item , ItemKind , VisibilityKind } ;
43use rustc_errors:: Applicability ;
5- use rustc_lint:: { EarlyContext , EarlyLintPass , LintContext } ;
4+ use rustc_hir:: { Item , ItemKind } ;
5+ use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
66use rustc_middle:: lint:: in_external_macro;
77use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
88use rustc_span:: symbol:: Ident ;
@@ -38,12 +38,14 @@ declare_clippy_lint! {
3838
3939#[ derive( Default ) ]
4040pub struct UpperCaseAcronyms {
41+ avoid_breaking_exported_api : bool ,
4142 upper_case_acronyms_aggressive : bool ,
4243}
4344
4445impl UpperCaseAcronyms {
45- pub fn new ( aggressive : bool ) -> Self {
46+ pub fn new ( avoid_breaking_exported_api : bool , aggressive : bool ) -> Self {
4647 Self {
48+ avoid_breaking_exported_api,
4749 upper_case_acronyms_aggressive : aggressive,
4850 }
4951 }
@@ -72,7 +74,7 @@ fn correct_ident(ident: &str) -> String {
7274 ident
7375}
7476
75- fn check_ident ( cx : & EarlyContext < ' _ > , ident : & Ident , be_aggressive : bool ) {
77+ fn check_ident ( cx : & LateContext < ' _ > , ident : & Ident , be_aggressive : bool ) {
7678 let span = ident. span ;
7779 let ident = & ident. as_str ( ) ;
7880 let corrected = correct_ident ( ident) ;
@@ -96,23 +98,27 @@ fn check_ident(cx: &EarlyContext<'_>, ident: &Ident, be_aggressive: bool) {
9698 }
9799}
98100
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 < ' _ > ) {
101103 // 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 ( ..) => {
107111 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, _) => {
109114 // check enum variants seperately because again we only want to lint on private enums and
110115 // the fn check_variant does not know about the vis of the enum of its variants
111116 enumdef
112117 . variants
113118 . iter ( )
114119 . for_each ( |variant| check_ident ( cx, & variant. ident , self . upper_case_acronyms_aggressive ) ) ;
115- }
120+ } ,
121+ _ => { } ,
116122 }
117123 }
118124}
0 commit comments