Skip to content

Fix non_exhaustive_omitted_patterns on arguments and locals #109836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ use rustc_arena::TypedArena;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir::def_id::DefId;
use rustc_hir::HirId;
use rustc_hir::Node;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -867,6 +868,8 @@ fn is_useful<'p, 'tcx>(
&ctor,
Constructor::Missing { nonexhaustive_enum_missing_real_variants: true }
)
// We don't want to lint patterns which are function arguments or locals
&& !matches!(cx.tcx.hir().find_parent(hir_id), Some(Node::Param(_)|Node::Local(_)))
{
let patterns = {
let mut split_wildcard = SplitWildcard::new(pcx);
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/rfc-2008-non-exhaustive/omitted-patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,20 @@ fn main() {
// OK: both unstable and stable fields are matched with feature on
#[warn(non_exhaustive_omitted_patterns)]
let UnstableStruct { stable, stable2, unstable, .. } = UnstableStruct::default();

// Ok: local bindings are allowed
#[deny(non_exhaustive_omitted_patterns)]
let local = NonExhaustiveEnum::Unit;

// Ok: missing patterns will be blocked by the pattern being refutable
#[deny(non_exhaustive_omitted_patterns)]
let local_refutable @ NonExhaustiveEnum::Unit = NonExhaustiveEnum::Unit;
//~^ refutable pattern in local binding

}

#[deny(non_exhaustive_omitted_patterns)]
// Ok: Pattern in a param is always wildcard
pub fn takes_non_exhaustive(_: NonExhaustiveEnum) {
let _closure = |_: NonExhaustiveEnum| {};
}
17 changes: 16 additions & 1 deletion tests/ui/rfc-2008-non-exhaustive/omitted-patterns.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,20 @@ note: the lint level is defined here
LL | #[deny(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors; 6 warnings emitted
error[E0005]: refutable pattern in local binding
--> $DIR/omitted-patterns.rs:194:9
|
LL | let local_refutable @ NonExhaustiveEnum::Unit = NonExhaustiveEnum::Unit;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `NonExhaustiveEnum`
help: you might want to use `let else` to handle the variant that isn't matched
|
LL | let local_refutable @ NonExhaustiveEnum::Unit = NonExhaustiveEnum::Unit else { todo!() };
| ++++++++++++++++

error: aborting due to 9 previous errors; 6 warnings emitted

For more information about this error, try `rustc --explain E0005`.