Skip to content

single-match makes incorrect suggestion for unsafe code #10808

Closed
@nyurik

Description

@nyurik

Summary

This type of code creates incorrect suggestions for all lints like single-match and single-match-else:

match bar {
    Some(v) => unsafe { ... }
    ...
}

Reproducer

Several non-passing tests for this issue have already been created in #10809

Run cargo clippy -- -W clippy::single-match with this code:

fn main() {
    foo(None);
}

pub fn foo(bar: Option<u32>) {
    match bar {
        Some(v) => unsafe {
            let r = &v as *const u32;
            println!("{}", *r);
        },
        _ => {}
    }
}

I expected Clippy to suggest this code:

    if let Some(v) = bar {
        unsafe {
            let r = &v as *const u32;
            println!("{}", *r);
        }
    }

Instead, it suggested this broken code:

    if let Some(v) = bar unsafe {
        let r = &v as *const u32;
        println!("{}", *r);
    }

Version

rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-unknown-linux-gnu
release: 1.69.0
LLVM version: 15.0.7

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions