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

Newline in assignment causes false negative in undocumented_unsafe_blocks #11534

Closed
joshlf opened this issue Sep 18, 2023 · 7 comments
Closed
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@joshlf
Copy link

joshlf commented Sep 18, 2023

Summary

When a newline separates the = sign and the beginning of an unsafe block (as rustfmt sometimes produces), Clippy doesn't recognize a safety comment as decorating the unsafe block:

// SAFETY: ...
let x =
    unsafe { ... };

Lint Name

undocumented_unsafe_blocks

Reproducer

I tried this code:

#![deny(clippy::undocumented_unsafe_blocks)]

fn main() {
    // SAFETY: No unsafe code here!
    let _x =
        unsafe { 0 };
    // SAFETY: No unsafe code here!
    let _y = unsafe { 0 };
}

I expected to see this happen:

Both unsafe blocks should have been recognized as having a safety comment.

Instead, this happened:

undocumented_unsafe_blocks fired on let _x = ....

Version

1.72 (stable)
@joshlf joshlf added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Sep 18, 2023
@y21
Copy link
Member

y21 commented Sep 18, 2023

The expected behavior is behind a configuration option. Setting accept-comment-above-statement = true in clippy.toml makes it recognize both safety comments and not lint.
(Also, this would be a FP, wouldn't it? It lints on code where it shouldn't)

@joshlf
Copy link
Author

joshlf commented Sep 19, 2023

The expected behavior is behind a configuration option. Setting accept-comment-above-statement = true in clippy.toml makes it recognize both safety comments and not lint. (Also, this would be a FP, wouldn't it? It lints on code where it shouldn't)

Okay great, thanks!

And yeah, I should have said false positive - I guess I meant that the false negative was failing to notice the safety comment.

@Alexendoo Alexendoo added I-false-positive Issue: The lint was triggered on code it shouldn't have and removed I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Sep 19, 2023
@Alexendoo
Copy link
Member

Alexendoo commented Sep 19, 2023

Is there a reason that isn't on by default?

edit: #10832 (comment)

@Centri3
Copy link
Member

Centri3 commented Sep 20, 2023

There's #11170 which changes both to true. Looks like it just needs a collect-metadata run

@Alexendoo
Copy link
Member

Nice, fixed by #11170

@jstarks
Copy link

jstarks commented Oct 12, 2023

This is still broken. It's working for let bindings, but not normal assignments.

E.g., this is fine:

let x = 
    unsafe { () };

This gives a false positive:

let x;
x =
    unsafe { () };

@Alexendoo
Copy link
Member

Could you open a new issue for that @jstarks? Looks like it would have a different cause

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

5 participants