Skip to content

G.EXP.01 避免复合条件表达式永远为 truefalse

【级别】 建议

【描述】

复合条件表达式永远为 truefalse 通常是因为代码编写错误。

【反例】

Rust
let x = 2;
// 不符合:该表达式会永远是 false
if (x & 1 == 2) { }

let x = 2;
if (x & 3) < 4 { // 不符合:判断表达式结果永远为 true 
  // do something
}

let y = 0;
if (x < y) && false { // 不符合:判断表达式结果永远为 false 
  // do something
}

【正例】

Rust
let x = 2;
// 符合
if x == 2 { }