Skip to content

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

@xphoniex

Description

@xphoniex

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions