rust-lang / rust-lang/rust-clippy

`assigning_clones` complains about `static str` -> `String` assignment.

Open
#12,799 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

The assigning_clones lint fires when I assign to a String using a const &str and .to_owned().

The suggested replacement is harder to read and obfuscates the meaning of the code.

Lint Name

assigning_clones

Reproducer

I tried this code:

const INIT_FOO: &str = "foo";

pub struct Foo {
    x: String,
}

impl Foo {
    pub fn load(&mut self) {
        self.x = INIT_FOO.to_owned();
    }
}

I saw this happen:

warning: assigning the result of `ToOwned::to_owned()` may be inefficient
 --> src/lib.rs:9:9
  |
9 |         self.x = INIT_FOO.to_owned();
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `INIT_FOO.clone_into(&mut self.x)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assigning_clones
  = note: `#[warn(clippy::assigning_clones)]` on by default

I expected to see this happen:

Nothing.

Version
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the implementation and tests for the assigning_clones lint, then reproduce the reported const &str to String assignment. Done means this example no longer emits the warning while the lint continues to catch the inefficient assignments it is intended to report.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.