use cpp_parser::CppAstNode;
use super::{Check, CheckContext, DiagnosticCode};
pub struct BracesAroundStatements;
impl Check for BracesAroundStatements {
const CODES: &'static [DiagnosticCode] = &[DiagnosticCode::BracesAroundStatements];
fn check(context: &mut CheckContext<'_>) {
for statement in context.own::<cpp_parser::CppIfStat>().collect::<Vec<_>>() {
let Some(then_branch) = statement.get_then_branch() else {
continue;
};
let written = context.node_text(then_branch.syntax()).trim_start();
if written.starts_with('{') {
continue;
}
context.add(
DiagnosticCode::BracesAroundStatements,
cpp_parser::source_range(then_branch.syntax().text_range()),
written.to_string(),
"a body that is one statement should be a block — `if (…) { … }` — because `if (a) if (b) f(); else g();` binds the `else` to the inner `if`"
.to_string(),
);
}
}
}