]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/unwrap.rs
rustup https://github.com/rust-lang/rust/pull/68944
[rust.git] / clippy_lints / src / unwrap.rs
index 7f127350ba92ac9c61a5798c55b42c3e2d945ddd..767b48d4a0963c125a0e0ef736e64a33f043fae3 100644 (file)
@@ -1,11 +1,11 @@
-use if_chain::if_chain;
-use rustc::declare_lint_pass;
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc_session::declare_tool_lint;
-
 use crate::utils::{higher::if_block, match_type, paths, span_lint_and_then, usage::is_potentially_mutated};
-use rustc::hir::intravisit::*;
-use rustc_hir::*;
+use if_chain::if_chain;
+use rustc::hir::map::Map;
+use rustc::lint::in_external_macro;
+use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor};
+use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Path, QPath, UnOp};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
 use rustc_span::source_map::Span;
 
 declare_clippy_lint! {
@@ -136,7 +136,13 @@ fn visit_branch(&mut self, cond: &'tcx Expr<'_>, branch: &'tcx Expr<'_>, else_br
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
+    type Map = Map<'tcx>;
+
     fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
+        // Shouldn't lint when `expr` is in macro.
+        if in_external_macro(self.cx.tcx.sess, expr.span) {
+            return;
+        }
         if let Some((cond, then, els)) = if_block(&expr) {
             walk_expr(self, cond);
             self.visit_branch(cond, then, false);
@@ -179,8 +185,8 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
         }
     }
 
-    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
-        NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir())
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
+        NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
     }
 }