Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 514b1bc

Browse files
committed
Do not lint unnecessary_unwrap in macros
1 parent 536c255 commit 514b1bc

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

clippy_lints/src/unwrap.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{higher::if_block, match_type, paths, span_lint_and_then, usage::is_potentially_mutated};
1+
use crate::utils::{higher::if_block, in_macro, match_type, paths, span_lint_and_then, usage::is_potentially_mutated};
22
use if_chain::if_chain;
33
use rustc::hir::map::Map;
44
use rustc_hir::intravisit::*;
@@ -138,6 +138,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
138138
type Map = Map<'tcx>;
139139

140140
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
141+
// Shouldn't lint when `expr` is in macro.
142+
if in_macro(expr.span) {
143+
return;
144+
}
141145
if let Some((cond, then, els)) = if_block(&expr) {
142146
walk_expr(self, cond);
143147
self.visit_branch(cond, then, false);

tests/ui/checked_unwrap/simple_conditionals.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ fn main() {
3939
// it will always panic but the lint is not smart enough to see this (it
4040
// only checks if conditions).
4141
}
42+
43+
assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern
4244
}

0 commit comments

Comments
 (0)