Skip to content

erroneous "unnecessary use of to_string" #10033

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

Closed
hexmode opened this issue Dec 5, 2022 · 0 comments · Fixed by #10913
Closed

erroneous "unnecessary use of to_string" #10033

hexmode opened this issue Dec 5, 2022 · 0 comments · Fixed by #10913
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

Comments

@hexmode
Copy link

hexmode commented Dec 5, 2022

Summary

Clippy complains about an extra to_string when a Deref implementation is provided that results in something different result than to_string. I originally described this problem at Why does clippy want two to_string() calls in a row? and @zirconium-n provided the example case here.

Lint Name

clippy::unnecessary_to_owned

Reproducer

I tried this code:

use std::{fmt::Display, ops::Deref};

fn main() {
    let f = Foo;

    foo(&f.to_string())
}

fn foo(s: &str) {
    println!("{}", s);
}

struct Foo;

impl Deref for Foo {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        "str"
    }
}

impl Display for Foo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Foo")
    }
}

I saw this happen:

warning: unnecessary use of `to_string`
 --> src/main.rs:6:11
  |
6 |     foo(&f.to_string())
  |           ^^^^^^^^^^^^ help: remove this
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
  = note: `#[warn(clippy::unnecessary_to_owned)]` on by default

warning: `clippy-bug` (bin "clippy-bug") generated 1 warning

I expected to see this happen: (no errors)

Version

rustc 1.67.0-nightly (09508489e 2022-11-04)
binary: rustc
commit-hash: 09508489efc223287731fe8abbd2a81bbf7adf8e
commit-date: 2022-11-04
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

@hexmode hexmode 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 Dec 5, 2022
@bors bors closed this as completed in 05de787 Jun 9, 2023
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant