Skip to content

Commit 1a7127c

Browse files
Fix print_literal suggests wrongly for inline literal following a numbered arg (#15583)
Closes #15576 changelog: [`numbered arg`] fix wrong suggestions for inline literal following a numbered arg
2 parents 44abf28 + b616ba8 commit 1a7127c

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

clippy_lints/src/write.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ fn check_literal(cx: &LateContext<'_>, format_args: &FormatArgs, name: &str) {
537537

538538
sug_span = Some(sug_span.unwrap_or(arg.expr.span).to(arg.expr.span));
539539

540-
if let Some((_, index)) = positional_arg_piece_span(piece) {
540+
if let Some((_, index)) = format_arg_piece_span(piece) {
541541
replaced_position.push(index);
542542
}
543543

@@ -569,16 +569,11 @@ fn check_literal(cx: &LateContext<'_>, format_args: &FormatArgs, name: &str) {
569569
}
570570
}
571571

572-
/// Extract Span and its index from the given `piece`, if it's positional argument.
573-
fn positional_arg_piece_span(piece: &FormatArgsPiece) -> Option<(Span, usize)> {
572+
/// Extract Span and its index from the given `piece`
573+
fn format_arg_piece_span(piece: &FormatArgsPiece) -> Option<(Span, usize)> {
574574
match piece {
575575
FormatArgsPiece::Placeholder(FormatPlaceholder {
576-
argument:
577-
FormatArgPosition {
578-
index: Ok(index),
579-
kind: FormatArgPositionKind::Number,
580-
..
581-
},
576+
argument: FormatArgPosition { index: Ok(index), .. },
582577
span: Some(span),
583578
..
584579
}) => Some((*span, *index)),

tests/ui/print_literal.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,16 @@ fn issue_14930() {
105105
println!("Hello x is {0:2$.1$}", 0.01, 2, 3);
106106
//~^ print_literal
107107
}
108+
109+
fn issue_15576() {
110+
println!("Hello x is {1:.*}", 5, 0.01);
111+
//~^ print_literal
112+
113+
println!("Hello x is {:.p$}", 0.01, p = 5);
114+
//~^ print_literal
115+
116+
println!(
117+
"Hello name: x is {1:.*} (which {1} with {0} places)", 5, 0.01
118+
);
119+
//~^^ print_literal
120+
}

tests/ui/print_literal.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,17 @@ fn issue_14930() {
106106
println!("Hello {0} is {1:3$.2$}", "x", 0.01, 2, 3);
107107
//~^ print_literal
108108
}
109+
110+
fn issue_15576() {
111+
println!("Hello {} is {2:.*}", "x", 5, 0.01);
112+
//~^ print_literal
113+
114+
println!("Hello {} is {:.p$}", "x", 0.01, p = 5);
115+
//~^ print_literal
116+
117+
println!(
118+
"Hello {}: {2} is {3:.*} (which {3} with {1} places)",
119+
"name", 5, "x", 0.01
120+
);
121+
//~^^ print_literal
122+
}

tests/ui/print_literal.stderr

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,41 @@ LL - println!("Hello {0} is {1:3$.2$}", "x", 0.01, 2, 3);
277277
LL + println!("Hello x is {0:2$.1$}", 0.01, 2, 3);
278278
|
279279

280-
error: aborting due to 22 previous errors
280+
error: literal with an empty format string
281+
--> tests/ui/print_literal.rs:111:36
282+
|
283+
LL | println!("Hello {} is {2:.*}", "x", 5, 0.01);
284+
| ^^^
285+
|
286+
help: try
287+
|
288+
LL - println!("Hello {} is {2:.*}", "x", 5, 0.01);
289+
LL + println!("Hello x is {1:.*}", 5, 0.01);
290+
|
291+
292+
error: literal with an empty format string
293+
--> tests/ui/print_literal.rs:114:36
294+
|
295+
LL | println!("Hello {} is {:.p$}", "x", 0.01, p = 5);
296+
| ^^^
297+
|
298+
help: try
299+
|
300+
LL - println!("Hello {} is {:.p$}", "x", 0.01, p = 5);
301+
LL + println!("Hello x is {:.p$}", 0.01, p = 5);
302+
|
303+
304+
error: literal with an empty format string
305+
--> tests/ui/print_literal.rs:119:9
306+
|
307+
LL | "name", 5, "x", 0.01
308+
| ^^^^^^^^^^^^^^
309+
|
310+
help: try
311+
|
312+
LL - "Hello {}: {2} is {3:.*} (which {3} with {1} places)",
313+
LL + "Hello name: x is {1:.*} (which {1} with {0} places)", 5, 0.01
314+
|
315+
316+
error: aborting due to 25 previous errors
281317

0 commit comments

Comments
 (0)