Skip to content
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

RA thinks unsafe is unnecessary on unsafe match arms when the match is incomplete (missing arms) #15514

Closed
sam0x17 opened this issue Aug 25, 2023 · 1 comment
Labels
C-bug Category: bug

Comments

@sam0x17
Copy link

sam0x17 commented Aug 25, 2023

rust-analyzer version: 0.3.1631-standalone

rustc version: rustc 1.72.0 (5680fa18f 2023-08-23)

When you have an incomplete match statement (i.e. not covering all pattern variants) and one or more of the arms has unsafe code inside of unsafe { .. } blocks, Rust Analyzer will incorrectly put warnings on each unsafe keyword saying the unsafe block is unnecessary.

This was quite confusing, as when I encountered this, although I was aware my match statement was incomplete and I was in the process of filling in the missing variants, I actually removed the unsafe blocks due to these warnings because I thought "oh, I must be inside an unsafe fn", only to find that once I filled in the missing variants, it wanted the unsafe blocks back.

This toy example reproduces it:

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) },
    }
}

image

Once you fill in the missing MyEnum::Orange variant, everything will compile and the warnings disappear.

@sam0x17 sam0x17 added the C-bug Category: bug label Aug 25, 2023
@sam0x17 sam0x17 changed the title RA thinks unsafe is unnecessary on unsafe match arms when there are one or more missing match arms RA thinks unsafe is unnecessary on unsafe match arms when the match is incomplete (missing arms) Aug 25, 2023
@Veykril
Copy link
Member

Veykril commented Aug 29, 2023

This is a rustc diagnostic, see https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a3257deaf19572aaeb3cf7300eb12c87

So you might wanna report that upstream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

2 participants