1
1
use clippy_utils:: consts:: { constant, Constant } ;
2
2
use clippy_utils:: diagnostics:: { span_lint, span_lint_and_sugg, span_lint_and_then} ;
3
+ use clippy_utils:: higher;
3
4
use clippy_utils:: source:: { snippet, snippet_opt, snippet_with_applicability} ;
4
5
use clippy_utils:: sugg:: Sugg ;
5
6
use clippy_utils:: { get_parent_expr, in_constant, is_integer_const, meets_msrv, msrvs, path_to_local} ;
6
- use clippy_utils:: { higher, SpanlessEq } ;
7
7
use if_chain:: if_chain;
8
8
use rustc_ast:: ast:: RangeLimits ;
9
9
use rustc_errors:: Applicability ;
10
- use rustc_hir:: { BinOpKind , Expr , ExprKind , HirId , PathSegment , QPath } ;
10
+ use rustc_hir:: { BinOpKind , Expr , ExprKind , HirId } ;
11
11
use rustc_lint:: { LateContext , LateLintPass } ;
12
12
use rustc_middle:: ty;
13
13
use rustc_semver:: RustcVersion ;
14
14
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
15
15
use rustc_span:: source_map:: { Span , Spanned } ;
16
- use rustc_span:: sym;
17
16
use std:: cmp:: Ordering ;
18
17
19
- declare_clippy_lint ! {
20
- /// ### What it does
21
- /// Checks for zipping a collection with the range of
22
- /// `0.._.len()`.
23
- ///
24
- /// ### Why is this bad?
25
- /// The code is better expressed with `.enumerate()`.
26
- ///
27
- /// ### Example
28
- /// ```rust
29
- /// # let x = vec![1];
30
- /// let _ = x.iter().zip(0..x.len());
31
- /// ```
32
- ///
33
- /// Use instead:
34
- /// ```rust
35
- /// # let x = vec![1];
36
- /// let _ = x.iter().enumerate();
37
- /// ```
38
- #[ clippy:: version = "pre 1.29.0" ]
39
- pub RANGE_ZIP_WITH_LEN ,
40
- complexity,
41
- "zipping iterator with a range when `enumerate()` would do"
42
- }
43
-
44
18
declare_clippy_lint ! {
45
19
/// ### What it does
46
20
/// Checks for exclusive ranges where 1 is added to the
@@ -198,7 +172,6 @@ impl Ranges {
198
172
}
199
173
200
174
impl_lint_pass ! ( Ranges => [
201
- RANGE_ZIP_WITH_LEN ,
202
175
RANGE_PLUS_ONE ,
203
176
RANGE_MINUS_ONE ,
204
177
REVERSED_EMPTY_RANGES ,
@@ -207,16 +180,10 @@ impl_lint_pass!(Ranges => [
207
180
208
181
impl < ' tcx > LateLintPass < ' tcx > for Ranges {
209
182
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
210
- match expr. kind {
211
- ExprKind :: MethodCall ( path, args, _) => {
212
- check_range_zip_with_len ( cx, path, args, expr. span ) ;
213
- } ,
214
- ExprKind :: Binary ( ref op, l, r) => {
215
- if meets_msrv ( self . msrv , msrvs:: RANGE_CONTAINS ) {
216
- check_possible_range_contains ( cx, op. node , l, r, expr, expr. span ) ;
217
- }
218
- } ,
219
- _ => { } ,
183
+ if let ExprKind :: Binary ( ref op, l, r) = expr. kind {
184
+ if meets_msrv ( self . msrv , msrvs:: RANGE_CONTAINS ) {
185
+ check_possible_range_contains ( cx, op. node , l, r, expr, expr. span ) ;
186
+ }
220
187
}
221
188
222
189
check_exclusive_range_plus_one ( cx, expr) ;
@@ -380,34 +347,6 @@ fn check_range_bounds<'a>(cx: &'a LateContext<'_>, ex: &'a Expr<'_>) -> Option<R
380
347
None
381
348
}
382
349
383
- fn check_range_zip_with_len ( cx : & LateContext < ' _ > , path : & PathSegment < ' _ > , args : & [ Expr < ' _ > ] , span : Span ) {
384
- if_chain ! {
385
- if path. ident. as_str( ) == "zip" ;
386
- if let [ iter, zip_arg] = args;
387
- // `.iter()` call
388
- if let ExprKind :: MethodCall ( iter_path, [ iter_caller, ..] , _) = iter. kind;
389
- if iter_path. ident. name == sym:: iter;
390
- // range expression in `.zip()` call: `0..x.len()`
391
- if let Some ( higher:: Range { start: Some ( start) , end: Some ( end) , .. } ) = higher:: Range :: hir( zip_arg) ;
392
- if is_integer_const( cx, start, 0 ) ;
393
- // `.len()` call
394
- if let ExprKind :: MethodCall ( len_path, [ len_caller] , _) = end. kind;
395
- if len_path. ident. name == sym:: len;
396
- // `.iter()` and `.len()` called on same `Path`
397
- if let ExprKind :: Path ( QPath :: Resolved ( _, iter_path) ) = iter_caller. kind;
398
- if let ExprKind :: Path ( QPath :: Resolved ( _, len_path) ) = len_caller. kind;
399
- if SpanlessEq :: new( cx) . eq_path_segments( iter_path. segments, len_path. segments) ;
400
- then {
401
- span_lint( cx,
402
- RANGE_ZIP_WITH_LEN ,
403
- span,
404
- & format!( "it is more idiomatic to use `{}.iter().enumerate()`" ,
405
- snippet( cx, iter_caller. span, "_" ) )
406
- ) ;
407
- }
408
- }
409
- }
410
-
411
350
// exclusive range plus one: `x..(y+1)`
412
351
fn check_exclusive_range_plus_one ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
413
352
if_chain ! {
0 commit comments