G.TYP.BOL.01 不应将布尔值和布尔字面量进行比较

【级别】 要求

【描述】

在 Rust 中,返回为布尔值的表达式或函数值可以直接当作布尔值使用。

总之,使用布尔表达式的时候,要尽可能地简洁明了。

【反例】

#![allow(unused)] fn main() { // 不符合 if x == true {} if y == false {} assert_eq!("a".is_empty(), false); assert_ne!("a".is_empty(), true); }

【正例】

#![allow(unused)] fn main() { // 符合 if x {} if !y {} assert!(!"a".is_empty()); }

【Lint 检测】

lint nameClippy 可检测Rustc 可检测Lint Grouplevel
bool_comparison yesnocomplexitywarn
bool_assert_comparison yesnostylewarn
needless_bool yesnocomplexitywarn
nonminimal_bool yesnocomplexitywarn
needless_bitwise_bool yesnopedanticallow
assertions_on_constants yesnopedanticwarn