Skip to content

unsafe_code lint does not properly detect usages of unsafe functions when in an unsafe scope #104987

Open
@asquared31415

Description

@asquared31415

This code:

fn uwu() {
    unsafe {
        #[forbid(unsafe_code)]
        let owo = (&1 as *const i32).read();
        dbg!(owo);
    }
}

Playground

Does not produce an error for using unsafe code in an expression that is annotated with a forbid(unsafe_code)

Instead, this happened: program compiled without any output

The same issue occurs in several similar scenarios, such as inside an unsafe fn (when unsafe_op_in_unsafe_fn is not enabled)

unsafe fn uwu() {
    #[forbid(unsafe_code)]
    {
        let owo = (&1 as *const i32).read();
        dbg!(owo);
    }
}

Meta

Reproduces on all of current stable, beta, and nightly:
Stable 1.65.0
Beta 1.66.0-beta.2
Nightly 1.67.0 2022-11-26 80a9646

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-unsafe_codeLint: unsafe_codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions