Skip to content

Commit 79fc271

Browse files
committed
Fix #[expect] for unnecessary_unwrap, panicking_unwrap
1 parent bdc6ece commit 79fc271

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

clippy_lints/src/unwrap.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_then;
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::higher;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use clippy_utils::{path_to_local, usage::is_potentially_mutated};
@@ -251,9 +251,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
251251
unwrappable.kind.error_variant_pattern()
252252
};
253253

254-
span_lint_and_then(
254+
span_lint_hir_and_then(
255255
self.cx,
256256
UNNECESSARY_UNWRAP,
257+
expr.hir_id,
257258
expr.span,
258259
&format!(
259260
"called `{}` on `{}` after checking its variant with `{}`",
@@ -283,9 +284,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
283284
},
284285
);
285286
} else {
286-
span_lint_and_then(
287+
span_lint_hir_and_then(
287288
self.cx,
288289
PANICKING_UNWRAP,
290+
expr.hir_id,
289291
expr.span,
290292
&format!("this call to `{}()` will always panic",
291293
method_name.ident.name),

tests/ui/checked_unwrap/simple_conditionals.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(lint_reasons)]
12
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
23
#![allow(clippy::if_same_then_else, clippy::branches_sharing_code)]
34

@@ -84,3 +85,18 @@ fn main() {
8485

8586
assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern
8687
}
88+
89+
fn check_expect() {
90+
let x = Some(());
91+
if x.is_some() {
92+
#[expect(clippy::unnecessary_unwrap)]
93+
x.unwrap(); // unnecessary
94+
#[expect(clippy::unnecessary_unwrap)]
95+
x.expect("an error message"); // unnecessary
96+
} else {
97+
#[expect(clippy::panicking_unwrap)]
98+
x.unwrap(); // will panic
99+
#[expect(clippy::panicking_unwrap)]
100+
x.expect("an error message"); // will panic
101+
}
102+
}

tests/ui/checked_unwrap/simple_conditionals.stderr

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: called `unwrap` on `x` after checking its variant with `is_some`
2-
--> $DIR/simple_conditionals.rs:39:9
2+
--> $DIR/simple_conditionals.rs:40:9
33
|
44
LL | if x.is_some() {
55
| -------------- help: try: `if let Some(..) = x`
66
LL | x.unwrap(); // unnecessary
77
| ^^^^^^^^^^
88
|
99
note: the lint level is defined here
10-
--> $DIR/simple_conditionals.rs:1:35
10+
--> $DIR/simple_conditionals.rs:2:35
1111
|
1212
LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: called `expect` on `x` after checking its variant with `is_some`
16-
--> $DIR/simple_conditionals.rs:40:9
16+
--> $DIR/simple_conditionals.rs:41:9
1717
|
1818
LL | if x.is_some() {
1919
| -------------- help: try: `if let Some(..) = x`
@@ -22,7 +22,7 @@ LL | x.expect("an error message"); // unnecessary
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323

2424
error: this call to `unwrap()` will always panic
25-
--> $DIR/simple_conditionals.rs:42:9
25+
--> $DIR/simple_conditionals.rs:43:9
2626
|
2727
LL | if x.is_some() {
2828
| ----------- because of this check
@@ -31,13 +31,13 @@ LL | x.unwrap(); // will panic
3131
| ^^^^^^^^^^
3232
|
3333
note: the lint level is defined here
34-
--> $DIR/simple_conditionals.rs:1:9
34+
--> $DIR/simple_conditionals.rs:2:9
3535
|
3636
LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
error: this call to `expect()` will always panic
40-
--> $DIR/simple_conditionals.rs:43:9
40+
--> $DIR/simple_conditionals.rs:44:9
4141
|
4242
LL | if x.is_some() {
4343
| ----------- because of this check
@@ -46,15 +46,15 @@ LL | x.expect("an error message"); // will panic
4646
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4747

4848
error: this call to `unwrap()` will always panic
49-
--> $DIR/simple_conditionals.rs:46:9
49+
--> $DIR/simple_conditionals.rs:47:9
5050
|
5151
LL | if x.is_none() {
5252
| ----------- because of this check
5353
LL | x.unwrap(); // will panic
5454
| ^^^^^^^^^^
5555

5656
error: called `unwrap` on `x` after checking its variant with `is_none`
57-
--> $DIR/simple_conditionals.rs:48:9
57+
--> $DIR/simple_conditionals.rs:49:9
5858
|
5959
LL | if x.is_none() {
6060
| -------------- help: try: `if let Some(..) = x`
@@ -63,7 +63,7 @@ LL | x.unwrap(); // unnecessary
6363
| ^^^^^^^^^^
6464

6565
error: called `unwrap` on `x` after checking its variant with `is_some`
66-
--> $DIR/simple_conditionals.rs:7:13
66+
--> $DIR/simple_conditionals.rs:8:13
6767
|
6868
LL | if $a.is_some() {
6969
| --------------- help: try: `if let Some(..) = x`
@@ -76,15 +76,15 @@ LL | m!(x);
7676
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
7777

7878
error: called `unwrap` on `x` after checking its variant with `is_ok`
79-
--> $DIR/simple_conditionals.rs:56:9
79+
--> $DIR/simple_conditionals.rs:57:9
8080
|
8181
LL | if x.is_ok() {
8282
| ------------ help: try: `if let Ok(..) = x`
8383
LL | x.unwrap(); // unnecessary
8484
| ^^^^^^^^^^
8585

8686
error: called `expect` on `x` after checking its variant with `is_ok`
87-
--> $DIR/simple_conditionals.rs:57:9
87+
--> $DIR/simple_conditionals.rs:58:9
8888
|
8989
LL | if x.is_ok() {
9090
| ------------ help: try: `if let Ok(..) = x`
@@ -93,7 +93,7 @@ LL | x.expect("an error message"); // unnecessary
9393
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9494

9595
error: this call to `unwrap_err()` will always panic
96-
--> $DIR/simple_conditionals.rs:58:9
96+
--> $DIR/simple_conditionals.rs:59:9
9797
|
9898
LL | if x.is_ok() {
9999
| --------- because of this check
@@ -102,7 +102,7 @@ LL | x.unwrap_err(); // will panic
102102
| ^^^^^^^^^^^^^^
103103

104104
error: this call to `unwrap()` will always panic
105-
--> $DIR/simple_conditionals.rs:60:9
105+
--> $DIR/simple_conditionals.rs:61:9
106106
|
107107
LL | if x.is_ok() {
108108
| --------- because of this check
@@ -111,7 +111,7 @@ LL | x.unwrap(); // will panic
111111
| ^^^^^^^^^^
112112

113113
error: this call to `expect()` will always panic
114-
--> $DIR/simple_conditionals.rs:61:9
114+
--> $DIR/simple_conditionals.rs:62:9
115115
|
116116
LL | if x.is_ok() {
117117
| --------- because of this check
@@ -120,7 +120,7 @@ LL | x.expect("an error message"); // will panic
120120
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121121

122122
error: called `unwrap_err` on `x` after checking its variant with `is_ok`
123-
--> $DIR/simple_conditionals.rs:62:9
123+
--> $DIR/simple_conditionals.rs:63:9
124124
|
125125
LL | if x.is_ok() {
126126
| ------------ help: try: `if let Err(..) = x`
@@ -129,15 +129,15 @@ LL | x.unwrap_err(); // unnecessary
129129
| ^^^^^^^^^^^^^^
130130

131131
error: this call to `unwrap()` will always panic
132-
--> $DIR/simple_conditionals.rs:65:9
132+
--> $DIR/simple_conditionals.rs:66:9
133133
|
134134
LL | if x.is_err() {
135135
| ---------- because of this check
136136
LL | x.unwrap(); // will panic
137137
| ^^^^^^^^^^
138138

139139
error: called `unwrap_err` on `x` after checking its variant with `is_err`
140-
--> $DIR/simple_conditionals.rs:66:9
140+
--> $DIR/simple_conditionals.rs:67:9
141141
|
142142
LL | if x.is_err() {
143143
| ------------- help: try: `if let Err(..) = x`
@@ -146,7 +146,7 @@ LL | x.unwrap_err(); // unnecessary
146146
| ^^^^^^^^^^^^^^
147147

148148
error: called `unwrap` on `x` after checking its variant with `is_err`
149-
--> $DIR/simple_conditionals.rs:68:9
149+
--> $DIR/simple_conditionals.rs:69:9
150150
|
151151
LL | if x.is_err() {
152152
| ------------- help: try: `if let Ok(..) = x`
@@ -155,7 +155,7 @@ LL | x.unwrap(); // unnecessary
155155
| ^^^^^^^^^^
156156

157157
error: this call to `unwrap_err()` will always panic
158-
--> $DIR/simple_conditionals.rs:69:9
158+
--> $DIR/simple_conditionals.rs:70:9
159159
|
160160
LL | if x.is_err() {
161161
| ---------- because of this check

0 commit comments

Comments
 (0)