@@ -45,6 +45,7 @@ mod iter_nth_zero;
4545mod iter_on_single_or_empty_collections;
4646mod iter_overeager_cloned;
4747mod iter_skip_next;
48+ mod iter_skip_zero;
4849mod iter_with_drain;
4950mod iterator_step_by_zero;
5051mod manual_next_back;
@@ -3443,6 +3444,27 @@ declare_clippy_lint! {
34433444 "`format!`ing every element in a collection, then collecting the strings into a new `String`"
34443445}
34453446
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+
34463468pub struct Methods {
34473469 avoid_breaking_exported_api : bool ,
34483470 msrv : Msrv ,
@@ -3579,6 +3601,7 @@ impl_lint_pass!(Methods => [
35793601 MANUAL_TRY_FOLD ,
35803602 FORMAT_COLLECT ,
35813603 STRING_LIT_CHARS_ANY ,
3604+ ITER_SKIP_ZERO ,
35823605] ) ;
35833606
35843607/// Extracts a method call name, args, and `Span` of the method name.
@@ -3901,7 +3924,16 @@ impl Methods {
39013924 unnecessary_join:: check ( cx, expr, recv, join_arg, span) ;
39023925 }
39033926 } ,
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" , [ ] ) => {
39053937 if let Some ( ( name2, recv2, args2, _span2, _) ) = method_call ( recv) {
39063938 if let ( "cloned" , [ ] ) = ( name2, args2) {
39073939 iter_overeager_cloned:: check ( cx, expr, recv, recv2, false , false ) ;
0 commit comments