Skip to content

Commit 01271d9

Browse files
committed
Add format_args_collector internal lint
1 parent 3686b2b commit 01271d9

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
528528
.collect(),
529529
))
530530
});
531+
store.register_early_pass(|| Box::new(utils::format_args_collector::FormatArgsCollector));
531532
store.register_late_pass(|_| Box::new(utils::dump_hir::DumpHir));
532533
store.register_late_pass(|_| Box::new(utils::author::Author));
533534
let await_holding_invalid_types = conf.await_holding_invalid_types.clone();
@@ -862,7 +863,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
862863
let allow_dbg_in_tests = conf.allow_dbg_in_tests;
863864
store.register_late_pass(move |_| Box::new(dbg_macro::DbgMacro::new(allow_dbg_in_tests)));
864865
let allow_print_in_tests = conf.allow_print_in_tests;
865-
store.register_early_pass(move || Box::new(write::Write::new(allow_print_in_tests)));
866866
store.register_late_pass(move |_| Box::new(write::Write::new(allow_print_in_tests)));
867867
let cargo_ignore_publish = conf.cargo_ignore_publish;
868868
store.register_late_pass(move |_| {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use clippy_utils::macros::collect_ast_format_args;
2+
use rustc_ast::{Expr, ExprKind};
3+
use rustc_lint::{EarlyContext, EarlyLintPass};
4+
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
6+
declare_clippy_lint! {
7+
/// ### What it does
8+
/// Collects [`rustc_ast::FormatArgs`] so that future late passes can call
9+
/// [`clippy_utils::macros::find_format_args`]
10+
pub FORMAT_ARGS_COLLECTOR,
11+
internal_warn,
12+
"helper to dump info about code"
13+
}
14+
15+
declare_lint_pass!(FormatArgsCollector => [FORMAT_ARGS_COLLECTOR]);
16+
17+
impl EarlyLintPass for FormatArgsCollector {
18+
fn check_expr(&mut self, _: &EarlyContext<'_>, expr: &Expr) {
19+
if let ExprKind::FormatArgs(args) = &expr.kind {
20+
collect_ast_format_args(expr.span, args);
21+
}
22+
}
23+
}

clippy_lints/src/utils/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod author;
22
pub mod conf;
33
pub mod dump_hir;
4+
pub mod format_args_collector;
45
#[cfg(feature = "internal")]
56
pub mod internal_lints;

clippy_lints/src/write.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
2-
use clippy_utils::macros::{
3-
find_format_args, format_arg_removal_span, populate_ast_format_args, root_macro_call_first_node, MacroCall,
4-
};
2+
use clippy_utils::macros::{find_format_args, format_arg_removal_span, root_macro_call_first_node, MacroCall};
53
use clippy_utils::source::{expand_past_previous_comma, snippet_opt};
64
use clippy_utils::{is_in_cfg_test, is_in_test_function};
75
use rustc_ast::token::LitKind;
86
use rustc_ast::{FormatArgPosition, FormatArgs, FormatArgsPiece, FormatOptions, FormatPlaceholder, FormatTrait};
97
use rustc_errors::Applicability;
108
use rustc_hir::{Expr, Impl, Item, ItemKind};
11-
use rustc_lint::{EarlyLintPass, LateContext, LateLintPass, LintContext};
9+
use rustc_lint::{LateContext, LateLintPass, LintContext};
1210
use rustc_session::{declare_tool_lint, impl_lint_pass};
1311
use rustc_span::{sym, BytePos};
1412

@@ -260,12 +258,6 @@ impl_lint_pass!(Write => [
260258
WRITE_LITERAL,
261259
]);
262260

263-
impl EarlyLintPass for Write {
264-
fn check_expr(&mut self, _: &rustc_lint::EarlyContext<'_>, expr: &rustc_ast::Expr) {
265-
populate_ast_format_args(expr);
266-
}
267-
}
268-
269261
impl<'tcx> LateLintPass<'tcx> for Write {
270262
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
271263
if is_debug_impl(cx, item) {

clippy_utils/src/macros.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_span::hygiene::{self, MacroKind, SyntaxContext};
1919
use rustc_span::{sym, BytePos, ExpnData, ExpnId, ExpnKind, Pos, Span, SpanData, Symbol};
2020
use std::cell::RefCell;
2121
use std::iter::{once, zip};
22-
use std::ops::{ControlFlow, Deref};
22+
use std::ops::ControlFlow;
2323
use std::sync::atomic::{AtomicBool, Ordering};
2424

2525
const FORMAT_MACRO_DIAG_ITEMS: &[Symbol] = &[
@@ -361,14 +361,12 @@ thread_local! {
361361
};
362362
}
363363

364-
/// Record [`rustc_ast::FormatArgs`] for use in late lint passes, this only needs to be called by
365-
/// one lint pass.
366-
pub fn populate_ast_format_args(expr: &rustc_ast::Expr) {
367-
if let rustc_ast::ExprKind::FormatArgs(args) = &expr.kind {
368-
AST_FORMAT_ARGS.with(|ast_format_args| {
369-
ast_format_args.borrow_mut().insert(expr.span, args.deref().clone());
370-
});
371-
}
364+
/// Record [`rustc_ast::FormatArgs`] for use in late lint passes, this should only be called by
365+
/// `FormatArgsCollector`
366+
pub fn collect_ast_format_args(span: Span, format_args: &FormatArgs) {
367+
AST_FORMAT_ARGS.with(|ast_format_args| {
368+
ast_format_args.borrow_mut().insert(span, format_args.clone());
369+
});
372370
}
373371

374372
/// Calls `callback` with an AST [`FormatArgs`] node if one is found

0 commit comments

Comments
 (0)