Skip to content

necessary unsafe keywords marked as unnecessary when inside incomplete match #115348

@sam0x17

Description

@sam0x17

Code

enum MyEnum {
    Red,
    Blue,
    Purple,
    Gray,
    Orange,
}

fn my_fn(input: MyEnum) -> u32 {
    let bytes1 = [1u8, 2u8, 3u8, 4u8];
    let bytes2 = [1u8, 3u8, 3u8, 4u8];
    let bytes3 = [1u8, 2u8, 0u8, 4u8];
    let bytes4 = [1u8, 7u8, 3u8, 4u8];
    match input {
        MyEnum::Red => unsafe { core::mem::transmute_copy(&bytes1) },
        MyEnum::Blue => unsafe { core::mem::transmute_copy(&bytes2) },
        MyEnum::Purple => unsafe { core::mem::transmute_copy(&bytes3) },
        MyEnum::Gray => unsafe { core::mem::transmute_copy(&bytes4) },
    }
}

Current output

error[E0004]: non-exhaustive patterns: `MyEnum::Orange` not covered
  --> src/lib.rs:15:11
   |
15 |     match input {
   |           ^^^^^ pattern `MyEnum::Orange` not covered
   |
note: `MyEnum` defined here
  --> src/lib.rs:7:5
   |
2  | enum MyEnum {
   |      ------
...
7  |     Orange,
   |     ^^^^^^ not covered
   = note: the matched value is of type `MyEnum`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
19 ~         MyEnum::Gray => unsafe { core::mem::transmute_copy(&bytes4) },
20 ~         MyEnum::Orange => todo!(),
   |

warning: unnecessary `unsafe` block
  --> src/lib.rs:16:24
   |
16 |         MyEnum::Red => unsafe { core::mem::transmute_copy(&bytes1) },
   |                        ^^^^^^ unnecessary `unsafe` block
   |
   = note: `#[warn(unused_unsafe)]` on by default

warning: unnecessary `unsafe` block
  --> src/lib.rs:17:25
   |
17 |         MyEnum::Blue => unsafe { core::mem::transmute_copy(&bytes2) },
   |                         ^^^^^^ unnecessary `unsafe` block

warning: unnecessary `unsafe` block
  --> src/lib.rs:18:27
   |
18 |         MyEnum::Purple => unsafe { core::mem::transmute_copy(&bytes3) },
   |                           ^^^^^^ unnecessary `unsafe` block

warning: unnecessary `unsafe` block
  --> src/lib.rs:19:25
   |
19 |         MyEnum::Gray => unsafe { core::mem::transmute_copy(&bytes4) },
   |                         ^^^^^^ unnecessary `unsafe` block

For more information about this error, try `rustc --explain E0004`.
warning: `playground` (lib) generated 4 warnings
error: could not compile `playground` (lib) due to previous error; 4 warnings emitted

Desired output

error[E0004]: non-exhaustive patterns: `MyEnum::Orange` not covered
  --> src/lib.rs:15:11
   |
15 |     match input {
   |           ^^^^^ pattern `MyEnum::Orange` not covered
   |
note: `MyEnum` defined here
  --> src/lib.rs:7:5
   |
2  | enum MyEnum {
   |      ------
...
7  |     Orange,
   |     ^^^^^^ not covered
   = note: the matched value is of type `MyEnum`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
19 ~         MyEnum::Gray => unsafe { core::mem::transmute_copy(&bytes4) },
20 ~         MyEnum::Orange => todo!(),
   |
For more information about this error, try `rustc --explain E0004`.
warning: `playground` (lib) generated 4 warnings
error: could not compile `playground` (lib) due to previous error; 4 warnings emitted

Rationale and extra context

Each of the above unsafe keywords gets marked with an "unnecessary" warning until the missing match arms are added at which point the warnings disappear. This is confusing since many will see those warnings and remove the unsafe blocks only to need to re-add them when they finish writing their match.

Originally reported on RA here: rust-lang/rust-analyzer#15514 (comment)

Other cases

No response

Anything else?

Any scenario where you have a match statement with necessary unsafe blocks in any of the arms, and the match statement is incomplete.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions