Skip to content

Commit bd47855

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

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

clippy_lints/src/let_else_on_result_ok.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use clippy_utils::{
77
};
88
use rustc_hir::{ExprKind, FnRetTy, ItemKind, OwnerNode, Stmt, StmtKind};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
10-
use rustc_middle::{lint::in_external_macro, ty};
10+
use rustc_middle::{
11+
lint::in_external_macro,
12+
ty::{self, VariantDef},
13+
};
1114
use rustc_session::{declare_tool_lint, impl_lint_pass};
1215
use rustc_span::sym;
1316
use std::ops::ControlFlow;
@@ -107,7 +110,12 @@ impl<'tcx> LateLintPass<'tcx> for LetElseOnResultOk {
107110
&& let [_, err_ty] = substs.as_slice()
108111
&& let Some(err_ty) = err_ty.as_type()
109112
&& let Some(err_def) = err_ty.ty_adt_def()
110-
&& err_def.all_fields().count() != 0
113+
&& (err_def.all_fields().count() != 0
114+
|| err_def
115+
.variants()
116+
.iter()
117+
.any(VariantDef::is_field_list_non_exhaustive)
118+
|| err_def.is_variant_list_non_exhaustive())
111119
{
112120
spans.push(pat.span);
113121
}

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)