Skip to content

unnecessary_to_owned: borrow trait bound is required by method thet gets the &.. #16693

@matthiaskrgr

Description

@matthiaskrgr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions