rust-lang / rust-lang/rust-clippy

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

Open
#3,156 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint I-false-negative L-pedantic
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the Rust example and compare format!("{}", y) with y.to_string(). Determine whether rust-clippy should flag this pattern and document the decision; the issue is complete when the lint proposal is accepted or rejected with rationale.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.