Skip to content

Commit 4cedcb7

Browse files
committed
fix: unnecessary_to_owned FP when map key is a reference
1 parent da7b678 commit 4cedcb7

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,10 @@ fn check_borrow_predicate<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
739739
&& let caller_ty = cx.typeck_results().expr_ty(caller)
740740
// For now we limit it to "map types".
741741
&& is_a_std_map_type(cx, caller_ty)
742+
// We need to check that the key type is not a reference.
743+
&& let ty::Adt(_, caller_ty_args) = caller_ty.kind()
744+
&& let (_, n_key_ref) = peel_middle_ty_refs(caller_ty_args.type_at(0))
745+
&& n_key_ref == 0
742746
{
743747
check_if_applicable_to_argument(cx, &arg);
744748
}

tests/ui/unnecessary_to_owned.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,9 @@ mod issue_14242 {
675675
rc_slice_provider().to_vec().into_iter()
676676
}
677677
}
678+
679+
fn issue14833() {
680+
use std::collections::HashSet;
681+
let mut s = HashSet::<&String>::new();
682+
s.remove(&"hello".to_owned());
683+
}

tests/ui/unnecessary_to_owned.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,9 @@ mod issue_14242 {
675675
rc_slice_provider().to_vec().into_iter()
676676
}
677677
}
678+
679+
fn issue14833() {
680+
use std::collections::HashSet;
681+
let mut s = HashSet::<&String>::new();
682+
s.remove(&"hello".to_owned());
683+
}

0 commit comments

Comments
 (0)