Skip to content

Commit 30fcc93

Browse files
authored
feat(lints): refine help message when formatting error in tracing::event (#14752)
Signed-off-by: Bugen Zhao <[email protected]>
1 parent 6b7f37a commit 30fcc93

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lints/src/format_error.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use clippy_utils::macros::{
1717
find_format_arg_expr, find_format_args, is_format_macro, macro_backtrace,
1818
};
1919
use clippy_utils::ty::implements_trait;
20-
use clippy_utils::{is_in_cfg_test, is_in_test_function, is_trait_method, match_function_call};
20+
use clippy_utils::{
21+
is_in_cfg_test, is_in_test_function, is_trait_method, match_def_path, match_function_call,
22+
};
2123
use rustc_ast::FormatArgsPiece;
2224
use rustc_hir::{Expr, ExprKind};
2325
use rustc_lint::{LateContext, LateLintPass};
@@ -60,6 +62,7 @@ impl_lint_pass!(FormatError => [FORMAT_ERROR]);
6062

6163
const TRACING_FIELD_DEBUG: [&str; 3] = ["tracing_core", "field", "debug"];
6264
const TRACING_FIELD_DISPLAY: [&str; 3] = ["tracing_core", "field", "display"];
65+
const TRACING_MACRO_EVENT: [&str; 3] = ["tracing", "macros", "event"];
6366

6467
impl<'tcx> LateLintPass<'tcx> for FormatError {
6568
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
@@ -77,6 +80,9 @@ impl<'tcx> LateLintPass<'tcx> for FormatError {
7780
}
7881

7982
// `{}`, `{:?}` in format macros.
83+
let in_tracing_macro_event = macro_backtrace(expr.span)
84+
.any(|macro_call| match_def_path(cx, macro_call.def_id, &TRACING_MACRO_EVENT));
85+
8086
for macro_call in macro_backtrace(expr.span) {
8187
if is_format_macro(cx, macro_call.def_id)
8288
&& let Some(format_args) = find_format_args(cx, expr, macro_call.expn)
@@ -87,7 +93,11 @@ impl<'tcx> LateLintPass<'tcx> for FormatError {
8793
&& let Some(arg) = format_args.arguments.all_args().get(index)
8894
&& let Ok(arg_expr) = find_format_arg_expr(expr, arg)
8995
{
90-
check_fmt_arg(cx, arg_expr);
96+
if in_tracing_macro_event {
97+
check_fmt_arg_in_tracing_event(cx, arg_expr);
98+
} else {
99+
check_fmt_arg(cx, arg_expr);
100+
}
91101
}
92102
}
93103
}
@@ -111,6 +121,17 @@ fn check_fmt_arg(cx: &LateContext<'_>, arg_expr: &Expr<'_>) {
111121
);
112122
}
113123

124+
fn check_fmt_arg_in_tracing_event(cx: &LateContext<'_>, arg_expr: &Expr<'_>) {
125+
// TODO: replace `<error>` with the actual code snippet.
126+
check_arg(
127+
cx,
128+
arg_expr,
129+
arg_expr.span,
130+
"consider importing `thiserror_ext::AsReport` and recording the error as a field
131+
with `error = %<error>.as_report()` instead",
132+
);
133+
}
134+
114135
fn check_to_string_call(cx: &LateContext<'_>, receiver: &Expr<'_>, to_string_span: Span) {
115136
check_arg(
116137
cx,

lints/ui/format_error.stderr

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,17 @@ error: should not format error directly
118118
LL | info!("{}", err);
119119
| ^^^
120120
|
121-
= help: consider importing `thiserror_ext::AsReport` and using `.as_report()` instead
121+
= help: consider importing `thiserror_ext::AsReport` and recording the error as a field
122+
with `error = %<error>.as_report()` instead
122123

123124
error: should not format error directly
124125
--> $DIR/format_error.rs:32:20
125126
|
126127
LL | my_info!("{}", err);
127128
| ^^^
128129
|
129-
= help: consider importing `thiserror_ext::AsReport` and using `.as_report()` instead
130+
= help: consider importing `thiserror_ext::AsReport` and recording the error as a field
131+
with `error = %<error>.as_report()` instead
130132

131133
error: should not format error directly
132134
--> $DIR/format_error.rs:34:29
@@ -166,7 +168,8 @@ error: should not format error directly
166168
LL | info!(%err, "{}", err);
167169
| ^^^
168170
|
169-
= help: consider importing `thiserror_ext::AsReport` and using `.as_report()` instead
171+
= help: consider importing `thiserror_ext::AsReport` and recording the error as a field
172+
with `error = %<error>.as_report()` instead
170173

171174
error: should not format error directly
172175
--> $DIR/format_error.rs:39:5

0 commit comments

Comments
 (0)