rust-lang / rust-lang/rust-clippy
unnecessary_to_owned: borrow trait bound is required by method thet gets the &..
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Using the following flags
--force-warn clippy::unnecessary_to_owned
this code:
use std::collections::BTreeMap;
use std::rc::Rc;
fn main() {
let mut bt = BTreeMap::<Rc<String>, u64>::new();
let key: Rc<String> = Rc::from("foo".to_string());
bt.insert(key, 12);
println!("{:?}", bt.get(&"a\té \u{7f}💩\r".to_string()));
}
caused the following diagnostics:
Checking _313fb8a6a5c242b188a8201680f9ea187a85f66a v0.1.0 (/tmp/icemaker_global_tempdir.gPgWiqot4T9Z/icemaker_clippyfix_tempdir.mZJZu5FJNVPe/_313fb8a6a5c242b188a8201680f9ea187a85f66a)
warning: unnecessary use of `to_string`
--> src/main.rs:8:29
|
8 | println!("{:?}", bt.get(&"a\té \u{7f}💩\r".to_string()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `"a\té \u{7f}💩\r"`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
= note: requested on the command line with `--force-warn clippy::unnecessary-to-owned`
warning: `_313fb8a6a5c242b188a8201680f9ea187a85f66a` (bin "_313fb8a6a5c242b188a8201680f9ea187a85f66a") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.17s
However after applying these diagnostics, the resulting code:
use std::collections::BTreeMap;
use std::rc::Rc;
fn main() {
let mut bt = BTreeMap::<Rc<String>, u64>::new();
let key: Rc<String> = Rc::from("foo".to_string());
bt.insert(key, 12);
println!("{:?}", bt.get("a\té \u{7f}💩\r"));
}
no longer compiled:
Checking _313fb8a6a5c242b188a8201680f9ea187a85f66a v0.1.0 (/tmp/icemaker_global_tempdir.gPgWiqot4T9Z/icemaker_clippyfix_tempdir.mZJZu5FJNVPe/_313fb8a6a5c242b188a8201680f9ea187a85f66a)
error[E0277]: the trait bound `std::rc::Rc<std::string::String>: std::borrow::Borrow<str>` is not satisfied
--> src/main.rs:8:29
|
8 | println!("{:?}", bt.get("a\té \u{7f}💩\r"));
| --- ^^^^^^^^^^^^^^^^^ the trait `std::borrow::Borrow<str>` is not implemented for `std::rc::Rc<std::string::String>`
| |
| required by a bound introduced by this call
|
help: the trait `Borrow<str>` is not implemented for `std::rc::Rc<std::string::String>`
but trait `Borrow<std::string::String>` is implemented for it
--> /home/gh-matthiaskrgr/.rustup/toolchains/master/lib/rustlib/src/rust/library/alloc/src/rc.rs:3821:1
|
3821 | impl<T: ?Sized, A: Allocator> borrow::Borrow<T> for Rc<T, A> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: for that trait implementation, expected `std::string::String`, found `str`
note: required by a bound in `std::collections::BTreeMap::<K, V, A>::get`
--> /home/gh-matthiaskrgr/.rustup/toolchains/master/lib/rustlib/src/rust/library/alloc/src/collections/btree/map.rs:721:12
|
719 | pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
| --- required by a bound in this associated function
720 | where
721 | K: Borrow<Q> + Ord,
| ^^^^^^^^^ required by this bound in `BTreeMap::<K, V, A>::get`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `_313fb8a6a5c242b188a8201680f9ea187a85f66a` (bin "_313fb8a6a5c242b188a8201680f9ea187a85f66a") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_313fb8a6a5c242b188a8201680f9ea187a85f66a` (bin "_313fb8a6a5c242b188a8201680f9ea187a85f66a" test) due to 1 previous error
Version:
rustc 1.96.0-nightly (e3d66fe39 2026-03-07)
binary: rustc
commit-hash: e3d66fe39ae70380fa2365c008e2927479114844
commit-date: 2026-03-07
host: x86_64-unknown-linux-gnu
release: 1.96.0-nightly
LLVM version: 22.1.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the unnecessary_to_owned lint entry point and reproduce the BTreeMap<Rc, u64> example with the supplied nightly version. Check how the lint determines whether replacing to_string() is valid when BTreeMap::get requires a Borrow bound. Done means the diagnostic no longer suggests a replacement that makes the example fail to compile, with a regression test covering this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100