Skip to content

Commit 7a05faf

Browse files
committed
Fix incorrect format-arg inlining
An edge case with format arguments being wrapped in parentesis
1 parent 425e1ea commit 7a05faf

File tree

4 files changed

+132
-76
lines changed

4 files changed

+132
-76
lines changed

clippy_lints/src/format_args.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si
199199
fn check_one_arg(cx: &LateContext<'_>, param: &FormatParam<'_>, fixes: &mut Vec<(Span, String)>) -> bool {
200200
if matches!(param.kind, Implicit | Starred | Named(_) | Numbered)
201201
&& let ExprKind::Path(QPath::Resolved(None, path)) = param.value.kind
202-
&& let Path { span, segments, .. } = path
202+
&& let Path { segments, .. } = path
203203
&& let [segment] = segments
204204
{
205205
let replacement = match param.usage {
@@ -208,7 +208,7 @@ fn check_one_arg(cx: &LateContext<'_>, param: &FormatParam<'_>, fixes: &mut Vec<
208208
FormatParamUsage::Precision => format!(".{}$", segment.ident.name),
209209
};
210210
fixes.push((param.span, replacement));
211-
let arg_span = expand_past_previous_comma(cx, *span);
211+
let arg_span = expand_past_previous_comma(cx, param.value.span);
212212
fixes.push((arg_span, String::new()));
213213
true // successful inlining, continue checking
214214
} else {

tests/ui/uninlined_format_args.fixed

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// run-rustfix
2+
23
#![feature(custom_inner_attributes)]
34
#![warn(clippy::uninlined_format_args)]
45
#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)]
5-
#![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)]
6+
#![allow(
7+
clippy::double_parens,
8+
clippy::eq_op,
9+
clippy::format_in_format_args,
10+
clippy::print_literal
11+
)]
612

713
macro_rules! no_param_str {
814
() => {
@@ -125,6 +131,10 @@ fn tester(fn_arg: i32) {
125131
println!("{}", format!("{local_i32}"));
126132
my_println!("{}", local_i32);
127133
my_println_args!("{}", local_i32);
134+
println!("{local_i32}");
135+
#[rustfmt::skip]
136+
println!("{local_i32}");
137+
println!("{local_i32:width$.prec$}");
128138

129139
// these should NOT be modified by the lint
130140
println!(concat!("nope ", "{}"), local_i32);

tests/ui/uninlined_format_args.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// run-rustfix
2+
23
#![feature(custom_inner_attributes)]
34
#![warn(clippy::uninlined_format_args)]
45
#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)]
5-
#![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)]
6+
#![allow(
7+
clippy::double_parens,
8+
clippy::eq_op,
9+
clippy::format_in_format_args,
10+
clippy::print_literal
11+
)]
612

713
macro_rules! no_param_str {
814
() => {
@@ -128,6 +134,10 @@ fn tester(fn_arg: i32) {
128134
println!("{}", format!("{}", local_i32));
129135
my_println!("{}", local_i32);
130136
my_println_args!("{}", local_i32);
137+
println!("{}", (local_i32));
138+
#[rustfmt::skip]
139+
println!("{}", ((local_i32)));
140+
println!("{0:1$.2$}", (local_i32), (width), (prec));
131141

132142
// these should NOT be modified by the lint
133143
println!(concat!("nope ", "{}"), local_i32);

0 commit comments

Comments
 (0)