-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Comments
Note to anyone working on this. |
@rustbot claim |
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 ) |
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 |
Hey, I'd already done the preliminary work and prepared a fix. You can skip this issue. |
Yes, this is the same exact issue. PR #9329 fixes this. |
@rustbot release |
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
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.
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.
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.
Summary
I'm programmatically commiting a patch to a git repo and then writing the hash (a
git2::Oid
) in a file: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:solution is to use
format!(...)
instead, but that's pretty ugly.Lint Name
unnecessary_to_owned
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: