Skip to content

Commit

Permalink
Add and prepare tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Oct 14, 2023
1 parent 3e71754 commit 29bd8be
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

fn main() {
enum Foo {
A, B, C,
A,
B,
C,
}

#[allow(non_exhaustive_omitted_patterns)]
Expand All @@ -23,12 +25,14 @@ fn main() {
}
//~^^^^ ERROR non-exhaustive patterns: `Foo::C` not covered

#[warn(non_exhaustive_omitted_patterns)]
//~^ WARNING unknown lint: `non_exhaustive_omitted_patterns`
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
match Foo::A {
Foo::A => {}
Foo::B => {}
#[warn(non_exhaustive_omitted_patterns)]
_ => {}
}
//~^^^ WARNING unknown lint: `non_exhaustive_omitted_patterns`
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LL | #![allow(non_exhaustive_omitted_patterns)]
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable

warning: unknown lint: `non_exhaustive_omitted_patterns`
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:15:5
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:17:5
|
LL | #[allow(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -30,7 +30,7 @@ LL | #[allow(non_exhaustive_omitted_patterns)]
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable

warning: unknown lint: `non_exhaustive_omitted_patterns`
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:15:5
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:17:5
|
LL | #[allow(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -41,15 +41,26 @@ LL | #[allow(non_exhaustive_omitted_patterns)]
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: unknown lint: `non_exhaustive_omitted_patterns`
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:29:9
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:28:5
|
LL | #[warn(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | #[warn(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the `non_exhaustive_omitted_patterns` lint is unstable
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable

warning: unknown lint: `non_exhaustive_omitted_patterns`
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:28:5
|
LL | #[warn(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the `non_exhaustive_omitted_patterns` lint is unstable
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: unknown lint: `non_exhaustive_omitted_patterns`
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:3:1
|
Expand All @@ -73,7 +84,7 @@ LL | #![allow(non_exhaustive_omitted_patterns)]
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: unknown lint: `non_exhaustive_omitted_patterns`
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:15:5
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:17:5
|
LL | #[allow(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -84,7 +95,7 @@ LL | #[allow(non_exhaustive_omitted_patterns)]
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: unknown lint: `non_exhaustive_omitted_patterns`
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:15:5
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:17:5
|
LL | #[allow(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -95,36 +106,48 @@ LL | #[allow(non_exhaustive_omitted_patterns)]
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: unknown lint: `non_exhaustive_omitted_patterns`
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:29:9
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:28:5
|
LL | #[warn(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the `non_exhaustive_omitted_patterns` lint is unstable
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: unknown lint: `non_exhaustive_omitted_patterns`
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:28:5
|
LL | #[warn(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | #[warn(non_exhaustive_omitted_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the `non_exhaustive_omitted_patterns` lint is unstable
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0004]: non-exhaustive patterns: `Foo::C` not covered
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:20:11
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:22:11
|
LL | match Foo::A {
| ^^^^^^ pattern `Foo::C` not covered
|
note: `Foo` defined here
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:12:15
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:14:9
|
LL | enum Foo {
| ---
LL | A, B, C,
| ^ not covered
...
LL | C,
| ^ not covered
= note: the matched value is of type `Foo`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ Foo::B => {},
LL + Foo::C => todo!()
|

error: aborting due to previous error; 10 warnings emitted
error: aborting due to previous error; 12 warnings emitted

For more information about this error, try `rustc --explain E0004`.
60 changes: 53 additions & 7 deletions tests/ui/rfcs/rfc-2008-non-exhaustive/omitted-patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use enums::{
EmptyNonExhaustiveEnum, NestedNonExhaustive, NonExhaustiveEnum, NonExhaustiveSingleVariant,
VariantNonExhaustive,
};
use unstable::{UnstableEnum, OnlyUnstableEnum, UnstableStruct, OnlyUnstableStruct};
use structs::{FunctionalRecord, MixedVisFields, NestedStruct, NormalStruct};
use unstable::{OnlyUnstableEnum, OnlyUnstableStruct, UnstableEnum, UnstableStruct};

#[non_exhaustive]
#[derive(Default)]
Expand All @@ -35,10 +35,10 @@ fn main() {
let enumeration = Bar::A;

// Ok: this is a crate local non_exhaustive enum
#[deny(non_exhaustive_omitted_patterns)]
match enumeration {
Bar::A => {}
Bar::B => {}
#[deny(non_exhaustive_omitted_patterns)]
_ => {}
}

Expand All @@ -51,38 +51,80 @@ fn main() {
_ => {}
}

#[deny(non_exhaustive_omitted_patterns)]
match non_enum {
NonExhaustiveEnum::Unit => {}
NonExhaustiveEnum::Tuple(_) => {}
#[deny(non_exhaustive_omitted_patterns)]
_ => {}
}
//~^^ some variants are not matched explicitly

#[deny(non_exhaustive_omitted_patterns)]
match non_enum {
NonExhaustiveEnum::Unit | NonExhaustiveEnum::Struct { .. } => {}
#[deny(non_exhaustive_omitted_patterns)]
_ => {}
}
//~^^ some variants are not matched explicitly

let x = 5;
#[deny(non_exhaustive_omitted_patterns)]
match non_enum {
NonExhaustiveEnum::Unit if x > 10 => {}
NonExhaustiveEnum::Tuple(_) => {}
NonExhaustiveEnum::Struct { .. } => {}
#[deny(non_exhaustive_omitted_patterns)]
_ => {}
}
//~^^ some variants are not matched explicitly

#[deny(non_exhaustive_omitted_patterns)]
match (non_enum, true) {
(NonExhaustiveEnum::Unit, true) => {}
(NonExhaustiveEnum::Tuple(_), false) => {}
(NonExhaustiveEnum::Struct { .. }, false) => {}
_ => {}
}
#[deny(non_exhaustive_omitted_patterns)]
match (non_enum, true) {
(NonExhaustiveEnum::Unit, true) => {}
(NonExhaustiveEnum::Tuple(_), false) => {}
_ => {}
}
//~^^ some variants are not matched explicitly

// FIXME(Nadrieril): asymmetrical behavior
#[deny(non_exhaustive_omitted_patterns)]
match (true, non_enum) {
(true, NonExhaustiveEnum::Unit) => {}
(false, NonExhaustiveEnum::Tuple(_)) => {}
(false, NonExhaustiveEnum::Struct { .. }) => {}
_ => {}
}
//~^^ some variants are not matched explicitly
//~| some variants are not matched explicitly
#[deny(non_exhaustive_omitted_patterns)]
match (true, non_enum) {
(true, NonExhaustiveEnum::Unit) => {}
(false, NonExhaustiveEnum::Tuple(_)) => {}
_ => {}
}
//~^^ some variants are not matched explicitly
//~| some variants are not matched explicitly

// FIXME(Nadrieril): we should detect this
#[deny(non_exhaustive_omitted_patterns)]
match Some(non_enum) {
Some(NonExhaustiveEnum::Unit) => {}
Some(NonExhaustiveEnum::Tuple(_)) => {}
_ => {}
}

// Ok: all covered and not `unreachable-patterns`
#[deny(unreachable_patterns)]
#[deny(non_exhaustive_omitted_patterns)]
match non_enum {
NonExhaustiveEnum::Unit => {}
NonExhaustiveEnum::Tuple(_) => {}
NonExhaustiveEnum::Struct { .. } => {}
#[deny(non_exhaustive_omitted_patterns)]
_ => {}
}

Expand Down Expand Up @@ -132,15 +174,19 @@ fn main() {
_ => {}
}
//~^^ some variants are not matched explicitly
#[deny(non_exhaustive_omitted_patterns)]
match &NonExhaustiveSingleVariant::A(true) {
_ => {}
}

// Ok: we don't lint on `if let` expressions
#[deny(non_exhaustive_omitted_patterns)]
if let NonExhaustiveEnum::Tuple(_) = non_enum {}

#[deny(non_exhaustive_omitted_patterns)]
match UnstableEnum::Stable {
UnstableEnum::Stable => {}
UnstableEnum::Stable2 => {}
#[deny(non_exhaustive_omitted_patterns)]
_ => {}
}
//~^^ some variants are not matched explicitly
Expand Down
Loading

0 comments on commit 29bd8be

Please sign in to comment.