use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_condition_compared_against_a_literal_is_reported() {
let against_true = codes_of("void f(bool ready) {\n if (ready == true) { return; }\n}\n");
assert_reported(
&against_true,
DiagnosticCode::SimplifyBooleanExpr,
"`ready == true` is `ready`",
);
let against_false = codes_of("void f(bool ready) {\n if (ready == false) { return; }\n}\n");
assert_reported(
&against_false,
DiagnosticCode::SimplifyBooleanExpr,
"`ready == false` is `!ready`",
);
let mirrored = codes_of("void f(bool ready) {\n if (true == ready) { return; }\n}\n");
assert_reported(
&mirrored,
DiagnosticCode::SimplifyBooleanExpr,
"the literal may stand on either side of the comparison",
);
}
#[test]
fn a_condition_that_is_already_the_value_is_quiet() {
let plain = codes_of("void f(bool ready) {\n if (ready) { return; }\n}\n");
assert_silent_about(
&plain,
DiagnosticCode::SimplifyBooleanExpr,
"`if (ready)` is the sentence the rule would rewrite `ready == true` to",
);
let negated = codes_of("void f(bool ready) {\n if (!ready) { return; }\n}\n");
assert_silent_about(
&negated,
DiagnosticCode::SimplifyBooleanExpr,
"`!ready` is already the negation the rule would suggest",
);
}
#[test]
fn a_comparison_against_another_value_is_quiet() {
let against_a_number = codes_of("void f(int count) {\n if (count == 1) { return; }\n}\n");
assert_silent_about(
&against_a_number,
DiagnosticCode::SimplifyBooleanExpr,
"`1` is a number, not the literal `true` this rule is about",
);
let two_values =
codes_of("void f(int left, int right) {\n if (left == right) { return; }\n}\n");
assert_silent_about(
&two_values,
DiagnosticCode::SimplifyBooleanExpr,
"two values compared with each other are already the expression",
);
let an_inequality = codes_of("void f(bool ready) {\n if (ready != true) { return; }\n}\n");
assert_silent_about(
&an_inequality,
DiagnosticCode::SimplifyBooleanExpr,
"`!=` is not one of the four spellings the rule looks for",
);
}
#[test]
fn only_an_if_condition_is_read() {
let in_a_loop = codes_of("void f(bool ready) {\n while (ready == false) { break; }\n}\n");
assert_silent_about(
&in_a_loop,
DiagnosticCode::SimplifyBooleanExpr,
"the rule walks `if` statements, and a `while` head is a different statement",
);
let outside = codes_of("void f(bool ready) {\n bool same = ready == true;\n}\n");
assert_silent_about(
&outside,
DiagnosticCode::SimplifyBooleanExpr,
"a comparison in an initialiser is not a condition",
);
}