diff options
| -rw-r--r-- | tests/ui/unnecessary_to_owned.fixed | 36 | ||||
| -rw-r--r-- | tests/ui/unnecessary_to_owned.rs | 36 | ||||
| -rw-r--r-- | tests/ui/unnecessary_to_owned.stderr | 32 |
3 files changed, 103 insertions, 1 deletions
diff --git a/tests/ui/unnecessary_to_owned.fixed b/tests/ui/unnecessary_to_owned.fixed index 7f01c981a93..1afa5ab54c4 100644 --- a/tests/ui/unnecessary_to_owned.fixed +++ b/tests/ui/unnecessary_to_owned.fixed @@ -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()); +} diff --git a/tests/ui/unnecessary_to_owned.rs b/tests/ui/unnecessary_to_owned.rs index a270ed1e1c2..aa88dde43bf 100644 --- a/tests/ui/unnecessary_to_owned.rs +++ b/tests/ui/unnecessary_to_owned.rs @@ -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()); +} diff --git a/tests/ui/unnecessary_to_owned.stderr b/tests/ui/unnecessary_to_owned.stderr index d4199f8a30a..5475df9c7b9 100644 --- a/tests/ui/unnecessary_to_owned.stderr +++ b/tests/ui/unnecessary_to_owned.stderr @@ -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 |
