Skip to content

Commit f2968e2

Browse files
committed
Fix incorrect format-arg inlining
An edge case with format arguments being wrapped in parentesis
1 parent e8c1b54 commit f2968e2

File tree

4 files changed

+134
-86
lines changed

4 files changed

+134
-86
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

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// run-rustfix
22

3-
#![allow(clippy::eq_op)]
4-
#![allow(clippy::format_in_format_args)]
5-
#![allow(clippy::print_literal)]
6-
#![allow(named_arguments_used_positionally)]
7-
#![allow(unused_variables, unused_imports, unused_macros)]
8-
#![warn(clippy::uninlined_format_args)]
93
#![feature(custom_inner_attributes)]
4+
#![warn(clippy::uninlined_format_args)]
5+
#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)]
6+
#![allow(
7+
clippy::double_parens,
8+
clippy::eq_op,
9+
clippy::format_in_format_args,
10+
clippy::print_literal
11+
)]
1012

1113
macro_rules! no_param_str {
1214
() => {
@@ -129,6 +131,10 @@ fn tester(fn_arg: i32) {
129131
println!("{}", format!("{local_i32}"));
130132
my_println!("{}", local_i32);
131133
my_println_args!("{}", local_i32);
134+
println!("{local_i32}");
135+
#[rustfmt::skip]
136+
println!("{local_i32}");
137+
println!("{local_i32:width$.prec$}");
132138

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

tests/ui/uninlined_format_args.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// run-rustfix
22

3-
#![allow(clippy::eq_op)]
4-
#![allow(clippy::format_in_format_args)]
5-
#![allow(clippy::print_literal)]
6-
#![allow(named_arguments_used_positionally)]
7-
#![allow(unused_variables, unused_imports, unused_macros)]
8-
#![warn(clippy::uninlined_format_args)]
93
#![feature(custom_inner_attributes)]
4+
#![warn(clippy::uninlined_format_args)]
5+
#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)]
6+
#![allow(
7+
clippy::double_parens,
8+
clippy::eq_op,
9+
clippy::format_in_format_args,
10+
clippy::print_literal
11+
)]
1012

1113
macro_rules! no_param_str {
1214
() => {
@@ -132,6 +134,10 @@ fn tester(fn_arg: i32) {
132134
println!("{}", format!("{}", local_i32));
133135
my_println!("{}", local_i32);
134136
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));
135141

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

0 commit comments

Comments
 (0)