use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_literal_condition_is_reported() {
let never = codes_of("int main() { if (false) { return 1; } return 0; }\n");
assert_reported(
&never,
DiagnosticCode::UnnecessaryBranch,
"`if (false)` cannot run",
);
let always = codes_of("void f() { if (true) { } }\n");
assert_reported(
&always,
DiagnosticCode::UnnecessaryBranch,
"`if (true)` decides nothing",
);
}
#[test]
fn parentheses_do_not_hide_the_literal() {
let codes = codes_of("void f() { if ((false)) { } }\n");
assert_reported(
&codes,
DiagnosticCode::UnnecessaryBranch,
"`(false)` is still a bare literal",
);
}
#[test]
fn a_name_is_not_a_literal() {
let codes = codes_of("int main() { if (DEBUG) { return 1; } return 0; }\n");
assert_silent_about(
&codes,
DiagnosticCode::UnnecessaryBranch,
"`DEBUG` is a name, and folding it is another layer's question",
);
}
#[test]
fn nothing_is_folded() {
let comparison = codes_of("void f() { if (1 == 1) { } }\n");
assert_silent_about(
&comparison,
DiagnosticCode::UnnecessaryBranch,
"`1 == 1` needs a constant folder",
);
let looped = codes_of("void f() { while (true) { } }\n");
assert_silent_about(
&looped,
DiagnosticCode::UnnecessaryBranch,
"the rule walks `if` statements, and a `while` head is a different statement",
);
}
#[test]
fn prose_and_data_are_not_conditions() {
let commented = codes_of("// if (false) { return; }\nvoid f() { }\n");
assert_silent_about(
&commented,
DiagnosticCode::UnnecessaryBranch,
"a comment is not a statement",
);
let quoted = codes_of("void g(const char*);\nvoid f() { g(\"if (false) { }\"); }\n");
assert_silent_about(
"ed,
DiagnosticCode::UnnecessaryBranch,
"a string literal is not a condition",
);
}