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

FN: useless format: formatting PathBuf.display() #3021

Open
matthiaskrgr opened this issue Aug 9, 2018 · 5 comments
Open

FN: useless format: formatting PathBuf.display() #3021

matthiaskrgr opened this issue Aug 9, 2018 · 5 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@matthiaskrgr
Copy link
Member

use std::path::PathBuf;

fn main() {
    let x = PathBuf::from("/bar/foo/qux");
    let y = format!("{}", x.display()); // can just be   let y = x.display();
    println!("{}", y);
}
@oli-obk
Copy link
Contributor

oli-obk commented Aug 9, 2018

We could generally lint the use of the result of format! as an argumennt in another formatting

@matthiaskrgr
Copy link
Member Author

Mmh, this might be tricky.

IIRC it is faster to have a single print() than multiple ones (due to less syscalls) so when I need to print large strings, I usually construct them first with a/multiple format!()s and then print a single String in the end.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 9, 2018

sure, but you don't need to format!, you can just format_args! it and print that at the end without ever allocating a String

@matthiaskrgr
Copy link
Member Author

Ah, I'll have to read up on this!

@phansch phansch added the C-bug Category: Clippy is not doing the correct thing label Dec 11, 2018
@matthiaskrgr matthiaskrgr added the I-false-negative Issue: The lint should have been triggered on code, but wasn't label Dec 18, 2020
@adilhasan
Copy link

Hello, I stumbled upon a similar problem and figured out a way to print a PathBuf. Here's my (shaky) reasoning:

  • I can convert from PathBuf to a str using the to_str function.
  • The to_str function returns an Option str, so I need to unwrap it to get the str
  • Then I can print out the path:
    println!("path {}", x.to_str().unwrap());

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-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

4 participants