Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ fn check_literal(cx: &LateContext<'_>, format_args: &FormatArgs, name: &str) {

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

if let Some((_, index)) = positional_arg_piece_span(piece) {
if let Some((_, index)) = format_arg_piece_span(piece) {
replaced_position.push(index);
}

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

/// Extract Span and its index from the given `piece`, if it's positional argument.
fn positional_arg_piece_span(piece: &FormatArgsPiece) -> Option<(Span, usize)> {
/// Extract Span and its index from the given `piece`
fn format_arg_piece_span(piece: &FormatArgsPiece) -> Option<(Span, usize)> {
match piece {
FormatArgsPiece::Placeholder(FormatPlaceholder {
argument:
FormatArgPosition {
index: Ok(index),
kind: FormatArgPositionKind::Number,
..
},
argument: FormatArgPosition { index: Ok(index), .. },
span: Some(span),
..
}) => Some((*span, *index)),
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/print_literal.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,16 @@ fn issue_14930() {
println!("Hello x is {0:2$.1$}", 0.01, 2, 3);
//~^ print_literal
}

fn issue_15576() {
println!("Hello x is {1:.*}", 5, 0.01);
//~^ print_literal

println!("Hello x is {:.p$}", 0.01, p = 5);
//~^ print_literal

println!(
"Hello name: x is {1:.*} (which {1} with {0} places)", 5, 0.01
);
//~^^ print_literal
}
14 changes: 14 additions & 0 deletions tests/ui/print_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,17 @@ fn issue_14930() {
println!("Hello {0} is {1:3$.2$}", "x", 0.01, 2, 3);
//~^ print_literal
}

fn issue_15576() {
println!("Hello {} is {2:.*}", "x", 5, 0.01);
//~^ print_literal

println!("Hello {} is {:.p$}", "x", 0.01, p = 5);
//~^ print_literal

println!(
"Hello {}: {2} is {3:.*} (which {3} with {1} places)",
"name", 5, "x", 0.01
);
//~^^ print_literal
}
38 changes: 37 additions & 1 deletion tests/ui/print_literal.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,41 @@ LL - println!("Hello {0} is {1:3$.2$}", "x", 0.01, 2, 3);
LL + println!("Hello x is {0:2$.1$}", 0.01, 2, 3);
|

error: aborting due to 22 previous errors
error: literal with an empty format string
--> tests/ui/print_literal.rs:111:36
|
LL | println!("Hello {} is {2:.*}", "x", 5, 0.01);
| ^^^
|
help: try
|
LL - println!("Hello {} is {2:.*}", "x", 5, 0.01);
LL + println!("Hello x is {1:.*}", 5, 0.01);
|

error: literal with an empty format string
--> tests/ui/print_literal.rs:114:36
|
LL | println!("Hello {} is {:.p$}", "x", 0.01, p = 5);
| ^^^
|
help: try
|
LL - println!("Hello {} is {:.p$}", "x", 0.01, p = 5);
LL + println!("Hello x is {:.p$}", 0.01, p = 5);
|

error: literal with an empty format string
--> tests/ui/print_literal.rs:119:9
|
LL | "name", 5, "x", 0.01
| ^^^^^^^^^^^^^^
|
help: try
|
LL - "Hello {}: {2} is {3:.*} (which {3} with {1} places)",
LL + "Hello name: x is {1:.*} (which {1} with {0} places)", 5, 0.01
|

error: aborting due to 25 previous errors