use diagnostics::LisetteDiagnostic;
use syntax::ast::{BinaryOperator, Expression};
use super::helpers::bool_literal;
pub fn check_bool_literal_comparison(
expression: &Expression,
diagnostics: &mut Vec<LisetteDiagnostic>,
) {
let Expression::Binary {
operator,
left,
right,
span,
..
} = expression
else {
return;
};
use BinaryOperator::*;
let is_equal = match operator {
Equal => true,
NotEqual => false,
_ => return,
};
let (other, bool_value) = match (
bool_literal(left.unwrap_parens()),
bool_literal(right.unwrap_parens()),
) {
(Some(b), None) => (right.unwrap_parens(), b),
(None, Some(b)) => (left.unwrap_parens(), b),
_ => return,
};
let Some(other_text) = other.as_dotted_path() else {
return;
};
let negate = bool_value != is_equal;
let replacement = if negate {
format!("!{other_text}")
} else {
other_text
};
diagnostics.push(diagnostics::lint::bool_literal_comparison(
span,
&replacement,
));
}