Skip to content

Commit d281461

Browse files
Add more ui tests for unnecessary_to_owned
1 parent 9cde201 commit d281461

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

tests/ui/unnecessary_to_owned.fixed

+36
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,39 @@ mod issue_11952 {
530530
IntoFuture::into_future(foo([], &0));
531531
}
532532
}
533+
534+
fn borrow_checks() {
535+
use std::borrow::Borrow;
536+
use std::collections::HashSet;
537+
538+
fn inner(a: &[&str]) {
539+
let mut s = HashSet::from([vec!["a"]]);
540+
s.remove(a); //~ ERROR: unnecessary use of `to_vec`
541+
}
542+
543+
let mut s = HashSet::from(["a".to_string()]);
544+
s.remove("b"); //~ ERROR: unnecessary use of `to_owned`
545+
s.remove("b"); //~ ERROR: unnecessary use of `to_string`
546+
// Should not warn.
547+
s.remove("b");
548+
549+
let mut s = HashSet::from([vec!["a"]]);
550+
s.remove(["b"].as_slice()); //~ ERROR: unnecessary use of `to_vec`
551+
s.remove((&["b"]).as_slice()); //~ ERROR: unnecessary use of `to_vec`
552+
553+
// Should not warn.
554+
s.remove(&["b"].to_vec().clone());
555+
s.remove(["a"].as_slice());
556+
557+
trait SetExt {
558+
fn foo<Q: Borrow<str>>(&self, _: &String);
559+
}
560+
561+
impl<K> SetExt for HashSet<K> {
562+
fn foo<Q: Borrow<str>>(&self, _: &String) {}
563+
}
564+
565+
// Should not lint!
566+
HashSet::<i32>::new().foo::<&str>(&"".to_owned());
567+
HashSet::<String>::new().get(&1.to_string());
568+
}

tests/ui/unnecessary_to_owned.rs

+36
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,39 @@ mod issue_11952 {
530530
IntoFuture::into_future(foo([].to_vec(), &0));
531531
}
532532
}
533+
534+
fn borrow_checks() {
535+
use std::borrow::Borrow;
536+
use std::collections::HashSet;
537+
538+
fn inner(a: &[&str]) {
539+
let mut s = HashSet::from([vec!["a"]]);
540+
s.remove(&a.to_vec()); //~ ERROR: unnecessary use of `to_vec`
541+
}
542+
543+
let mut s = HashSet::from(["a".to_string()]);
544+
s.remove(&"b".to_owned()); //~ ERROR: unnecessary use of `to_owned`
545+
s.remove(&"b".to_string()); //~ ERROR: unnecessary use of `to_string`
546+
// Should not warn.
547+
s.remove("b");
548+
549+
let mut s = HashSet::from([vec!["a"]]);
550+
s.remove(&["b"].to_vec()); //~ ERROR: unnecessary use of `to_vec`
551+
s.remove(&(&["b"]).to_vec()); //~ ERROR: unnecessary use of `to_vec`
552+
553+
// Should not warn.
554+
s.remove(&["b"].to_vec().clone());
555+
s.remove(["a"].as_slice());
556+
557+
trait SetExt {
558+
fn foo<Q: Borrow<str>>(&self, _: &String);
559+
}
560+
561+
impl<K> SetExt for HashSet<K> {
562+
fn foo<Q: Borrow<str>>(&self, _: &String) {}
563+
}
564+
565+
// Should not lint!
566+
HashSet::<i32>::new().foo::<&str>(&"".to_owned());
567+
HashSet::<String>::new().get(&1.to_string());
568+
}

tests/ui/unnecessary_to_owned.stderr

+31-1
Original file line numberDiff line numberDiff line change
@@ -523,5 +523,35 @@ error: unnecessary use of `to_vec`
523523
LL | IntoFuture::into_future(foo([].to_vec(), &0));
524524
| ^^^^^^^^^^^ help: use: `[]`
525525

526-
error: aborting due to 80 previous errors
526+
error: unnecessary use of `to_vec`
527+
--> tests/ui/unnecessary_to_owned.rs:540:18
528+
|
529+
LL | s.remove(&a.to_vec());
530+
| ^^^^^^^^^^^ help: replace it with: `a`
531+
532+
error: unnecessary use of `to_owned`
533+
--> tests/ui/unnecessary_to_owned.rs:544:14
534+
|
535+
LL | s.remove(&"b".to_owned());
536+
| ^^^^^^^^^^^^^^^ help: replace it with: `"b"`
537+
538+
error: unnecessary use of `to_string`
539+
--> tests/ui/unnecessary_to_owned.rs:545:14
540+
|
541+
LL | s.remove(&"b".to_string());
542+
| ^^^^^^^^^^^^^^^^ help: replace it with: `"b"`
543+
544+
error: unnecessary use of `to_vec`
545+
--> tests/ui/unnecessary_to_owned.rs:550:14
546+
|
547+
LL | s.remove(&["b"].to_vec());
548+
| ^^^^^^^^^^^^^^^ help: replace it with: `["b"].as_slice()`
549+
550+
error: unnecessary use of `to_vec`
551+
--> tests/ui/unnecessary_to_owned.rs:551:14
552+
|
553+
LL | s.remove(&(&["b"]).to_vec());
554+
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `(&["b"]).as_slice()`
555+
556+
error: aborting due to 85 previous errors
527557

0 commit comments

Comments
 (0)