rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

unnecessary_lazy_evaluations suggests running `unsafe` code eagerly

Open kornelski opened this issue 1 year ago • 6 comments

Summary

For unsafe {} blocks it matters when they're executed, even if they're cheap.

Lint Name

unnecessary_lazy_evaluations

Reproducer

I tried this code:

is_safe_to_access.then_some(|| unsafe { &a_union.field });

Clippy suggests:

is_safe_to_access.then(unsafe { &a_union.field });

which runs unsafe code unconditionally, bypassing the safety check.

Version

rustc 1.81.0-nightly (bcf94dec5 2024-06-23)
binary: rustc
commit-hash: bcf94dec5ba6838e435902120c0384c360126a26
commit-date: 2024-06-23
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7

Additional Labels

No response

kornelski avatar Aug 06 '24 16:08 kornelski