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: struct with fmt::Display and format!() ? #3156

Open
matthiaskrgr opened this issue Sep 9, 2018 · 1 comment
Open

FN useless format: struct with fmt::Display and format!() ? #3156

matthiaskrgr opened this issue Sep 9, 2018 · 1 comment
Labels
A-lint Area: New lints I-false-negative Issue: The lint should have been triggered on code, but wasn't L-pedantic Lint: Belongs in the pedantic lint group

Comments

@matthiaskrgr
Copy link
Member

struct StructX {
    x: u32,
}

impl std::fmt::Display for StructX {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "hello {}", self.x)?;
        Ok(())
    }
}

/*
 * note that we have 
 * impl<T> ToString for T
 *    where T: Display + ?Sized
 * which means to_string() will be available for StructX
 */

fn main() {
    let y = StructX { x: 4, };
    let s = format!("{}", y);
    // due to the impl, the above line can be
    // let s = y.to_string();
    println!("{}", s.len());
}

In godbolt, format!("{}", y) and y.to_string() didn't seem to differ, yet I think that .to_string() is a bit clearer since we don't perform any special formatting here.
Thoughts?

@matthiaskrgr matthiaskrgr added the I-false-negative Issue: The lint should have been triggered on code, but wasn't label Dec 18, 2020
@camsteffen camsteffen added A-lint Area: New lints L-pedantic Lint: Belongs in the pedantic lint group labels Nov 15, 2021
@camsteffen
Copy link
Contributor

Marking as pedantic for reasons discussed in #3753

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints I-false-negative Issue: The lint should have been triggered on code, but wasn't L-pedantic Lint: Belongs in the pedantic lint group
Projects
None yet
Development

No branches or pull requests

2 participants