Skip to content

Commit a3135ba

Browse files
committed
Auto merge of #5132 - JohnTitor:fix-fp-in-unwrap-lint, r=flip1995
Do not lint `unnecessary_unwrap` in external macros Fixes #5131 I think we shouldn't lint `{panicking, unnecessary}_unwrap` in macros, not just `assert!`. changelog: Fix false positive in `unnecessary_unwrap`
2 parents c1f8be0 + 19ce66c commit a3135ba

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

clippy_lints/src/unwrap.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::utils::{higher::if_block, match_type, paths, span_lint_and_then, usage::is_potentially_mutated};
22
use if_chain::if_chain;
33
use rustc::hir::map::Map;
4+
use rustc::lint::in_external_macro;
45
use rustc_hir::intravisit::*;
56
use rustc_hir::*;
67
use rustc_lint::{LateContext, LateLintPass};
@@ -138,6 +139,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
138139
type Map = Map<'tcx>;
139140

140141
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
142+
// Shouldn't lint when `expr` is in macro.
143+
if in_external_macro(self.cx.tcx.sess, expr.span) {
144+
return;
145+
}
141146
if let Some((cond, then, els)) = if_block(&expr) {
142147
walk_expr(self, cond);
143148
self.visit_branch(cond, then, false);

tests/ui/checked_unwrap/simple_conditionals.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
22
#![allow(clippy::if_same_then_else)]
33

4+
macro_rules! m {
5+
($a:expr) => {
6+
if $a.is_some() {
7+
$a.unwrap(); // unnecessary
8+
}
9+
};
10+
}
11+
412
fn main() {
513
let x = Some(());
614
if x.is_some() {
@@ -13,6 +21,7 @@ fn main() {
1321
} else {
1422
x.unwrap(); // unnecessary
1523
}
24+
m!(x);
1625
let mut x: Result<(), ()> = Ok(());
1726
if x.is_ok() {
1827
x.unwrap(); // unnecessary
@@ -39,4 +48,6 @@ fn main() {
3948
// it will always panic but the lint is not smart enough to see this (it
4049
// only checks if conditions).
4150
}
51+
52+
assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern
4253
}

tests/ui/checked_unwrap/simple_conditionals.stderr

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: You checked before that `unwrap()` cannot fail. Instead of checking and unwrapping, it's better to use `if let` or `match`.
2-
--> $DIR/simple_conditionals.rs:7:9
2+
--> $DIR/simple_conditionals.rs:15:9
33
|
44
LL | if x.is_some() {
55
| ----------- the check is happening here
@@ -13,7 +13,7 @@ LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: This call to `unwrap()` will always panic.
16-
--> $DIR/simple_conditionals.rs:9:9
16+
--> $DIR/simple_conditionals.rs:17:9
1717
|
1818
LL | if x.is_some() {
1919
| ----------- because of this check
@@ -28,15 +28,15 @@ LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
2828
| ^^^^^^^^^^^^^^^^^^^^^^^^
2929

3030
error: This call to `unwrap()` will always panic.
31-
--> $DIR/simple_conditionals.rs:12:9
31+
--> $DIR/simple_conditionals.rs:20:9
3232
|
3333
LL | if x.is_none() {
3434
| ----------- because of this check
3535
LL | x.unwrap(); // will panic
3636
| ^^^^^^^^^^
3737

3838
error: You checked before that `unwrap()` cannot fail. Instead of checking and unwrapping, it's better to use `if let` or `match`.
39-
--> $DIR/simple_conditionals.rs:14:9
39+
--> $DIR/simple_conditionals.rs:22:9
4040
|
4141
LL | if x.is_none() {
4242
| ----------- the check is happening here
@@ -45,15 +45,26 @@ LL | x.unwrap(); // unnecessary
4545
| ^^^^^^^^^^
4646

4747
error: You checked before that `unwrap()` cannot fail. Instead of checking and unwrapping, it's better to use `if let` or `match`.
48-
--> $DIR/simple_conditionals.rs:18:9
48+
--> $DIR/simple_conditionals.rs:7:13
49+
|
50+
LL | if $a.is_some() {
51+
| ------------ the check is happening here
52+
LL | $a.unwrap(); // unnecessary
53+
| ^^^^^^^^^^^
54+
...
55+
LL | m!(x);
56+
| ------ in this macro invocation
57+
58+
error: You checked before that `unwrap()` cannot fail. Instead of checking and unwrapping, it's better to use `if let` or `match`.
59+
--> $DIR/simple_conditionals.rs:27:9
4960
|
5061
LL | if x.is_ok() {
5162
| --------- the check is happening here
5263
LL | x.unwrap(); // unnecessary
5364
| ^^^^^^^^^^
5465

5566
error: This call to `unwrap_err()` will always panic.
56-
--> $DIR/simple_conditionals.rs:19:9
67+
--> $DIR/simple_conditionals.rs:28:9
5768
|
5869
LL | if x.is_ok() {
5970
| --------- because of this check
@@ -62,7 +73,7 @@ LL | x.unwrap_err(); // will panic
6273
| ^^^^^^^^^^^^^^
6374

6475
error: This call to `unwrap()` will always panic.
65-
--> $DIR/simple_conditionals.rs:21:9
76+
--> $DIR/simple_conditionals.rs:30:9
6677
|
6778
LL | if x.is_ok() {
6879
| --------- because of this check
@@ -71,7 +82,7 @@ LL | x.unwrap(); // will panic
7182
| ^^^^^^^^^^
7283

7384
error: You checked before that `unwrap_err()` cannot fail. Instead of checking and unwrapping, it's better to use `if let` or `match`.
74-
--> $DIR/simple_conditionals.rs:22:9
85+
--> $DIR/simple_conditionals.rs:31:9
7586
|
7687
LL | if x.is_ok() {
7788
| --------- the check is happening here
@@ -80,15 +91,15 @@ LL | x.unwrap_err(); // unnecessary
8091
| ^^^^^^^^^^^^^^
8192

8293
error: This call to `unwrap()` will always panic.
83-
--> $DIR/simple_conditionals.rs:25:9
94+
--> $DIR/simple_conditionals.rs:34:9
8495
|
8596
LL | if x.is_err() {
8697
| ---------- because of this check
8798
LL | x.unwrap(); // will panic
8899
| ^^^^^^^^^^
89100

90101
error: You checked before that `unwrap_err()` cannot fail. Instead of checking and unwrapping, it's better to use `if let` or `match`.
91-
--> $DIR/simple_conditionals.rs:26:9
102+
--> $DIR/simple_conditionals.rs:35:9
92103
|
93104
LL | if x.is_err() {
94105
| ---------- the check is happening here
@@ -97,7 +108,7 @@ LL | x.unwrap_err(); // unnecessary
97108
| ^^^^^^^^^^^^^^
98109

99110
error: You checked before that `unwrap()` cannot fail. Instead of checking and unwrapping, it's better to use `if let` or `match`.
100-
--> $DIR/simple_conditionals.rs:28:9
111+
--> $DIR/simple_conditionals.rs:37:9
101112
|
102113
LL | if x.is_err() {
103114
| ---------- the check is happening here
@@ -106,13 +117,13 @@ LL | x.unwrap(); // unnecessary
106117
| ^^^^^^^^^^
107118

108119
error: This call to `unwrap_err()` will always panic.
109-
--> $DIR/simple_conditionals.rs:29:9
120+
--> $DIR/simple_conditionals.rs:38:9
110121
|
111122
LL | if x.is_err() {
112123
| ---------- because of this check
113124
...
114125
LL | x.unwrap_err(); // will panic
115126
| ^^^^^^^^^^^^^^
116127

117-
error: aborting due to 12 previous errors
128+
error: aborting due to 13 previous errors
118129

0 commit comments

Comments
 (0)