diff options
| author | est31 <MTest31@outlook.com> | 2022-02-19 07:15:20 +0100 |
|---|---|---|
| committer | est31 <MTest31@outlook.com> | 2022-10-24 22:05:39 +0200 |
| commit | 5da7a176b778656d2e6f736f5a23ce6fdb73379d (patch) | |
| tree | 2ae948f417575b447a272fdd7bfa01a1e160d5d8 /tests | |
| parent | c5a7696231ef60e77fdeb5068c729d63c0393547 (diff) | |
| download | rust-5da7a176b778656d2e6f736f5a23ce6fdb73379d.tar.gz rust-5da7a176b778656d2e6f736f5a23ce6fdb73379d.zip | |
Don't suggest let else in match if the else arm explicitly mentions non obvious paths
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/manual_let_else_match.rs | 62 | ||||
| -rw-r--r-- | tests/ui/manual_let_else_match.stderr | 30 |
2 files changed, 69 insertions, 23 deletions
diff --git a/tests/ui/manual_let_else_match.rs b/tests/ui/manual_let_else_match.rs index 56da1db49f2..93c86ca24fe 100644 --- a/tests/ui/manual_let_else_match.rs +++ b/tests/ui/manual_let_else_match.rs @@ -4,12 +4,6 @@ // Ensure that we don't conflict with match -> if let lints #![warn(clippy::single_match_else, clippy::single_match)] -enum Variant { - Foo, - Bar(u32), - Baz(u32), -} - fn f() -> Result<u32, u32> { Ok(0) } @@ -18,7 +12,17 @@ fn g() -> Option<()> { None } -fn h() -> Variant { +fn h() -> (Option<()>, Option<()>) { + (None, None) +} + +enum Variant { + Foo, + Bar(u32), + Baz(u32), +} + +fn build_enum() -> Variant { Variant::Foo } @@ -36,9 +40,14 @@ fn fire() { }; loop { - // More complex pattern for the identity arm + // More complex pattern for the identity arm and diverging arm let v = match h() { - Variant::Foo => continue, + (Some(_), Some(_)) | (None, None) => continue, + (Some(v), None) | (None, Some(v)) => v, + }; + // Custom enums are supported as long as the "else" arm is a simple _ + let v = match build_enum() { + _ => continue, Variant::Bar(v) | Variant::Baz(v) => v, }; } @@ -49,21 +58,27 @@ fn fire() { Ok(v) => v, Err(_) => return, }; + + // Err(()) is an allowed pattern + let v = match f().map_err(|_| ()) { + Ok(v) => v, + Err(()) => return, + }; } fn not_fire() { // Multiple diverging arms let v = match h() { - Variant::Foo => panic!(), - Variant::Bar(_v) => return, - Variant::Baz(v) => v, + _ => panic!(), + (None, Some(_v)) => return, + (Some(v), None) => v, }; // Multiple identity arms let v = match h() { - Variant::Foo => panic!(), - Variant::Bar(v) => v, - Variant::Baz(v) => v, + _ => panic!(), + (None, Some(v)) => v, + (Some(v), None) => v, }; // No diverging arm at all, only identity arms. @@ -74,8 +89,8 @@ fn not_fire() { }; // The identity arm has a guard - let v = match h() { - Variant::Bar(v) if g().is_none() => v, + let v = match g() { + Some(v) if g().is_none() => v, _ => return, }; @@ -90,4 +105,17 @@ fn not_fire() { Ok(v) => v, Err(e) => panic!("error: {e}"), }; + + // Custom enum where the diverging arm + // explicitly mentions the variant + let v = match build_enum() { + Variant::Foo => return, + Variant::Bar(v) | Variant::Baz(v) => v, + }; + + // The custom enum is surrounded by an Err() + let v = match Err(build_enum()) { + Ok(v) | Err(Variant::Bar(v) | Variant::Baz(v)) => v, + Err(Variant::Foo) => return, + }; } diff --git a/tests/ui/manual_let_else_match.stderr b/tests/ui/manual_let_else_match.stderr index 2dae1d15ad4..a79afc6401d 100644 --- a/tests/ui/manual_let_else_match.stderr +++ b/tests/ui/manual_let_else_match.stderr @@ -1,5 +1,5 @@ error: this could be rewritten as `let else` - --> $DIR/manual_let_else_match.rs:28:5 + --> $DIR/manual_let_else_match.rs:32:5 | LL | / let v = match g() { LL | | Some(v_some) => v_some, @@ -10,7 +10,7 @@ LL | | }; = note: `-D clippy::manual-let-else` implied by `-D warnings` error: this could be rewritten as `let else` - --> $DIR/manual_let_else_match.rs:33:5 + --> $DIR/manual_let_else_match.rs:37:5 | LL | / let v = match g() { LL | | Some(v_some) => v_some, @@ -19,16 +19,25 @@ LL | | }; | |______^ error: this could be rewritten as `let else` - --> $DIR/manual_let_else_match.rs:40:9 + --> $DIR/manual_let_else_match.rs:44:9 | LL | / let v = match h() { -LL | | Variant::Foo => continue, +LL | | (Some(_), Some(_)) | (None, None) => continue, +LL | | (Some(v), None) | (None, Some(v)) => v, +LL | | }; + | |__________^ + +error: this could be rewritten as `let else` + --> $DIR/manual_let_else_match.rs:49:9 + | +LL | / let v = match build_enum() { +LL | | _ => continue, LL | | Variant::Bar(v) | Variant::Baz(v) => v, LL | | }; | |__________^ error: this could be rewritten as `let else` - --> $DIR/manual_let_else_match.rs:48:5 + --> $DIR/manual_let_else_match.rs:57:5 | LL | / let v = match f() { LL | | Ok(v) => v, @@ -36,5 +45,14 @@ LL | | Err(_) => return, LL | | }; | |______^ -error: aborting due to 4 previous errors +error: this could be rewritten as `let else` + --> $DIR/manual_let_else_match.rs:63:5 + | +LL | / let v = match f().map_err(|_| ()) { +LL | | Ok(v) => v, +LL | | Err(()) => return, +LL | | }; + | |______^ + +error: aborting due to 6 previous errors |
