Skip to content

Commit d876352

Browse files
committed
Handle panic! inline_format-arg before ed2021
1 parent 2c8e473 commit d876352

File tree

6 files changed

+1212
-74
lines changed

6 files changed

+1212
-74
lines changed

clippy_lints/src/format_args.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::macros::FormatParamKind::{Implicit, Named, Numbered, Starred};
3-
use clippy_utils::macros::{is_format_macro, FormatArgsExpn, FormatParam, FormatParamUsage};
3+
use clippy_utils::macros::{is_format_macro, is_panic, FormatArgsExpn, FormatParam, FormatParamUsage};
44
use clippy_utils::source::snippet_opt;
55
use clippy_utils::ty::implements_trait;
66
use clippy_utils::{is_diag_trait_item, meets_msrv, msrvs};
@@ -13,6 +13,8 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment};
1313
use rustc_middle::ty::Ty;
1414
use rustc_semver::RustcVersion;
1515
use rustc_session::{declare_tool_lint, impl_lint_pass};
16+
use rustc_span::def_id::DefId;
17+
use rustc_span::edition::Edition::Edition2021;
1618
use rustc_span::{sym, ExpnData, ExpnKind, Span, Symbol};
1719

1820
declare_clippy_lint! {
@@ -149,7 +151,7 @@ impl<'tcx> LateLintPass<'tcx> for FormatArgs {
149151
check_to_string_in_format_args(cx, name, arg.param.value);
150152
}
151153
if meets_msrv(self.msrv, msrvs::FORMAT_ARGS_CAPTURE) {
152-
check_uninlined_args(cx, &format_args, outermost_expn_data.call_site);
154+
check_uninlined_args(cx, &format_args, outermost_expn_data.call_site, macro_def_id);
153155
}
154156
}
155157
}
@@ -158,10 +160,14 @@ impl<'tcx> LateLintPass<'tcx> for FormatArgs {
158160
extract_msrv_attr!(LateContext);
159161
}
160162

161-
fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_site: Span) {
163+
fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_site: Span, def_id: DefId) {
162164
if args.format_string.span.from_expansion() {
163165
return;
164166
}
167+
if cx.sess().opts.edition < Edition2021 && is_panic(cx, def_id) {
168+
// panic! before 2021 edition considers a single string argument as non-format
169+
return;
170+
}
165171

166172
let mut fixes = Vec::new();
167173
// If any of the arguments are referenced by an index number,

tests/ui/uninlined_format_args.fixed renamed to tests/ui/uninlined_format_args.edition2018.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: edition2018 edition2021
2+
// [edition2018] edition:2018
3+
// [edition2021] edition:2021
14
// aux-build:proc_macro_with_span.rs
25
// run-rustfix
36
#![feature(custom_inner_attributes)]
@@ -150,6 +153,19 @@ fn tester(fn_arg: i32) {
150153

151154
println!(with_span!("{0} {1}" "{1} {0}"), local_i32, local_f64);
152155
println!("{}", with_span!(span val));
156+
157+
if local_i32 > 0 {
158+
panic!("p1 {local_i32}");
159+
}
160+
if local_i32 > 0 {
161+
panic!("p2 {local_i32}");
162+
}
163+
if local_i32 > 0 {
164+
panic!("p3 {local_i32}");
165+
}
166+
if local_i32 > 0 {
167+
panic!("p4 {local_i32}");
168+
}
153169
}
154170

155171
fn main() {

0 commit comments

Comments
 (0)