Skip to content

Commit d2e5344

Browse files
committed
Now also lints non-exhaustive structs
1 parent 0a6e3ae commit d2e5344

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

clippy_lints/src/let_else_on_result_ok.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ impl<'tcx> LateLintPass<'tcx> for LetElseOnResultOk {
107107
&& let [_, err_ty] = substs.as_slice()
108108
&& let Some(err_ty) = err_ty.as_type()
109109
&& let Some(err_def) = err_ty.ty_adt_def()
110-
&& err_def.all_fields().count() != 0
110+
&& (err_def.all_fields().count() != 0
111+
|| err_def
112+
.variants()
113+
.iter()
114+
.any(|variant| variant.is_field_list_non_exhaustive())
115+
|| err_def.is_variant_list_non_exhaustive())
111116
{
112117
spans.push(pat.span);
113118
}

tests/ui/let_else_on_result_ok.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ union F {
3030
a: u32,
3131
}
3232

33+
#[non_exhaustive]
34+
struct G {}
35+
36+
#[non_exhaustive]
37+
enum H {}
38+
3339
fn a() -> Result<(), ()> {
3440
Ok(())
3541
}
@@ -57,6 +63,14 @@ fn f() -> Result<(), F> {
5763
todo!()
5864
}
5965

66+
fn g() -> Result<(), G> {
67+
todo!()
68+
}
69+
70+
fn h() -> Result<(), H> {
71+
todo!()
72+
}
73+
6074
fn a_constructor() -> A {
6175
todo!();
6276
}
@@ -76,6 +90,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
7690
let Ok(_) = f() else {
7791
return Ok(());
7892
};
93+
let Ok(_) = g() else {
94+
return Ok(());
95+
};
96+
let Ok(_) = h() else {
97+
return Ok(());
98+
};
7999
// Don't lint
80100
loop {
81101
let Ok(_) = c() else {

tests/ui/let_else_on_result_ok.stderr

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: usage of `let...else` on `Ok`
2-
--> $DIR/let_else_on_result_ok.rs:70:9
2+
--> $DIR/let_else_on_result_ok.rs:84:9
33
|
44
LL | let Ok(_) = c() else {
55
| ^^^^^
@@ -9,7 +9,7 @@ LL | let Ok(_) = c() else {
99
= note: `-D clippy::let-else-on-result-ok` implied by `-D warnings`
1010

1111
error: usage of `let...else` on `Ok`
12-
--> $DIR/let_else_on_result_ok.rs:73:9
12+
--> $DIR/let_else_on_result_ok.rs:87:9
1313
|
1414
LL | let Ok(_) = e() else {
1515
| ^^^^^
@@ -18,13 +18,31 @@ LL | let Ok(_) = e() else {
1818
= help: consider using a `match` instead, or propagating it to the caller
1919

2020
error: usage of `let...else` on `Ok`
21-
--> $DIR/let_else_on_result_ok.rs:76:9
21+
--> $DIR/let_else_on_result_ok.rs:90:9
2222
|
2323
LL | let Ok(_) = f() else {
2424
| ^^^^^
2525
|
2626
= note: this will ignore the contents of the `Err` variant
2727
= help: consider using a `match` instead, or propagating it to the caller
2828

29-
error: aborting due to 3 previous errors
29+
error: usage of `let...else` on `Ok`
30+
--> $DIR/let_else_on_result_ok.rs:93:9
31+
|
32+
LL | let Ok(_) = g() else {
33+
| ^^^^^
34+
|
35+
= note: this will ignore the contents of the `Err` variant
36+
= help: consider using a `match` instead, or propagating it to the caller
37+
38+
error: usage of `let...else` on `Ok`
39+
--> $DIR/let_else_on_result_ok.rs:96:9
40+
|
41+
LL | let Ok(_) = h() else {
42+
| ^^^^^
43+
|
44+
= note: this will ignore the contents of the `Err` variant
45+
= help: consider using a `match` instead, or propagating it to the caller
46+
47+
error: aborting due to 5 previous errors
3048

0 commit comments

Comments
 (0)