rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.7k stars 12.5k forks source link

Detect boolean literals in logical expressions #127801

Open bvanjoi opened 1 month ago

bvanjoi commented 1 month ago

Code

fn main() {
  let _ = false || true; // expect to throw a warning that it will always return `true`.
  let _ = true || false;
  let _ = false && true; // expect to throw a warning that it will always return `false`.
  let _ = true && false;
}

Current output

compile succeeded, and no warnings were reported.

Desired output

No response

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.81.0-nightly (24d2ac0b5 2024-07-15)
binary: rustc
commit-hash: 24d2ac0b56fcbde13d827745f66e73efb1e17156
commit-date: 2024-07-15
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7

Anything else?

No response

eggyal commented 1 month ago

How often do boolean expressions with multiple literal operands actually arise in practice? Feels rather niche.

ShE3py commented 1 month ago

Clippy already lints those four cases. false && cond & true || cond are often used for quick debugging, so not warning what the user intended to do is a nice thing imo.

tgross35 commented 1 month ago

As noted, clippy::nonminimal_bool covers these. In theory they could be uplifted to regular lints, but this feels more like a cleanliness lint rather than correctness.