@@ -4,14 +4,17 @@ use rustc_data_structures::fx::FxHashMap;
44use rustc_errors:: MultiSpan ;
55use rustc_hir:: intravisit:: { walk_impl_item, walk_item, walk_param_bound, walk_ty, Visitor } ;
66use rustc_hir:: {
7- BodyId , ExprKind , GenericParamKind , Generics , ImplItem , ImplItemKind , Item , ItemKind , PredicateOrigin , Ty , TyKind ,
8- WherePredicate ,
7+ BodyId , ExprKind , GenericBound , GenericParamKind , Generics , ImplItem , ImplItemKind , Item , ItemKind ,
8+ PredicateOrigin , Ty , TyKind , WherePredicate ,
99} ;
1010use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
1111use rustc_middle:: hir:: nested_filter;
1212use rustc_middle:: lint:: in_external_macro;
13- use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
14- use rustc_span:: { def_id:: DefId , Span } ;
13+ use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
14+ use rustc_span:: {
15+ def_id:: { DefId , LocalDefId } ,
16+ Span ,
17+ } ;
1518
1619declare_clippy_lint ! {
1720 /// ### What it does
@@ -38,7 +41,29 @@ declare_clippy_lint! {
3841 complexity,
3942 "unused type parameters in function definitions"
4043}
41- declare_lint_pass ! ( ExtraUnusedTypeParameters => [ EXTRA_UNUSED_TYPE_PARAMETERS ] ) ;
44+
45+ pub struct ExtraUnusedTypeParameters {
46+ avoid_breaking_exported_api : bool ,
47+ }
48+
49+ impl ExtraUnusedTypeParameters {
50+ pub fn new ( avoid_breaking_exported_api : bool ) -> Self {
51+ Self {
52+ avoid_breaking_exported_api,
53+ }
54+ }
55+
56+ /// Don't lint external macros or functions with empty bodies. Also, don't lint public items if
57+ /// the `avoid_breaking_exported_api` config option is set.
58+ fn check_false_positive ( & self , cx : & LateContext < ' _ > , span : Span , def_id : LocalDefId , body_id : BodyId ) -> bool {
59+ let body = cx. tcx . hir ( ) . body ( body_id) . value ;
60+ let fn_empty = matches ! ( & body. kind, ExprKind :: Block ( blk, None ) if blk. stmts. is_empty( ) && blk. expr. is_none( ) ) ;
61+ let is_exported = cx. effective_visibilities . is_exported ( def_id) ;
62+ in_external_macro ( cx. sess ( ) , span) || ( self . avoid_breaking_exported_api && is_exported) || fn_empty
63+ }
64+ }
65+
66+ impl_lint_pass ! ( ExtraUnusedTypeParameters => [ EXTRA_UNUSED_TYPE_PARAMETERS ] ) ;
4267
4368/// A visitor struct that walks a given function and gathers generic type parameters, plus any
4469/// trait bounds those parameters have.
@@ -56,13 +81,10 @@ struct TypeWalker<'cx, 'tcx> {
5681 /// Otherwise, if any type parameters end up being used, or if any lifetime or const-generic
5782 /// parameters are present, this will be set to `false`.
5883 all_params_unused : bool ,
59- /// Whether or not the function has an empty body, in which case any bounded type parameters
60- /// will not be linted.
61- fn_body_empty : bool ,
6284}
6385
6486impl < ' cx , ' tcx > TypeWalker < ' cx , ' tcx > {
65- fn new ( cx : & ' cx LateContext < ' tcx > , generics : & ' tcx Generics < ' tcx > , body_id : BodyId ) -> Self {
87+ fn new ( cx : & ' cx LateContext < ' tcx > , generics : & ' tcx Generics < ' tcx > ) -> Self {
6688 let mut all_params_unused = true ;
6789 let ty_params = generics
6890 . params
@@ -79,17 +101,18 @@ impl<'cx, 'tcx> TypeWalker<'cx, 'tcx> {
79101 } )
80102 . collect ( ) ;
81103
82- let body = cx. tcx . hir ( ) . body ( body_id) . value ;
83- let fn_body_empty =
84- matches ! ( & body. kind, ExprKind :: Block ( block, None ) if block. stmts. is_empty( ) && block. expr. is_none( ) ) ;
85-
86104 Self {
87105 cx,
88106 ty_params,
89107 bounds : FxHashMap :: default ( ) ,
90108 generics,
91109 all_params_unused,
92- fn_body_empty,
110+ }
111+ }
112+
113+ fn mark_param_used ( & mut self , def_id : DefId ) {
114+ if self . ty_params . remove ( & def_id) . is_some ( ) {
115+ self . all_params_unused = false ;
93116 }
94117 }
95118
@@ -128,14 +151,18 @@ impl<'cx, 'tcx> TypeWalker<'cx, 'tcx> {
128151 }
129152}
130153
154+ /// Given a generic bound, if the bound is for a trait that's not a `LangItem`, return the
155+ /// `LocalDefId` for that trait.
156+ fn bound_to_trait_def_id ( bound : & GenericBound < ' _ > ) -> Option < LocalDefId > {
157+ bound. trait_ref ( ) ?. trait_def_id ( ) ?. as_local ( )
158+ }
159+
131160impl < ' cx , ' tcx > Visitor < ' tcx > for TypeWalker < ' cx , ' tcx > {
132161 type NestedFilter = nested_filter:: OnlyBodies ;
133162
134163 fn visit_ty ( & mut self , t : & ' tcx Ty < ' tcx > ) {
135164 if let Some ( ( def_id, _) ) = t. peel_refs ( ) . as_generic_param ( ) {
136- if self . ty_params . remove ( & def_id) . is_some ( ) {
137- self . all_params_unused = false ;
138- }
165+ self . mark_param_used ( def_id) ;
139166 } else if let TyKind :: OpaqueDef ( id, _, _) = t. kind {
140167 // Explicitly walk OpaqueDef. Normally `walk_ty` would do the job, but it calls
141168 // `visit_nested_item`, which checks that `Self::NestedFilter::INTER` is set. We're
@@ -151,12 +178,16 @@ impl<'cx, 'tcx> Visitor<'tcx> for TypeWalker<'cx, 'tcx> {
151178 if let WherePredicate :: BoundPredicate ( predicate) = predicate {
152179 // Collect spans for any bounds on type parameters. We only keep bounds that appear in
153180 // the list of generics (not in a where-clause).
154- //
155- // Also, if the function body is empty, we don't lint the corresponding type parameters
156- // (See https://github.com/rust-lang/rust-clippy/issues/10319).
157181 if let Some ( ( def_id, _) ) = predicate. bounded_ty . peel_refs ( ) . as_generic_param ( ) {
158- if self . fn_body_empty {
159- self . ty_params . remove ( & def_id) ;
182+ // If the bound contains non-public traits, err on the safe side and don't lint the
183+ // corresponding parameter.
184+ if !predicate
185+ . bounds
186+ . iter ( )
187+ . filter_map ( bound_to_trait_def_id)
188+ . all ( |id| self . cx . effective_visibilities . is_exported ( id) )
189+ {
190+ self . mark_param_used ( def_id) ;
160191 } else if let PredicateOrigin :: GenericParam = predicate. origin {
161192 self . bounds . insert ( def_id, predicate. span ) ;
162193 }
@@ -176,9 +207,9 @@ impl<'cx, 'tcx> Visitor<'tcx> for TypeWalker<'cx, 'tcx> {
176207impl < ' tcx > LateLintPass < ' tcx > for ExtraUnusedTypeParameters {
177208 fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
178209 if let ItemKind :: Fn ( _, generics, body_id) = item. kind
179- && !in_external_macro ( cx . sess ( ) , item. span )
210+ && !self . check_false_positive ( cx , item. span , item . owner_id . def_id , body_id )
180211 {
181- let mut walker = TypeWalker :: new ( cx, generics, body_id ) ;
212+ let mut walker = TypeWalker :: new ( cx, generics) ;
182213 walk_item ( & mut walker, item) ;
183214 walker. emit_lint ( ) ;
184215 }
@@ -188,9 +219,9 @@ impl<'tcx> LateLintPass<'tcx> for ExtraUnusedTypeParameters {
188219 // Only lint on inherent methods, not trait methods.
189220 if let ImplItemKind :: Fn ( .., body_id) = item. kind
190221 && trait_ref_of_method ( cx, item. owner_id . def_id ) . is_none ( )
191- && !in_external_macro ( cx . sess ( ) , item. span )
222+ && !self . check_false_positive ( cx , item. span , item . owner_id . def_id , body_id )
192223 {
193- let mut walker = TypeWalker :: new ( cx, item. generics , body_id ) ;
224+ let mut walker = TypeWalker :: new ( cx, item. generics ) ;
194225 walk_impl_item ( & mut walker, item) ;
195226 walker. emit_lint ( ) ;
196227 }
0 commit comments