use cpp_parser::CppAstNode;
use super::{Check, CheckContext, DiagnosticCode};
pub struct SimplifyBooleanExpr;
impl Check for SimplifyBooleanExpr {
const CODES: &'static [DiagnosticCode] = &[DiagnosticCode::SimplifyBooleanExpr];
fn check(context: &mut CheckContext<'_>) {
for statement in context.own::<cpp_parser::CppIfStat>().collect::<Vec<_>>() {
let Some(condition) = statement.get_condition() else {
continue;
};
let written = context.node_text(condition.syntax());
let against_a_literal = ["== true", "== false", "true ==", "false =="]
.iter()
.any(|spelling| written.contains(spelling));
if !against_a_literal {
continue;
}
context.add(
DiagnosticCode::SimplifyBooleanExpr,
cpp_parser::source_range(condition.syntax().text_range()),
written.to_string(),
format!(
"`{written}` compares against a literal that is already the value — write the expression, or its negation: `if ({})` or `if (!({}))`",
written
.replace("== true", "")
.replace("== false", "")
.trim(),
written.replace("== true", "").replace("== false", "").trim()
),
);
}
}
}