Skip to content

Commit

Permalink
Add format_args_collector internal lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexendoo committed Feb 2, 2023
1 parent 3686b2b commit d1407e5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
.collect(),
))
});
store.register_early_pass(|| Box::new(utils::format_args_collector::FormatArgsCollector));
store.register_late_pass(|_| Box::new(utils::dump_hir::DumpHir));
store.register_late_pass(|_| Box::new(utils::author::Author));
let await_holding_invalid_types = conf.await_holding_invalid_types.clone();
Expand Down Expand Up @@ -862,7 +863,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
let allow_dbg_in_tests = conf.allow_dbg_in_tests;
store.register_late_pass(move |_| Box::new(dbg_macro::DbgMacro::new(allow_dbg_in_tests)));
let allow_print_in_tests = conf.allow_print_in_tests;
store.register_early_pass(move || Box::new(write::Write::new(allow_print_in_tests)));
store.register_late_pass(move |_| Box::new(write::Write::new(allow_print_in_tests)));
let cargo_ignore_publish = conf.cargo_ignore_publish;
store.register_late_pass(move |_| {
Expand Down
23 changes: 23 additions & 0 deletions clippy_lints/src/utils/format_args_collector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use clippy_utils::macros::collect_ast_format_args;
use rustc_ast::{Expr, ExprKind};
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
/// ### What it does
/// Collects [`rustc_ast::FormatArgs`] so that future late passes can call
/// [`clippy_utils::macros::find_format_args`]
pub FORMAT_ARGS_COLLECTOR,
internal_warn,
"collects `format_args` AST nodes for use in later lints"
}

declare_lint_pass!(FormatArgsCollector => [FORMAT_ARGS_COLLECTOR]);

impl EarlyLintPass for FormatArgsCollector {
fn check_expr(&mut self, _: &EarlyContext<'_>, expr: &Expr) {
if let ExprKind::FormatArgs(args) = &expr.kind {
collect_ast_format_args(expr.span, args);
}
}
}
1 change: 1 addition & 0 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod author;
pub mod conf;
pub mod dump_hir;
pub mod format_args_collector;
#[cfg(feature = "internal")]
pub mod internal_lints;
12 changes: 2 additions & 10 deletions clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::macros::{
find_format_args, format_arg_removal_span, populate_ast_format_args, root_macro_call_first_node, MacroCall,
};
use clippy_utils::macros::{find_format_args, format_arg_removal_span, root_macro_call_first_node, MacroCall};
use clippy_utils::source::{expand_past_previous_comma, snippet_opt};
use clippy_utils::{is_in_cfg_test, is_in_test_function};
use rustc_ast::token::LitKind;
use rustc_ast::{FormatArgPosition, FormatArgs, FormatArgsPiece, FormatOptions, FormatPlaceholder, FormatTrait};
use rustc_errors::Applicability;
use rustc_hir::{Expr, Impl, Item, ItemKind};
use rustc_lint::{EarlyLintPass, LateContext, LateLintPass, LintContext};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{sym, BytePos};

Expand Down Expand Up @@ -260,12 +258,6 @@ impl_lint_pass!(Write => [
WRITE_LITERAL,
]);

impl EarlyLintPass for Write {
fn check_expr(&mut self, _: &rustc_lint::EarlyContext<'_>, expr: &rustc_ast::Expr) {
populate_ast_format_args(expr);
}
}

impl<'tcx> LateLintPass<'tcx> for Write {
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
if is_debug_impl(cx, item) {
Expand Down
16 changes: 7 additions & 9 deletions clippy_utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_span::hygiene::{self, MacroKind, SyntaxContext};
use rustc_span::{sym, BytePos, ExpnData, ExpnId, ExpnKind, Pos, Span, SpanData, Symbol};
use std::cell::RefCell;
use std::iter::{once, zip};
use std::ops::{ControlFlow, Deref};
use std::ops::ControlFlow;
use std::sync::atomic::{AtomicBool, Ordering};

const FORMAT_MACRO_DIAG_ITEMS: &[Symbol] = &[
Expand Down Expand Up @@ -361,14 +361,12 @@ thread_local! {
};
}

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

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

0 comments on commit d1407e5

Please sign in to comment.