Return a value from find_format_args instead of using a callback

This commit is contained in:
Alex Macleod 2023-09-01 17:05:30 +00:00
parent b788addfcc
commit c29de92d85
9 changed files with 126 additions and 129 deletions

View file

@ -1,12 +1,15 @@
use clippy_utils::macros::collect_ast_format_args;
use clippy_utils::macros::AST_FORMAT_ARGS;
use clippy_utils::source::snippet_opt;
use itertools::Itertools;
use rustc_ast::{Expr, ExprKind, FormatArgs};
use rustc_ast::{Crate, Expr, ExprKind, FormatArgs};
use rustc_data_structures::fx::FxHashMap;
use rustc_lexer::{tokenize, TokenKind};
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::hygiene;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{hygiene, Span};
use std::iter::once;
use std::mem;
use std::rc::Rc;
declare_clippy_lint! {
/// ### What it does
@ -17,7 +20,12 @@ declare_clippy_lint! {
"collects `format_args` AST nodes for use in later lints"
}
declare_lint_pass!(FormatArgsCollector => [FORMAT_ARGS_COLLECTOR]);
#[derive(Default)]
pub struct FormatArgsCollector {
format_args: FxHashMap<Span, Rc<FormatArgs>>,
}
impl_lint_pass!(FormatArgsCollector => [FORMAT_ARGS_COLLECTOR]);
impl EarlyLintPass for FormatArgsCollector {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
@ -26,9 +34,17 @@ impl EarlyLintPass for FormatArgsCollector {
return;
}
collect_ast_format_args(expr.span, args);
self.format_args
.insert(expr.span.with_parent(None), Rc::new((**args).clone()));
}
}
fn check_crate_post(&mut self, _: &EarlyContext<'_>, _: &Crate) {
AST_FORMAT_ARGS.with(|ast_format_args| {
let result = ast_format_args.set(mem::take(&mut self.format_args));
debug_assert!(result.is_ok());
});
}
}
/// Detects if the format string or an argument has its span set by a proc macro to something inside