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

Clippy invalid suggestion for format!("{}", &s) #7078

Closed
ghost opened this issue Apr 14, 2021 · 2 comments
Closed

Clippy invalid suggestion for format!("{}", &s) #7078

ghost opened this issue Apr 14, 2021 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ghost
Copy link

ghost commented Apr 14, 2021

I tried to clippy --fix this code

#![allow(dead_code)]

enum A {
    B(String),
    C,
}

fn main() {
    let a = A::C;
    let s: String = match a {
       A::B(s) => format!("{}", &s),
       A::C => format!("{}", "hello"),
    };
    println!("{}", s);
}

This fixes to:

match a {
    A(s) => &s.to_string(),
    B(_) => "hello".to_string(),
}

But now the different branches have different types. This highlights that the suggested fix even on stable is not correct:

warning: useless use of `format!`
  --> src/main.rs:11:19
   |
11 |        A::B(s) => format!("{}", &s),
   |                   ^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `&s.to_string()`
   |
   = note: `#[warn(clippy::useless_format)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format

It should suggest

help: consider using `.to_string()`: `(&s).to_string()`

Meta

  • cargo clippy -V: clippy 0.1.51 (2fd73fab 2021-03-23)
  • cargo +nightly-x86_64-apple-darwin clippy -V: clippy 0.1.52 (07e0e2e 2021-03-24)
  • rustc -Vv:

rustc 1.51.0 (2fd73fabe 2021-03-23)
binary: rustc
commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
commit-date: 2021-03-23
host: x86_64-apple-darwin
release: 1.51.0
LLVM version: 11.0.1

@ghost ghost added the C-bug Category: Clippy is not doing the correct thing label Apr 14, 2021
@giraffate giraffate added good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Apr 15, 2021
@basavesh
Copy link
Contributor

#7092 Fixes this.

+error: useless use of format!

  • --> $DIR/format.rs:82:19
  • |
    +LL | A::B(s) => format!("{}", &s),
  • | ^^^^^^^^^^^^^^^^^ help: consider using .to_string(): (&s).to_string()

+error: useless use of format!

  • --> $DIR/format.rs:83:16
  • |
    +LL | A::C => format!("{}", "hello"),
  • | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): "hello".to_string()

+error: aborting due to 16 previous errors

@giraffate
Copy link
Contributor

#7092 fixed this, so I'm closing this.

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 good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

2 participants