@@ -164,10 +164,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
164
164
if let ( Res :: Local ( id_l) , Res :: Local ( id_r) ) = (
165
165
typeck_results. qpath_res ( qpath_l, lhs. hir_id ) ,
166
166
typeck_results. qpath_res ( qpath_r, rhs. hir_id ) ,
167
- ) {
168
- if id_l == id_r {
169
- return true ;
170
- }
167
+ ) && id_l == id_r
168
+ {
169
+ return true ;
171
170
}
172
171
return false ;
173
172
}
@@ -183,10 +182,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
183
182
}
184
183
}
185
184
186
- if let hir:: ExprKind :: Assign ( lhs, rhs, _) = assign. kind {
187
- if check_for_self_assign_helper ( self . typeck_results ( ) , lhs, rhs)
185
+ if let hir:: ExprKind :: Assign ( lhs, rhs, _) = assign. kind
186
+ && check_for_self_assign_helper ( self . typeck_results ( ) , lhs, rhs)
188
187
&& !assign. span . from_expansion ( )
189
- {
188
+ {
190
189
let is_field_assign = matches ! ( lhs. kind, hir:: ExprKind :: Field ( ..) ) ;
191
190
self . tcx . struct_span_lint_hir (
192
191
lint:: builtin:: DEAD_CODE ,
@@ -201,7 +200,6 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
201
200
. emit ( ) ;
202
201
} ,
203
202
)
204
- }
205
203
}
206
204
}
207
205
@@ -251,33 +249,33 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
251
249
return false ;
252
250
}
253
251
254
- if let Some ( trait_of) = self . tcx . trait_id_of_impl ( impl_of) {
255
- if self . tcx . has_attr ( trait_of, sym:: rustc_trivial_field_reads) {
256
- let trait_ref = self . tcx . impl_trait_ref ( impl_of) . unwrap ( ) ;
257
- if let ty:: Adt ( adt_def, _) = trait_ref. self_ty ( ) . kind ( ) {
258
- if let Some ( adt_def_id) = adt_def. did ( ) . as_local ( ) {
259
- self . ignored_derived_traits
260
- . entry ( adt_def_id)
261
- . or_default ( )
262
- . push ( ( trait_of, impl_of) ) ;
263
- }
264
- }
265
- return true ;
252
+ if let Some ( trait_of) = self . tcx . trait_id_of_impl ( impl_of)
253
+ && self . tcx . has_attr ( trait_of, sym:: rustc_trivial_field_reads)
254
+ {
255
+ let trait_ref = self . tcx . impl_trait_ref ( impl_of) . unwrap ( ) ;
256
+ if let ty:: Adt ( adt_def, _) = trait_ref. self_ty ( ) . kind ( )
257
+ && let Some ( adt_def_id) = adt_def. did ( ) . as_local ( )
258
+ {
259
+ self . ignored_derived_traits
260
+ . entry ( adt_def_id)
261
+ . or_default ( )
262
+ . push ( ( trait_of, impl_of) ) ;
266
263
}
264
+ return true ;
267
265
}
268
266
}
269
267
270
268
return false ;
271
269
}
272
270
273
271
fn visit_node ( & mut self , node : Node < ' tcx > ) {
274
- if let Some ( item_def_id) = match node {
275
- Node :: ImplItem ( hir:: ImplItem { def_id, .. } ) => Some ( def_id. to_def_id ( ) ) ,
276
- _ => None ,
277
- } {
278
- if self . should_ignore_item ( item_def_id) {
272
+ match node {
273
+ Node :: ImplItem ( hir:: ImplItem { def_id, .. } )
274
+ if self . should_ignore_item ( def_id. to_def_id ( ) ) =>
275
+ {
279
276
return ;
280
277
}
278
+ _ => ( ) ,
281
279
}
282
280
283
281
let had_repr_c = self . repr_has_repr_c ;
@@ -534,10 +532,10 @@ fn check_item<'tcx>(
534
532
}
535
533
DefKind :: Struct => {
536
534
let item = tcx. hir ( ) . item ( id) ;
537
- if let hir:: ItemKind :: Struct ( ref variant_data, _) = item. kind {
538
- if let Some ( ctor_hir_id) = variant_data. ctor_hir_id ( ) {
539
- struct_constructors . insert ( tcx . hir ( ) . local_def_id ( ctor_hir_id ) , item . def_id ) ;
540
- }
535
+ if let hir:: ItemKind :: Struct ( ref variant_data, _) = item. kind
536
+ && let Some ( ctor_hir_id) = variant_data. ctor_hir_id ( )
537
+ {
538
+ struct_constructors . insert ( tcx . hir ( ) . local_def_id ( ctor_hir_id ) , item . def_id ) ;
541
539
}
542
540
}
543
541
DefKind :: GlobalAsm => {
@@ -630,6 +628,7 @@ struct DeadVisitor<'tcx> {
630
628
tcx : TyCtxt < ' tcx > ,
631
629
live_symbols : & ' tcx FxHashSet < LocalDefId > ,
632
630
ignored_derived_traits : & ' tcx FxHashMap < LocalDefId , Vec < ( DefId , DefId ) > > ,
631
+ ignored_struct_def : FxHashSet < LocalDefId > ,
633
632
}
634
633
635
634
impl < ' tcx > DeadVisitor < ' tcx > {
@@ -677,10 +676,10 @@ impl<'tcx> DeadVisitor<'tcx> {
677
676
let inherent_impls = self . tcx . inherent_impls ( def_id) ;
678
677
for & impl_did in inherent_impls. iter ( ) {
679
678
for item_did in self . tcx . associated_item_def_ids ( impl_did) {
680
- if let Some ( def_id) = item_did. as_local ( ) {
681
- if self . live_symbols . contains ( & def_id) {
682
- return true ;
683
- }
679
+ if let Some ( def_id) = item_did. as_local ( )
680
+ && self . live_symbols . contains ( & def_id)
681
+ {
682
+ return true ;
684
683
}
685
684
}
686
685
}
@@ -698,9 +697,20 @@ impl<'tcx> DeadVisitor<'tcx> {
698
697
self . tcx . struct_span_lint_hir ( lint:: builtin:: DEAD_CODE , id, span, |lint| {
699
698
let def_id = self . tcx . hir ( ) . local_def_id ( id) ;
700
699
let descr = self . tcx . def_kind ( def_id) . descr ( def_id. to_def_id ( ) ) ;
701
- let mut err = lint. build ( & format ! ( "{} is never {}: `{}`" , descr , participle , name ) ) ;
700
+ let mut err = lint. build ( & format ! ( "{descr } is never {participle }: `{name }`" ) ) ;
702
701
let hir = self . tcx . hir ( ) ;
703
- if let Some ( encl_scope) = hir. get_enclosing_scope ( id)
702
+ let is_field_in_same_struct =
703
+ if let Some ( parent_hir_id) = self . tcx . hir ( ) . find_parent_node ( id)
704
+ && let Some ( parent_node) = self . tcx . hir ( ) . find ( parent_hir_id)
705
+ && let Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Struct ( ..) , ..} ) = parent_node
706
+ && let Some ( did) = self . tcx . hir ( ) . opt_local_def_id ( parent_hir_id)
707
+ {
708
+ !self . ignored_struct_def . insert ( did)
709
+ } else {
710
+ false
711
+ } ;
712
+ if !is_field_in_same_struct
713
+ && let Some ( encl_scope) = hir. get_enclosing_scope ( id)
704
714
&& let Some ( encl_def_id) = hir. opt_local_def_id ( encl_scope)
705
715
&& let Some ( ign_traits) = self . ignored_derived_traits . get ( & encl_def_id)
706
716
{
@@ -857,7 +867,12 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
857
867
858
868
fn check_mod_deathness ( tcx : TyCtxt < ' _ > , module : LocalDefId ) {
859
869
let ( live_symbols, ignored_derived_traits) = tcx. live_symbols_and_ignored_derived_traits ( ( ) ) ;
860
- let mut visitor = DeadVisitor { tcx, live_symbols, ignored_derived_traits } ;
870
+ let mut visitor = DeadVisitor {
871
+ tcx,
872
+ live_symbols,
873
+ ignored_derived_traits,
874
+ ignored_struct_def : FxHashSet :: default ( ) ,
875
+ } ;
861
876
let ( module, _, module_id) = tcx. hir ( ) . get_module ( module) ;
862
877
// Do not use an ItemLikeVisitor since we may want to skip visiting some items
863
878
// when a surrounding one is warned against or `_`.
0 commit comments