Auto merge of #109836 - clubby789:param-non-exhaustive, r=Nilstrieb

Fix `non_exhaustive_omitted_patterns` on arguments and locals

Fixes #99815

r? `@Nilstrieb`
This commit is contained in:
bors 2023-04-02 04:26:05 +00:00
commit f5f93d0063
3 changed files with 35 additions and 1 deletions

View File

@ -300,6 +300,7 @@
use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::HirId; use rustc_hir::HirId;
use rustc_hir::Node;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS; use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
@ -867,6 +868,8 @@ fn is_useful<'p, 'tcx>(
&ctor, &ctor,
Constructor::Missing { nonexhaustive_enum_missing_real_variants: true } 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 patterns = {
let mut split_wildcard = SplitWildcard::new(pcx); let mut split_wildcard = SplitWildcard::new(pcx);

View File

@ -184,4 +184,20 @@ fn main() {
// OK: both unstable and stable fields are matched with feature on // OK: both unstable and stable fields are matched with feature on
#[warn(non_exhaustive_omitted_patterns)] #[warn(non_exhaustive_omitted_patterns)]
let UnstableStruct { stable, stable2, unstable, .. } = UnstableStruct::default(); 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| {};
} }

View File

@ -184,5 +184,20 @@ note: the lint level is defined here
LL | #[deny(non_exhaustive_omitted_patterns)] 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`.