@@ -6,11 +6,11 @@ use rustc_ast::ast::{LitFloatType, LitKind};
66use rustc_data_structures:: fx:: FxHashMap ;
77use rustc_hir:: def_id:: DefId ;
88use rustc_hir:: {
9- self as hir, BindingMode , CaptureBy , Closure , ClosureKind , ConstArg , ConstArgKind , CoroutineKind , ExprKind ,
9+ self as hir, BindingMode , Body , CaptureBy , Closure , ClosureKind , ConstArg , ConstArgKind , CoroutineKind , ExprKind ,
1010 FnRetTy , HirId , Lit , PatExprKind , PatKind , QPath , StmtKind , StructTailExpr ,
1111} ;
1212use rustc_lint:: { LateContext , LateLintPass } ;
13- use rustc_middle:: ty:: { FloatTy , IntTy , UintTy } ;
13+ use rustc_middle:: ty:: { FloatTy , IntTy , TypeckResults , UintTy } ;
1414use rustc_session:: declare_lint_pass;
1515use rustc_span:: symbol:: { Ident , Symbol } ;
1616use std:: cell:: Cell ;
@@ -137,15 +137,31 @@ impl<'tcx> LateLintPass<'tcx> for Author {
137137
138138fn check_item ( cx : & LateContext < ' _ > , hir_id : HirId ) {
139139 if let Some ( body) = cx. tcx . hir_maybe_body_owned_by ( hir_id. expect_owner ( ) . def_id ) {
140- check_node ( cx, hir_id, |v| {
141- v. expr ( & v. bind ( "expr" , body. value ) ) ;
142- } ) ;
140+ check_node_with_body (
141+ cx,
142+ hir_id,
143+ |v| {
144+ v. expr ( & v. bind ( "expr" , body. value ) ) ;
145+ } ,
146+ Some ( body) ,
147+ ) ;
143148 }
144149}
145150
146151fn check_node ( cx : & LateContext < ' _ > , hir_id : HirId , f : impl Fn ( & PrintVisitor < ' _ , ' _ > ) ) {
152+ check_node_with_body ( cx, hir_id, f, None ) ;
153+ }
154+
155+ /// Check the node at `hir_id`, in the context of `body` or the default from `cx` if none is given.
156+ fn check_node_with_body (
157+ cx : & LateContext < ' _ > ,
158+ hir_id : HirId ,
159+ f : impl Fn ( & PrintVisitor < ' _ , ' _ > ) ,
160+ body : Option < & Body < ' _ > > ,
161+ ) {
147162 if has_attr ( cx, hir_id) {
148- f ( & PrintVisitor :: new ( cx) ) ;
163+ let typeck_results = body. map_or_else ( || cx. typeck_results ( ) , |body| cx. tcx . typeck_body ( body. id ( ) ) ) ;
164+ f ( & PrintVisitor :: new ( cx, typeck_results) ) ;
149165 println ! ( "{{" ) ;
150166 println ! ( " // report your lint here" ) ;
151167 println ! ( "}}" ) ;
@@ -199,6 +215,7 @@ impl<T: Display> Display for OptionPat<T> {
199215
200216struct PrintVisitor < ' a , ' tcx > {
201217 cx : & ' a LateContext < ' tcx > ,
218+ typeck_results : & ' tcx TypeckResults < ' tcx > ,
202219 /// Fields are the current index that needs to be appended to pattern
203220 /// binding names
204221 ids : Cell < FxHashMap < & ' static str , u32 > > ,
@@ -207,9 +224,10 @@ struct PrintVisitor<'a, 'tcx> {
207224}
208225
209226impl < ' a , ' tcx > PrintVisitor < ' a , ' tcx > {
210- fn new ( cx : & ' a LateContext < ' tcx > ) -> Self {
227+ fn new ( cx : & ' a LateContext < ' tcx > , typeck_results : & ' tcx TypeckResults < ' tcx > ) -> Self {
211228 Self {
212229 cx,
230+ typeck_results,
213231 ids : Cell :: default ( ) ,
214232 first : Cell :: new ( true ) ,
215233 }
@@ -291,7 +309,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
291309 }
292310
293311 fn maybe_path < ' p > ( & self , path : & Binding < impl MaybeQPath < ' p > > ) {
294- if let Some ( id) = path. value . res ( self . cx ) . opt_def_id ( )
312+ if let Some ( id) = path. value . res ( self . typeck_results ) . opt_def_id ( )
295313 && !id. is_local ( )
296314 {
297315 if let Some ( lang) = self . cx . tcx . lang_items ( ) . from_def_id ( id) {
0 commit comments