11use clippy_utils:: consts:: { constant, Constant } ;
22use clippy_utils:: diagnostics:: { span_lint, span_lint_and_sugg, span_lint_and_then} ;
3+ use clippy_utils:: higher;
34use clippy_utils:: source:: { snippet, snippet_opt, snippet_with_applicability} ;
45use clippy_utils:: sugg:: Sugg ;
56use clippy_utils:: { get_parent_expr, in_constant, is_integer_const, meets_msrv, msrvs, path_to_local} ;
6- use clippy_utils:: { higher, SpanlessEq } ;
77use if_chain:: if_chain;
88use rustc_ast:: ast:: RangeLimits ;
99use rustc_errors:: Applicability ;
10- use rustc_hir:: { BinOpKind , Expr , ExprKind , HirId , PathSegment , QPath } ;
10+ use rustc_hir:: { BinOpKind , Expr , ExprKind , HirId } ;
1111use rustc_lint:: { LateContext , LateLintPass } ;
1212use rustc_middle:: ty;
1313use rustc_semver:: RustcVersion ;
1414use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
1515use rustc_span:: source_map:: { Span , Spanned } ;
16- use rustc_span:: sym;
1716use std:: cmp:: Ordering ;
1817
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-
4418declare_clippy_lint ! {
4519 /// ### What it does
4620 /// Checks for exclusive ranges where 1 is added to the
@@ -198,7 +172,6 @@ impl Ranges {
198172}
199173
200174impl_lint_pass ! ( Ranges => [
201- RANGE_ZIP_WITH_LEN ,
202175 RANGE_PLUS_ONE ,
203176 RANGE_MINUS_ONE ,
204177 REVERSED_EMPTY_RANGES ,
@@ -207,16 +180,10 @@ impl_lint_pass!(Ranges => [
207180
208181impl < ' tcx > LateLintPass < ' tcx > for Ranges {
209182 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+ }
220187 }
221188
222189 check_exclusive_range_plus_one ( cx, expr) ;
@@ -380,34 +347,6 @@ fn check_range_bounds<'a>(cx: &'a LateContext<'_>, ex: &'a Expr<'_>) -> Option<R
380347 None
381348}
382349
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-
411350// exclusive range plus one: `x..(y+1)`
412351fn check_exclusive_range_plus_one ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
413352 if_chain ! {
0 commit comments