Skip to content

Commit 5610d22

Browse files
committed
Re-enable uninlined_format_args on multiline format!
But do not display the code suggestion which can be sometimes completely broken (fortunately when applied it's valid)
1 parent efadb55 commit 5610d22

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

clippy_lints/src/format_args.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use clippy_utils::source::snippet_opt;
99
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
1010
use if_chain::if_chain;
1111
use itertools::Itertools;
12-
use rustc_errors::Applicability;
12+
use rustc_errors::{
13+
Applicability,
14+
SuggestionStyle::{CompletelyHidden, ShowCode},
15+
};
1316
use rustc_hir::{Expr, ExprKind, HirId, QPath};
1417
use rustc_lint::{LateContext, LateLintPass, LintContext};
1518
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
@@ -286,18 +289,22 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si
286289
return;
287290
}
288291

289-
// Temporarily ignore multiline spans: https://github.com/rust-lang/rust/pull/102729#discussion_r988704308
290-
if fixes.iter().any(|(span, _)| cx.sess().source_map().is_multiline(*span)) {
291-
return;
292-
}
292+
// multiline span display suggestion is sometimes broken: https://github.com/rust-lang/rust/pull/102729#discussion_r988704308
293+
// in those cases, make the code suggestion hidden
294+
let multiline_fix = fixes.iter().any(|(span, _)| cx.sess().source_map().is_multiline(*span));
293295

294296
span_lint_and_then(
295297
cx,
296298
UNINLINED_FORMAT_ARGS,
297299
call_site,
298300
"variables can be used directly in the `format!` string",
299301
|diag| {
300-
diag.multipart_suggestion("change this to", fixes, Applicability::MachineApplicable);
302+
diag.multipart_suggestion_with_style(
303+
"change this to",
304+
fixes,
305+
Applicability::MachineApplicable,
306+
if multiline_fix { CompletelyHidden } else { ShowCode },
307+
);
301308
},
302309
);
303310
}

tests/ui/uninlined_format_args.fixed

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ fn tester(fn_arg: i32) {
4444
println!("val='{local_i32}'"); // space+tab
4545
println!("val='{local_i32}'"); // tab+space
4646
println!(
47-
"val='{
48-
}'",
49-
local_i32
47+
"val='{local_i32}'"
5048
);
5149
println!("{local_i32}");
5250
println!("{fn_arg}");
@@ -110,8 +108,7 @@ fn tester(fn_arg: i32) {
110108
println!("{local_f64:width$.prec$}");
111109
println!("{local_f64:width$.prec$} {local_f64} {width} {prec}");
112110
println!(
113-
"{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}",
114-
local_i32, width, prec,
111+
"{local_i32:width$.prec$} {local_i32:prec$.width$} {width:local_i32$.prec$} {width:prec$.local_i32$} {prec:local_i32$.width$} {prec:width$.local_i32$}",
115112
);
116113
println!(
117114
"{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$} {3}",
@@ -142,9 +139,7 @@ fn tester(fn_arg: i32) {
142139
println!(no_param_str!(), local_i32);
143140

144141
println!(
145-
"{}",
146-
// comment with a comma , in it
147-
val,
142+
"{val}",
148143
);
149144
println!("{val}");
150145

tests/ui/uninlined_format_args.stderr

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ LL - println!("val='{ }'", local_i32); // tab+space
5959
LL + println!("val='{local_i32}'"); // tab+space
6060
|
6161

62+
error: variables can be used directly in the `format!` string
63+
--> $DIR/uninlined_format_args.rs:46:5
64+
|
65+
LL | / println!(
66+
LL | | "val='{
67+
LL | | }'",
68+
LL | | local_i32
69+
LL | | );
70+
| |_____^
71+
6272
error: variables can be used directly in the `format!` string
6373
--> $DIR/uninlined_format_args.rs:51:5
6474
|
@@ -767,6 +777,15 @@ LL - println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec);
767777
LL + println!("{local_f64:width$.prec$} {local_f64} {width} {prec}");
768778
|
769779

780+
error: variables can be used directly in the `format!` string
781+
--> $DIR/uninlined_format_args.rs:112:5
782+
|
783+
LL | / println!(
784+
LL | | "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}",
785+
LL | | local_i32, width, prec,
786+
LL | | );
787+
| |_____^
788+
770789
error: variables can be used directly in the `format!` string
771790
--> $DIR/uninlined_format_args.rs:123:5
772791
|
@@ -815,6 +834,16 @@ LL - println!("{}", format!("{}", local_i32));
815834
LL + println!("{}", format!("{local_i32}"));
816835
|
817836

837+
error: variables can be used directly in the `format!` string
838+
--> $DIR/uninlined_format_args.rs:144:5
839+
|
840+
LL | / println!(
841+
LL | | "{}",
842+
LL | | // comment with a comma , in it
843+
LL | | val,
844+
LL | | );
845+
| |_____^
846+
818847
error: variables can be used directly in the `format!` string
819848
--> $DIR/uninlined_format_args.rs:149:5
820849
|
@@ -875,5 +904,5 @@ LL - println!("expand='{}'", local_i32);
875904
LL + println!("expand='{local_i32}'");
876905
|
877906

878-
error: aborting due to 73 previous errors
907+
error: aborting due to 76 previous errors
879908

0 commit comments

Comments
 (0)