use cpp_parser::CppAstNode;
use super::{Check, CheckContext, DiagnosticCode};
pub struct UseNullptr;
impl Check for UseNullptr {
const CODES: &'static [DiagnosticCode] = &[DiagnosticCode::UseNullptr];
fn check(context: &mut CheckContext<'_>) {
for expression in context.own::<cpp_parser::CppExpr>().collect::<Vec<_>>() {
if context.node_text(expression.syntax()) != "NULL" {
continue;
}
let range = expression.syntax().text_range();
let offset = usize::from(range.start());
context.add(
DiagnosticCode::UseNullptr,
cpp_parser::source_range(range),
"NULL".to_string(),
format!(
"`NULL` is `0` in every standard header, so this picks an `int` overload where one exists — write `nullptr`, which has the pointer type.{}{}",
context
.source
.get(offset..usize::from(range.end()))
.map(|_| "")
.unwrap_or_default(),
""
),
);
}
}
}