forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#5597 - esamudera:slice_iter_next, r=flip1995
New lint: iter_next_slice Hello, this is a work-in-progress PR for issue: rust-lang/rust-clippy#5572 I have implemented lint to replace `iter().next()` for `slice[index..]` and `array` with `get(index)` and `get(0)` respectively. However since I made a lot of changes, I would like to request some feedback before continuing so that I could fix mistakes. Thank you! --- changelog: implement `iter_next_slice` lint and test, and modify `needless_continues`, `for_loop_over_options_result` UI tests since they have `iter().next()`
- Loading branch information
Showing
14 changed files
with
204 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// run-rustfix | ||
#![warn(clippy::iter_next_slice)] | ||
|
||
fn main() { | ||
// test code goes here | ||
let s = [1, 2, 3]; | ||
let v = vec![1, 2, 3]; | ||
|
||
s.get(0); | ||
// Should be replaced by s.get(0) | ||
|
||
s.get(2); | ||
// Should be replaced by s.get(2) | ||
|
||
v.get(5); | ||
// Should be replaced by v.get(5) | ||
|
||
v.get(0); | ||
// Should be replaced by v.get(0) | ||
|
||
let o = Some(5); | ||
o.iter().next(); | ||
// Shouldn't be linted since this is not a Slice or an Array | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// run-rustfix | ||
#![warn(clippy::iter_next_slice)] | ||
|
||
fn main() { | ||
// test code goes here | ||
let s = [1, 2, 3]; | ||
let v = vec![1, 2, 3]; | ||
|
||
s.iter().next(); | ||
// Should be replaced by s.get(0) | ||
|
||
s[2..].iter().next(); | ||
// Should be replaced by s.get(2) | ||
|
||
v[5..].iter().next(); | ||
// Should be replaced by v.get(5) | ||
|
||
v.iter().next(); | ||
// Should be replaced by v.get(0) | ||
|
||
let o = Some(5); | ||
o.iter().next(); | ||
// Shouldn't be linted since this is not a Slice or an Array | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: Using `.iter().next()` on an array | ||
--> $DIR/iter_next_slice.rs:9:5 | ||
| | ||
LL | 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 | ||
| | ||
LL | 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 | ||
| | ||
LL | v[5..].iter().next(); | ||
| ^^^^^^^^^^^^^^^^^^^^ help: try calling: `v.get(5)` | ||
|
||
error: Using `.iter().next()` on an array | ||
--> $DIR/iter_next_slice.rs:18:5 | ||
| | ||
LL | v.iter().next(); | ||
| ^^^^^^^^^^^^^^^ help: try calling: `v.get(0)` | ||
|
||
error: aborting due to 4 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.