Skip to content

Commit

Permalink
Auto merge of rust-lang#95274 - jendrikw:slice-must-use, r=Dylan-DPC
Browse files Browse the repository at this point in the history
add #[must_use] to functions of slice and its iterators.

Continuation of rust-lang#92853.

Tracking issue: rust-lang#89692.
  • Loading branch information
bors committed Mar 26, 2022
2 parents 2d85270 + 41f1413 commit a7da8a6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions tests/ui/bytes_nth.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

fn main() {
let s = String::from("String");
s.as_bytes().get(3);
let _ = s.as_bytes().get(3);
let _ = &s.as_bytes().get(3);
s[..].as_bytes().get(3);
let _ = s[..].as_bytes().get(3);
}
4 changes: 2 additions & 2 deletions tests/ui/bytes_nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

fn main() {
let s = String::from("String");
s.bytes().nth(3);
let _ = s.bytes().nth(3);
let _ = &s.bytes().nth(3);
s[..].bytes().nth(3);
let _ = s[..].bytes().nth(3);
}
12 changes: 6 additions & 6 deletions tests/ui/bytes_nth.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: called `.byte().nth()` on a `String`
--> $DIR/bytes_nth.rs:8:5
--> $DIR/bytes_nth.rs:8:13
|
LL | s.bytes().nth(3);
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
LL | let _ = s.bytes().nth(3);
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
|
= note: `-D clippy::bytes-nth` implied by `-D warnings`

Expand All @@ -13,10 +13,10 @@ LL | let _ = &s.bytes().nth(3);
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`

error: called `.byte().nth()` on a `str`
--> $DIR/bytes_nth.rs:10:5
--> $DIR/bytes_nth.rs:10:13
|
LL | s[..].bytes().nth(3);
| ^^^^^^^^^^^^^^^^^^^^ help: try: `s[..].as_bytes().get(3)`
LL | let _ = s[..].bytes().nth(3);
| ^^^^^^^^^^^^^^^^^^^^ help: try: `s[..].as_bytes().get(3)`

error: aborting due to 3 previous errors

8 changes: 4 additions & 4 deletions tests/ui/iter_next_slice.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ fn main() {
let s = [1, 2, 3];
let v = vec![1, 2, 3];

s.get(0);
let _ = s.get(0);
// Should be replaced by s.get(0)

s.get(2);
let _ = s.get(2);
// Should be replaced by s.get(2)

v.get(5);
let _ = v.get(5);
// Should be replaced by v.get(5)

v.get(0);
let _ = v.get(0);
// Should be replaced by v.get(0)

let o = Some(5);
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/iter_next_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ fn main() {
let s = [1, 2, 3];
let v = vec![1, 2, 3];

s.iter().next();
let _ = s.iter().next();
// Should be replaced by s.get(0)

s[2..].iter().next();
let _ = s[2..].iter().next();
// Should be replaced by s.get(2)

v[5..].iter().next();
let _ = v[5..].iter().next();
// Should be replaced by v.get(5)

v.iter().next();
let _ = v.iter().next();
// Should be replaced by v.get(0)

let o = Some(5);
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/iter_next_slice.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
error: using `.iter().next()` on an array
--> $DIR/iter_next_slice.rs:9:5
--> $DIR/iter_next_slice.rs:9:13
|
LL | s.iter().next();
| ^^^^^^^^^^^^^^^ help: try calling: `s.get(0)`
LL | let _ = s.iter().next();
| ^^^^^^^^^^^^^^^ help: try calling: `s.get(0)`
|
= note: `-D clippy::iter-next-slice` implied by `-D warnings`

error: using `.iter().next()` on a Slice without end index
--> $DIR/iter_next_slice.rs:12:5
--> $DIR/iter_next_slice.rs:12:13
|
LL | s[2..].iter().next();
| ^^^^^^^^^^^^^^^^^^^^ help: try calling: `s.get(2)`
LL | let _ = s[2..].iter().next();
| ^^^^^^^^^^^^^^^^^^^^ help: try calling: `s.get(2)`

error: using `.iter().next()` on a Slice without end index
--> $DIR/iter_next_slice.rs:15:5
--> $DIR/iter_next_slice.rs:15:13
|
LL | v[5..].iter().next();
| ^^^^^^^^^^^^^^^^^^^^ help: try calling: `v.get(5)`
LL | let _ = v[5..].iter().next();
| ^^^^^^^^^^^^^^^^^^^^ help: try calling: `v.get(5)`

error: using `.iter().next()` on an array
--> $DIR/iter_next_slice.rs:18:5
--> $DIR/iter_next_slice.rs:18:13
|
LL | v.iter().next();
| ^^^^^^^^^^^^^^^ help: try calling: `v.get(0)`
LL | let _ = v.iter().next();
| ^^^^^^^^^^^^^^^ help: try calling: `v.get(0)`

error: aborting due to 4 previous errors

0 comments on commit a7da8a6

Please sign in to comment.