use super::{Check, CheckContext, DiagnosticCode};
pub struct UseAuto;
impl Check for UseAuto {
const CODES: &'static [DiagnosticCode] = &[DiagnosticCode::UseAuto];
fn check(context: &mut CheckContext<'_>) {
use cpp_parser::CppAstNode;
for declaration in context
.own::<cpp_parser::CppDeclaration>()
.collect::<Vec<_>>()
{
let Some(specifiers) = declaration.child::<cpp_parser::CppDeclSpecifierSeq>() else {
continue;
};
let written = context.node_text(specifiers.syntax()).trim_end();
let spelling = if written.ends_with("::const_iterator") {
"::const_iterator"
} else if written.ends_with("::iterator") {
"::iterator"
} else {
continue;
};
let mut named: Option<(String, cpp_parser::SourceRange)> = None;
for declarator in declaration.children::<cpp_parser::CppInitDeclarator>() {
let Some(name) = declarator.child::<cpp_parser::CppDeclarator>() else {
continue;
};
let text = context.node_text(name.syntax()).trim();
let is_one_name = !text.is_empty()
&& text
.chars()
.all(|character| character.is_alphanumeric() || character == '_');
if is_one_name {
named = Some((
text.to_string(),
cpp_parser::source_range(name.syntax().text_range()),
));
break;
}
}
let Some((name, range)) = named else {
continue;
};
context.add(
DiagnosticCode::UseAuto,
cpp_parser::source_range(specifiers.syntax().text_range()),
t!("%{spelling} %{name}", spelling = spelling, name = name),
t!(
"`%{name}` is declared by its full iterator spelling — write `auto %{name}`, which is the same type and \
cannot disagree with the initialiser",
name = name
),
);
let _ = range;
}
}
}