@@ -45,6 +45,7 @@ mod iter_nth_zero;
45
45
mod iter_on_single_or_empty_collections;
46
46
mod iter_overeager_cloned;
47
47
mod iter_skip_next;
48
+ mod iter_skip_zero;
48
49
mod iter_with_drain;
49
50
mod iterator_step_by_zero;
50
51
mod manual_next_back;
@@ -3443,6 +3444,27 @@ declare_clippy_lint! {
3443
3444
"`format!`ing every element in a collection, then collecting the strings into a new `String`"
3444
3445
}
3445
3446
3447
+ declare_clippy_lint ! {
3448
+ /// ### What it does
3449
+ /// Checks for usage of `.skip(0)` on iterators.
3450
+ ///
3451
+ /// ### Why is this bad?
3452
+ /// This was likely intended to be `.skip(1)` to skip the first element, as `.skip(0)` does
3453
+ /// nothing. If not, the call should be removed.
3454
+ ///
3455
+ /// ### Example
3456
+ /// ```rust
3457
+ /// let v = vec![1, 2, 3];
3458
+ /// let x = v.iter().skip(0).collect::<Vec<_>>();
3459
+ /// let y = v.iter().collect::<Vec<_>>();
3460
+ /// assert_eq!(x, y);
3461
+ /// ```
3462
+ #[ clippy:: version = "1.72.0" ]
3463
+ pub ITER_SKIP_ZERO ,
3464
+ correctness,
3465
+ "disallows `.skip(0)`"
3466
+ }
3467
+
3446
3468
pub struct Methods {
3447
3469
avoid_breaking_exported_api : bool ,
3448
3470
msrv : Msrv ,
@@ -3579,6 +3601,7 @@ impl_lint_pass!(Methods => [
3579
3601
MANUAL_TRY_FOLD ,
3580
3602
FORMAT_COLLECT ,
3581
3603
STRING_LIT_CHARS_ANY ,
3604
+ ITER_SKIP_ZERO ,
3582
3605
] ) ;
3583
3606
3584
3607
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3901,7 +3924,16 @@ impl Methods {
3901
3924
unnecessary_join:: check ( cx, expr, recv, join_arg, span) ;
3902
3925
}
3903
3926
} ,
3904
- ( "last" , [ ] ) | ( "skip" , [ _] ) => {
3927
+ ( "skip" , [ arg] ) => {
3928
+ iter_skip_zero:: check ( cx, expr, arg) ;
3929
+
3930
+ if let Some ( ( name2, recv2, args2, _span2, _) ) = method_call ( recv) {
3931
+ if let ( "cloned" , [ ] ) = ( name2, args2) {
3932
+ iter_overeager_cloned:: check ( cx, expr, recv, recv2, false , false ) ;
3933
+ }
3934
+ }
3935
+ }
3936
+ ( "last" , [ ] ) => {
3905
3937
if let Some ( ( name2, recv2, args2, _span2, _) ) = method_call ( recv) {
3906
3938
if let ( "cloned" , [ ] ) = ( name2, args2) {
3907
3939
iter_overeager_cloned:: check ( cx, expr, recv, recv2, false , false ) ;
0 commit comments