Add more ui tests for unnecessary_to_owned

This commit is contained in:
Guillaume Gomez 2024-02-20 17:53:19 +01:00
parent 9cde201ba1
commit d28146133c
3 changed files with 103 additions and 1 deletions

View File

@ -530,3 +530,39 @@ mod issue_11952 {
IntoFuture::into_future(foo([], &0));
}
}
fn borrow_checks() {
use std::borrow::Borrow;
use std::collections::HashSet;
fn inner(a: &[&str]) {
let mut s = HashSet::from([vec!["a"]]);
s.remove(a); //~ ERROR: unnecessary use of `to_vec`
}
let mut s = HashSet::from(["a".to_string()]);
s.remove("b"); //~ ERROR: unnecessary use of `to_owned`
s.remove("b"); //~ ERROR: unnecessary use of `to_string`
// Should not warn.
s.remove("b");
let mut s = HashSet::from([vec!["a"]]);
s.remove(["b"].as_slice()); //~ ERROR: unnecessary use of `to_vec`
s.remove((&["b"]).as_slice()); //~ ERROR: unnecessary use of `to_vec`
// Should not warn.
s.remove(&["b"].to_vec().clone());
s.remove(["a"].as_slice());
trait SetExt {
fn foo<Q: Borrow<str>>(&self, _: &String);
}
impl<K> SetExt for HashSet<K> {
fn foo<Q: Borrow<str>>(&self, _: &String) {}
}
// Should not lint!
HashSet::<i32>::new().foo::<&str>(&"".to_owned());
HashSet::<String>::new().get(&1.to_string());
}

View File

@ -530,3 +530,39 @@ mod issue_11952 {
IntoFuture::into_future(foo([].to_vec(), &0));
}
}
fn borrow_checks() {
use std::borrow::Borrow;
use std::collections::HashSet;
fn inner(a: &[&str]) {
let mut s = HashSet::from([vec!["a"]]);
s.remove(&a.to_vec()); //~ ERROR: unnecessary use of `to_vec`
}
let mut s = HashSet::from(["a".to_string()]);
s.remove(&"b".to_owned()); //~ ERROR: unnecessary use of `to_owned`
s.remove(&"b".to_string()); //~ ERROR: unnecessary use of `to_string`
// Should not warn.
s.remove("b");
let mut s = HashSet::from([vec!["a"]]);
s.remove(&["b"].to_vec()); //~ ERROR: unnecessary use of `to_vec`
s.remove(&(&["b"]).to_vec()); //~ ERROR: unnecessary use of `to_vec`
// Should not warn.
s.remove(&["b"].to_vec().clone());
s.remove(["a"].as_slice());
trait SetExt {
fn foo<Q: Borrow<str>>(&self, _: &String);
}
impl<K> SetExt for HashSet<K> {
fn foo<Q: Borrow<str>>(&self, _: &String) {}
}
// Should not lint!
HashSet::<i32>::new().foo::<&str>(&"".to_owned());
HashSet::<String>::new().get(&1.to_string());
}

View File

@ -523,5 +523,35 @@ error: unnecessary use of `to_vec`
LL | IntoFuture::into_future(foo([].to_vec(), &0));
| ^^^^^^^^^^^ help: use: `[]`
error: aborting due to 80 previous errors
error: unnecessary use of `to_vec`
--> tests/ui/unnecessary_to_owned.rs:540:18
|
LL | s.remove(&a.to_vec());
| ^^^^^^^^^^^ help: replace it with: `a`
error: unnecessary use of `to_owned`
--> tests/ui/unnecessary_to_owned.rs:544:14
|
LL | s.remove(&"b".to_owned());
| ^^^^^^^^^^^^^^^ help: replace it with: `"b"`
error: unnecessary use of `to_string`
--> tests/ui/unnecessary_to_owned.rs:545:14
|
LL | s.remove(&"b".to_string());
| ^^^^^^^^^^^^^^^^ help: replace it with: `"b"`
error: unnecessary use of `to_vec`
--> tests/ui/unnecessary_to_owned.rs:550:14
|
LL | s.remove(&["b"].to_vec());
| ^^^^^^^^^^^^^^^ help: replace it with: `["b"].as_slice()`
error: unnecessary use of `to_vec`
--> tests/ui/unnecessary_to_owned.rs:551:14
|
LL | s.remove(&(&["b"]).to_vec());
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `(&["b"]).as_slice()`
error: aborting due to 85 previous errors