@@ -6,6 +6,8 @@ use rustc_lint::LateContext;
66use rustc_middle:: ty;
77use rustc_span:: Span ;
88
9+ use std:: iter;
10+
911use super :: MISNAMED_GETTERS ;
1012
1113pub fn check_fn (
@@ -75,39 +77,35 @@ pub fn check_fn(
7577 }
7678 } ;
7779
78- let ty = cx. typeck_results ( ) . expr_ty_adjusted ( self_data) ;
79-
80- let def = {
81- let mut kind = ty. kind ( ) ;
82- loop {
83- match kind {
84- ty:: Adt ( def, _) => break def,
85- ty:: Ref ( _, ty, _) => kind = ty. kind ( ) ,
86- // We don't do tuples because the function name cannot be a number
87- _ => return ,
88- }
89- }
90- } ;
91-
9280 let mut used_field = None ;
9381 let mut correct_field = None ;
94- for f in def. all_fields ( ) {
95- if f. name . as_str ( ) == name {
96- correct_field = Some ( f) ;
97- }
98- if f. name == used_ident. name {
99- used_field = Some ( f) ;
82+ let typeck_results = cx. typeck_results ( ) ;
83+ for adjusted_type in iter:: once ( typeck_results. expr_ty ( self_data) )
84+ . chain ( typeck_results. expr_adjustments ( self_data) . iter ( ) . map ( |adj| adj. target ) )
85+ {
86+ let ty:: Adt ( def, _) = adjusted_type. kind ( ) else {
87+ continue ;
88+ } ;
89+
90+ for f in def. all_fields ( ) {
91+ if f. name . as_str ( ) == name {
92+ correct_field = Some ( f) ;
93+ }
94+ if f. name == used_ident. name {
95+ used_field = Some ( f) ;
96+ }
10097 }
10198 }
10299
103100 let Some ( used_field) = used_field else {
104- // FIXME: This can be reached if the field access uses autoderef.
105- // `dec.all_fields()` should be replaced by something that uses autoderef on the unajusted type of `self_data`
101+ // Can happen if the field access is a tuple. We don't lint those because the getter name could not start with a number.
106102 return ;
107103 } ;
108104
109105 let Some ( correct_field) = correct_field else {
110- return ;
106+ // There is no field corresponding to the getter name.
107+ // FIXME: This can be a false positive if the correct field is reachable trought deeper autodereferences than used_field is
108+ return ;
111109 } ;
112110
113111 if cx. tcx . type_of ( used_field. did ) == cx. tcx . type_of ( correct_field. did ) {
0 commit comments