-
Notifications
You must be signed in to change notification settings - Fork 1.7k
unnecessary_lazy_evaluations
incorrectly suggest to eagerly evaluate unsafe methods
#13438
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
Comments
unnecessary_lazy_evaluations
incorrectly suggest to evaluate unsafe methodsunnecessary_lazy_evaluations
incorrectly suggest to eagerly evaluate unsafe methods
Same as #13226 ? |
Yes, it is a duplicate. |
#13226 has a similar title, but based on the discussion there it really sounds more like it's specifically about the lint always warning field accesses without considering that it might be unions, whereas this is about const method calls being warned without considering that they might be marked unsafe. So I wouldn't say that this is a duplicate. Or at least, a fix for that other issue wouldn't necessarily fix this one.
Note that this case is already checked for by the lint. |
I meant, |
Fair, but I still don't really think this is a duplicate or in any way related to the linked issue. They're both about how the lint interacts with unsafe code, but these two issues are about very different operations that are flagged (union access vs unsafe method calls). Checking if the lazy evaluation is in an unsafe block leads to unnecessary false negatives (e.g. if you're in an |
Summary
Sometimes, we want to lazily evaluate some unsafe methods because eager evaluation is undefined behaviour (e.g. if we eagerly dereference a pointer)
Lint Name
unnecessary_lazy_evaluations
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
No warning because our code safe only because we call unsafe method
NonNull::as_ref
only if we havetrue
inis_valid
variable. Eagerly evaluatingas_ref
would be undefined behaviour because calling it only safe if there are no other living references and we are sure that it points to valid object.Version
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-pc-windows-msvc
release: 1.81.0
LLVM version: 18.1.7
Additional Labels
No response
The text was updated successfully, but these errors were encountered: