@@ -12,11 +12,11 @@ use rustc_lint::{LateContext, LateLintPass};
12
12
use rustc_middle:: hir:: map:: associated_body;
13
13
use rustc_middle:: hir:: nested_filter:: OnlyBodies ;
14
14
use rustc_middle:: mir:: FakeReadCause ;
15
- use rustc_middle:: ty:: { self , Ty , UpvarId , UpvarPath } ;
15
+ use rustc_middle:: ty:: { self , Ty , TyCtxt , UpvarId , UpvarPath } ;
16
16
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
17
- use rustc_span:: def_id:: LocalDefId ;
17
+ use rustc_span:: def_id:: { LocalDefId , CRATE_DEF_ID } ;
18
18
use rustc_span:: symbol:: kw;
19
- use rustc_span:: Span ;
19
+ use rustc_span:: { sym , Span } ;
20
20
use rustc_target:: spec:: abi:: Abi ;
21
21
22
22
declare_clippy_lint ! {
@@ -93,6 +93,16 @@ fn should_skip<'tcx>(
93
93
is_from_proc_macro ( cx, & input)
94
94
}
95
95
96
+ fn inherits_cfg ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> bool {
97
+ if def_id == CRATE_DEF_ID {
98
+ false
99
+ } else if tcx. has_attr ( def_id, sym:: cfg) {
100
+ true
101
+ } else {
102
+ inherits_cfg ( tcx, tcx. parent_module_from_def_id ( def_id) )
103
+ }
104
+ }
105
+
96
106
impl < ' tcx > LateLintPass < ' tcx > for NeedlessPassByRefMut < ' tcx > {
97
107
fn check_fn (
98
108
& mut self ,
@@ -192,10 +202,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
192
202
let show_semver_warning =
193
203
self . avoid_breaking_exported_api && cx. effective_visibilities . is_exported ( * fn_def_id) ;
194
204
205
+ let mut is_cfged = None ;
195
206
for input in unused {
196
207
// If the argument is never used mutably, we emit the warning.
197
208
let sp = input. span ;
198
209
if let rustc_hir:: TyKind :: Ref ( _, inner_ty) = input. kind {
210
+ let is_cfged = is_cfged. get_or_insert_with ( || inherits_cfg ( cx. tcx , * fn_def_id) ) ;
199
211
span_lint_hir_and_then (
200
212
cx,
201
213
NEEDLESS_PASS_BY_REF_MUT ,
@@ -212,6 +224,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
212
224
if show_semver_warning {
213
225
diag. warn ( "changing this function will impact semver compatibility" ) ;
214
226
}
227
+ if * is_cfged {
228
+ diag. note ( "this is cfg-gated and may require further changes" ) ;
229
+ }
215
230
} ,
216
231
) ;
217
232
}
0 commit comments