Skip to content

Commit e8aadad

Browse files
Add more ui tests for unnecessary_to_owned
1 parent a6d17dc commit e8aadad

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

tests/ui/unnecessary_to_owned.fixed

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,25 @@ mod issue_11952 {
530530
IntoFuture::into_future(foo([], &0));
531531
}
532532
}
533+
534+
fn borrow_checks() {
535+
use std::collections::HashSet;
536+
537+
fn inner(a: &[&str]) {
538+
let mut s = HashSet::from([vec!["a"]]);
539+
s.remove(a); //~ ERROR: unnecessary use of `to_vec`
540+
}
541+
542+
let mut s = HashSet::from(["a".to_string()]);
543+
s.remove("b"); //~ ERROR: unnecessary use of `to_owned`
544+
s.remove("b"); //~ ERROR: unnecessary use of `to_string`
545+
// Should not warn.
546+
s.remove("b");
547+
548+
let mut s = HashSet::from([vec!["a"]]);
549+
s.remove(["b"].as_slice()); //~ ERROR: unnecessary use of `to_vec`
550+
551+
// Should not warn.
552+
s.remove(&["b"].to_vec().clone());
553+
s.remove(["a"].as_slice());
554+
}

tests/ui/unnecessary_to_owned.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,25 @@ mod issue_11952 {
530530
IntoFuture::into_future(foo([].to_vec(), &0));
531531
}
532532
}
533+
534+
fn borrow_checks() {
535+
use std::collections::HashSet;
536+
537+
fn inner(a: &[&str]) {
538+
let mut s = HashSet::from([vec!["a"]]);
539+
s.remove(&a.to_vec()); //~ ERROR: unnecessary use of `to_vec`
540+
}
541+
542+
let mut s = HashSet::from(["a".to_string()]);
543+
s.remove(&"b".to_owned()); //~ ERROR: unnecessary use of `to_owned`
544+
s.remove(&"b".to_string()); //~ ERROR: unnecessary use of `to_string`
545+
// Should not warn.
546+
s.remove("b");
547+
548+
let mut s = HashSet::from([vec!["a"]]);
549+
s.remove(&["b"].to_vec()); //~ ERROR: unnecessary use of `to_vec`
550+
551+
// Should not warn.
552+
s.remove(&["b"].to_vec().clone());
553+
s.remove(["a"].as_slice());
554+
}

tests/ui/unnecessary_to_owned.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,5 +523,29 @@ 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:539: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:543: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:544: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:549:14
546+
|
547+
LL | s.remove(&["b"].to_vec());
548+
| ^^^^^^^^^^^^^^^ help: replace it with: `["b"].as_slice()`
549+
550+
error: aborting due to 84 previous errors
527551

0 commit comments

Comments
 (0)