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

unnecessary use of to_string for a type with custom ToString trait #9317

Closed
xphoniex opened this issue Aug 10, 2022 · 7 comments · Fixed by #9329
Closed

unnecessary use of to_string for a type with custom ToString trait #9317

xphoniex opened this issue Aug 10, 2022 · 7 comments · Fixed by #9329
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@xphoniex
Copy link
Contributor

xphoniex commented Aug 10, 2022

Summary

I'm programmatically commiting a patch to a git repo and then writing the hash (a git2::Oid) in a file:

let commit = util::commit(...);
fs::write(path, commit);

if I use above code, I'll get bytes written to the file whereas I wanted the string of hash, so naturally I change it to fs::write(path, commit.to_string()), however that's throwing a lint error:

warning: unnecessary use of `to_string`
   --> ...
    |
810 |             commit.to_string(),
    |             ^^^^^^^^^^^^^^^^^^ help: use: `commit`
    |
    = note: `#[warn(clippy::unnecessary_to_owned)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned

solution is to use format!(...) instead, but that's pretty ugly.

Lint Name

unnecessary_to_owned

Version

rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-unknown-linux-gnu
release: 1.61.0
LLVM version: 14.0.0

Additional Labels

No response

@xphoniex xphoniex 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 Aug 10, 2022
@Jarcho
Copy link
Contributor

Jarcho commented Aug 10, 2022

Note to anyone working on this. to_string should only be linted when the type is string-like. e.g. implements Deref<Target=str/String> or AsRef<str/String>.

@Jarcho Jarcho added the E-medium Call for participation: Medium difficulty level problem and requires some initial experience. label Aug 10, 2022
@kartva
Copy link
Contributor

kartva commented Aug 12, 2022

@rustbot claim

@kartva
Copy link
Contributor

kartva commented Aug 12, 2022

@xphoniex

I wasn't able to replicate your report with rust v1.61.0 locally nor v1.63.0 (playground for v1.63.0 - Tools - Clippy )
Am I perhaps missing any relevant trait implementations of Oid?

@rcgoodfellow
Copy link

Hitting this too. A simple repro is

main.rs

fn main() {
    let id = uuid::Uuid::new_v4();

    std::fs::write("bad", &id).unwrap();
    std::fs::write("good", id.to_string()).unwrap();
}

Cargo.toml

[package]
name = "poo"
version = "0.1.0"
edition = "2021"

[dependencies]
uuid = { version = "1.0.0", features = [ "serde", "v4" ] }

Demo

$ cargo run ; cat good; echo ""; cat bad; echo "";
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/poo`
e9d1adb8-a9b2-4864-a2d2-d8095cba34a9
�ѭ���Hd���	\�4�
$ cargo --version
cargo 1.63.0 (fd9c4297c 2022-07-01)
$ cargo clippy
warning: unnecessary use of `to_string`
 --> src/main.rs:5:28
  |
5 |     std::fs::write("good", id.to_string()).unwrap();
  |                            ^^^^^^^^^^^^^^ help: use: `id`
  |
  = note: `#[warn(clippy::unnecessary_to_owned)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned

warning: `poo` (bin "poo") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.40s

@xphoniex
Copy link
Contributor Author

@DesmondWillowbrook

Hey, I'd already done the preliminary work and prepared a fix. You can skip this issue.

@xphoniex
Copy link
Contributor Author

@rcgoodfellow

Yes, this is the same exact issue. PR #9329 fixes this.

@kartva
Copy link
Contributor

kartva commented Aug 14, 2022

@rustbot release

@bors bors closed this as completed in 8c9040c Aug 15, 2022
@kartva kartva removed their assignment Aug 15, 2022
bors added a commit that referenced this issue Sep 4, 2022
Fix `unnecessary_to_owned` false positive

Fixes #9351.

Note that this commit reworks that fix for #9317. The change
is to check that the type implements `AsRef<str>` before regarding
`to_string` as an equivalent of `to_owned`. This was suggested
by Jarcho in the #9317 issue comments.

The benefit of this is that it moves some complexity out of
`check_other_call_arg` and simplifies the module as a whole.

changelog: FP: [`unnecessary_to_owned`]: No longer lints, if type change would cause errors in the caller function
kraktus pushed a commit to kraktus/rust-clippy that referenced this issue Sep 5, 2022
Fixes rust-lang#9351.

Note that this commit reworks that fix for rust-lang#9317. The change
is to check that the type implements `AsRef<str>` before regarding
`to_string` as an equivalent of `to_owned`. This was suggested
by Jarcho in the rust-lang#9317 issue comments.

The benefit of this is that it moves some complexity out of
`check_other_call_arg` and simplifies the module as a whole.
kartva pushed a commit to kartva/rust-clippy that referenced this issue Sep 6, 2022
Fixes rust-lang#9351.

Note that this commit reworks that fix for rust-lang#9317. The change
is to check that the type implements `AsRef<str>` before regarding
`to_string` as an equivalent of `to_owned`. This was suggested
by Jarcho in the rust-lang#9317 issue comments.

The benefit of this is that it moves some complexity out of
`check_other_call_arg` and simplifies the module as a whole.
kartva pushed a commit to kartva/rust-clippy that referenced this issue Sep 7, 2022
Fixes rust-lang#9351.

Note that this commit reworks that fix for rust-lang#9317. The change
is to check that the type implements `AsRef<str>` before regarding
`to_string` as an equivalent of `to_owned`. This was suggested
by Jarcho in the rust-lang#9317 issue comments.

The benefit of this is that it moves some complexity out of
`check_other_call_arg` and simplifies the module as a whole.
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 E-medium Call for participation: Medium difficulty level problem and requires some initial experience. 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.

4 participants