Skip to content

Commit

Permalink
Now also lints non-exhaustive structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 20, 2023
1 parent 0a6e3ae commit f7c0ef9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
9 changes: 7 additions & 2 deletions clippy_lints/src/let_else_on_result_ok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use clippy_utils::{
};
use rustc_hir::{ExprKind, FnRetTy, ItemKind, OwnerNode, Stmt, StmtKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::{lint::in_external_macro, ty};
use rustc_middle::{lint::in_external_macro, ty::{self, VariantDef}};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::sym;
use std::ops::ControlFlow;
Expand Down Expand Up @@ -107,7 +107,12 @@ impl<'tcx> LateLintPass<'tcx> for LetElseOnResultOk {
&& let [_, err_ty] = substs.as_slice()
&& let Some(err_ty) = err_ty.as_type()
&& let Some(err_def) = err_ty.ty_adt_def()
&& err_def.all_fields().count() != 0
&& (err_def.all_fields().count() != 0
|| err_def
.variants()
.iter()
.any(VariantDef::is_field_list_non_exhaustive)
|| err_def.is_variant_list_non_exhaustive())
{
spans.push(pat.span);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/let_else_on_result_ok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ union F {
a: u32,
}

#[non_exhaustive]
struct G {}

#[non_exhaustive]
enum H {}

fn a() -> Result<(), ()> {
Ok(())
}
Expand Down Expand Up @@ -57,6 +63,14 @@ fn f() -> Result<(), F> {
todo!()
}

fn g() -> Result<(), G> {
todo!()
}

fn h() -> Result<(), H> {
todo!()
}

fn a_constructor() -> A {
todo!();
}
Expand All @@ -76,6 +90,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let Ok(_) = f() else {
return Ok(());
};
let Ok(_) = g() else {
return Ok(());
};
let Ok(_) = h() else {
return Ok(());
};
// Don't lint
loop {
let Ok(_) = c() else {
Expand Down
26 changes: 22 additions & 4 deletions tests/ui/let_else_on_result_ok.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: usage of `let...else` on `Ok`
--> $DIR/let_else_on_result_ok.rs:70:9
--> $DIR/let_else_on_result_ok.rs:84:9
|
LL | let Ok(_) = c() else {
| ^^^^^
Expand All @@ -9,7 +9,7 @@ LL | let Ok(_) = c() else {
= note: `-D clippy::let-else-on-result-ok` implied by `-D warnings`

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

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

error: aborting due to 3 previous errors
error: usage of `let...else` on `Ok`
--> $DIR/let_else_on_result_ok.rs:93:9
|
LL | let Ok(_) = g() else {
| ^^^^^
|
= note: this will ignore the contents of the `Err` variant
= help: consider using a `match` instead, or propagating it to the caller

error: usage of `let...else` on `Ok`
--> $DIR/let_else_on_result_ok.rs:96:9
|
LL | let Ok(_) = h() else {
| ^^^^^
|
= note: this will ignore the contents of the `Err` variant
= help: consider using a `match` instead, or propagating it to the caller

error: aborting due to 5 previous errors

0 comments on commit f7c0ef9

Please sign in to comment.