From 514b1bc2af3b841552e713cf431af506d2708f9d Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 4 Feb 2020 04:01:42 +0900 Subject: [PATCH] Do not lint `unnecessary_unwrap` in macros --- clippy_lints/src/unwrap.rs | 6 +++++- tests/ui/checked_unwrap/simple_conditionals.rs | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/unwrap.rs b/clippy_lints/src/unwrap.rs index b21cf41ee523..942543c7bab3 100644 --- a/clippy_lints/src/unwrap.rs +++ b/clippy_lints/src/unwrap.rs @@ -1,4 +1,4 @@ -use crate::utils::{higher::if_block, match_type, paths, span_lint_and_then, usage::is_potentially_mutated}; +use crate::utils::{higher::if_block, in_macro, match_type, paths, span_lint_and_then, usage::is_potentially_mutated}; use if_chain::if_chain; use rustc::hir::map::Map; use rustc_hir::intravisit::*; @@ -138,6 +138,10 @@ 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_macro(expr.span) { + return; + } if let Some((cond, then, els)) = if_block(&expr) { walk_expr(self, cond); self.visit_branch(cond, then, false); diff --git a/tests/ui/checked_unwrap/simple_conditionals.rs b/tests/ui/checked_unwrap/simple_conditionals.rs index c080ae82697a..d50abddade46 100644 --- a/tests/ui/checked_unwrap/simple_conditionals.rs +++ b/tests/ui/checked_unwrap/simple_conditionals.rs @@ -39,4 +39,6 @@ fn main() { // it will always panic but the lint is not smart enough to see this (it // only checks if conditions). } + + assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern }