]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_lint/src/late.rs
Rollup merge of #81333 - RalfJung:const-err-simplify, r=oli-obk
[rust.git] / compiler / rustc_lint / src / late.rs
index a6c04fb0b4c84a576bd8e576a7bc6fc19c4737f0..3821a393efb8b851c959a35f94135b4cf04421fa 100644 (file)
@@ -140,6 +140,8 @@ fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
     fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
         let generics = self.context.generics.take();
         self.context.generics = it.kind.generics();
+        let old_cached_typeck_results = self.context.cached_typeck_results.take();
+        let old_enclosing_body = self.context.enclosing_body.take();
         self.with_lint_attrs(it.hir_id, &it.attrs, |cx| {
             cx.with_param_env(it.hir_id, |cx| {
                 lint_callback!(cx, check_item, it);
@@ -147,6 +149,8 @@ fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
                 lint_callback!(cx, check_item_post, it);
             });
         });
+        self.context.enclosing_body = old_enclosing_body;
+        self.context.cached_typeck_results.set(old_cached_typeck_results);
         self.context.generics = generics;
     }
 
@@ -174,12 +178,13 @@ fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
     }
 
     fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
-        // statement attributes are actually just attributes on one of
-        // - item
-        // - local
-        // - expression
-        // so we keep track of lint levels there
-        lint_callback!(self, check_stmt, s);
+        let get_item = |id: hir::ItemId| self.context.tcx.hir().item(id.id);
+        let attrs = &s.kind.attrs(get_item);
+        // See `EarlyContextAndPass::visit_stmt` for an explanation
+        // of why we call `walk_stmt` outside of `with_lint_attrs`
+        self.with_lint_attrs(s.hir_id, attrs, |cx| {
+            lint_callback!(cx, check_stmt, s);
+        });
         hir_visit::walk_stmt(self, s);
     }