Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format_in_format_args lint suggests code that results in alternative behaviour #8643

Closed
rossmacarthur opened this issue Apr 6, 2022 · 2 comments · Fixed by #9349
Closed
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@rossmacarthur
Copy link

Summary

I have some code that looks like this

fn tabulate(devices: Vec<Device>) -> String {
    let mut s = String::from("Vendor ID  Product ID  Name\n");
    s.push_str("---------  ----------  ----------------------------------\n");
    for d in devices {
        s.push_str(&format!(
            "{:<9}  {:<10}  {}",
            format!("0x{:x}", d.vendor_id),
            format!("0x{:x}", d.product_id),
            d.name,
        ));
    }
    s
}

Clippy complains about the inner format! but replacing them with format_args! doesn't align to the columns properly.

Lint Name

format_in_format_args

Reproducer

I tried this code:

fn main() {
    println!(
        "{:>9} {:>10}",
        format!("0x{:x}", 42),
        format!("0x{:x}", 1337)
    );
}

I saw this happen:

warning: `format!` in `println!` args
 --> src/main.rs:2:5
  |
2 | /     println!(
3 | |         "{:>9} {:>10}",
4 | |         format!("0x{:x}", 42),
5 | |         format!("0x{:x}", 1337)
6 | |     );
  | |_____^
  |
  = note: `#[warn(clippy::format_in_format_args)]` on by default
  = help: combine the `format!(..)` arguments with the outer `println!(..)` call
  = help: or consider changing `format!` to `format_args!`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#format_in_format_args

I expected to see this happen:

No lint emitted, or a better suggestion

Version

rustc 1.62.0-nightly (306ba8357 2022-04-05)
binary: rustc
commit-hash: 306ba8357fb36212b7d30efb9eb9e41659ac1445
commit-date: 2022-04-05
host: aarch64-apple-darwin
release: 1.62.0-nightly
LLVM version: 14.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@rossmacarthur rossmacarthur added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 6, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 6, 2022
@hellow554
Copy link
Contributor

hellow554 commented Apr 6, 2022

In this very specific case, you could write:

println!("{:>#9x} {:>#10x}", 42, 1337);

also: that the code with format_args! doesn't work, seems like a bug to me in the rust library. Maybe this issue: rust-lang/rust#46569

@B-Reif
Copy link

B-Reif commented Jun 17, 2022

The documentation in https://doc.rust-lang.org/std/fmt/#fillalignment specifically suggests doing this to work around these kinds of alignment issues:

A good way to ensure padding is applied is to format your input, then pad this resulting string to obtain your output:
println!("Hello {:^15}!", format!("{:?}", Some("hi"))); // => "Hello Some("hi") !"

Then if you actually do this, clippy complains about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants