1 use crate::consts::{constant, Constant};
2 use if_chain::if_chain;
3 use rustc_ast::ast::RangeLimits;
4 use rustc_errors::Applicability;
5 use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, QPath};
6 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 use rustc_span::source_map::{Span, Spanned};
11 use rustc_span::symbol::Ident;
12 use std::cmp::Ordering;
14 use crate::utils::sugg::Sugg;
16 get_parent_expr, is_integer_const, single_segment_path, snippet, snippet_opt, snippet_with_applicability,
17 span_lint, span_lint_and_sugg, span_lint_and_then,
19 use crate::utils::{higher, SpanlessEq};
21 declare_clippy_lint! {
22 /// **What it does:** Checks for zipping a collection with the range of
25 /// **Why is this bad?** The code is better expressed with `.enumerate()`.
27 /// **Known problems:** None.
31 /// # let x = vec![1];
32 /// x.iter().zip(0..x.len());
34 /// Could be written as
36 /// # let x = vec![1];
37 /// x.iter().enumerate();
39 pub RANGE_ZIP_WITH_LEN,
41 "zipping iterator with a range when `enumerate()` would do"
44 declare_clippy_lint! {
45 /// **What it does:** Checks for exclusive ranges where 1 is added to the
46 /// upper bound, e.g., `x..(y+1)`.
48 /// **Why is this bad?** The code is more readable with an inclusive range
51 /// **Known problems:** Will add unnecessary pair of parentheses when the
52 /// expression is not wrapped in a pair but starts with a opening parenthesis
53 /// and ends with a closing one.
54 /// I.e., `let _ = (f()+1)..(f()+1)` results in `let _ = ((f()+1)..=f())`.
56 /// Also in many cases, inclusive ranges are still slower to run than
57 /// exclusive ranges, because they essentially add an extra branch that
58 /// LLVM may fail to hoist out of the loop.
60 /// This will cause a warning that cannot be fixed if the consumer of the
61 /// range only accepts a specific range type, instead of the generic
62 /// `RangeBounds` trait
63 /// ([#3307](https://github.com/rust-lang/rust-clippy/issues/3307)).
67 /// for x..(y+1) { .. }
69 /// Could be written as
75 "`x..(y+1)` reads better as `x..=y`"
78 declare_clippy_lint! {
79 /// **What it does:** Checks for inclusive ranges where 1 is subtracted from
80 /// the upper bound, e.g., `x..=(y-1)`.
82 /// **Why is this bad?** The code is more readable with an exclusive range
85 /// **Known problems:** This will cause a warning that cannot be fixed if
86 /// the consumer of the range only accepts a specific range type, instead of
87 /// the generic `RangeBounds` trait
88 /// ([#3307](https://github.com/rust-lang/rust-clippy/issues/3307)).
92 /// for x..=(y-1) { .. }
94 /// Could be written as
100 "`x..=(y-1)` reads better as `x..y`"
103 declare_clippy_lint! {
104 /// **What it does:** Checks for range expressions `x..y` where both `x` and `y`
105 /// are constant and `x` is greater or equal to `y`.
107 /// **Why is this bad?** Empty ranges yield no values so iterating them is a no-op.
108 /// Moreover, trying to use a reversed range to index a slice will panic at run-time.
110 /// **Known problems:** None.
116 /// (10..=0).for_each(|x| println!("{}", x));
118 /// let arr = [1, 2, 3, 4, 5];
119 /// let sub = &arr[3..1];
125 /// (0..=10).rev().for_each(|x| println!("{}", x));
127 /// let arr = [1, 2, 3, 4, 5];
128 /// let sub = &arr[1..3];
131 pub REVERSED_EMPTY_RANGES,
133 "reversing the limits of range expressions, resulting in empty ranges"
136 declare_clippy_lint! {
137 /// **What it does:** Checks for expressions like `x >= 3 && x < 8` that could
138 /// be more readably expressed as `(3..8).contains(x)`.
140 /// **Why is this bad?** `contains` expresses the intent better and has less
141 /// failure modes (such as fencepost errors or using `||` instead of `&&`).
143 /// **Known problems:** None.
151 /// assert!(x >= 3 && x < 8);
156 /// assert!((3..8).contains(&x));
158 pub MANUAL_RANGE_CONTAINS,
160 "manually reimplementing {`Range`, `RangeInclusive`}`::contains`"
163 declare_lint_pass!(Ranges => [
167 REVERSED_EMPTY_RANGES,
168 MANUAL_RANGE_CONTAINS,
171 impl<'tcx> LateLintPass<'tcx> for Ranges {
172 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
174 ExprKind::MethodCall(ref path, _, ref args, _) => {
175 check_range_zip_with_len(cx, path, args, expr.span);
177 ExprKind::Binary(ref op, ref l, ref r) => {
178 check_possible_range_contains(cx, op.node, l, r, expr.span);
183 check_exclusive_range_plus_one(cx, expr);
184 check_inclusive_range_minus_one(cx, expr);
185 check_reversed_empty_range(cx, expr);
189 fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'_>, r: &Expr<'_>, span: Span) {
190 let combine_and = match op {
191 BinOpKind::And | BinOpKind::BitAnd => true,
192 BinOpKind::Or | BinOpKind::BitOr => false,
195 // value, name, order (higher/lower), inclusiveness
196 if let (Some((lval, lname, name_span, lval_span, lord, linc)), Some((rval, rname, _, rval_span, rord, rinc))) =
197 (check_range_bounds(cx, l), check_range_bounds(cx, r))
199 // we only lint comparisons on the same name and with different
201 if lname != rname || lord == rord {
204 let ord = Constant::partial_cmp(cx.tcx, cx.typeck_results().expr_ty(l), &lval, &rval);
205 if combine_and && ord == Some(rord) {
206 // order lower bound and upper bound
207 let (l_span, u_span, l_inc, u_inc) = if rord == Ordering::Less {
208 (lval_span, rval_span, linc, rinc)
210 (rval_span, lval_span, rinc, linc)
212 // we only lint inclusive lower bounds
216 let (range_type, range_op) = if u_inc {
217 ("RangeInclusive", "..=")
221 let mut applicability = Applicability::MachineApplicable;
222 let name = snippet_with_applicability(cx, name_span, "_", &mut applicability);
223 let lo = snippet_with_applicability(cx, l_span, "_", &mut applicability);
224 let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability);
227 MANUAL_RANGE_CONTAINS,
229 &format!("manual `{}::contains` implementation", range_type),
231 format!("({}{}{}).contains(&{})", lo, range_op, hi, name),
234 } else if !combine_and && ord == Some(lord) {
236 // order lower bound and upper bound
237 let (l_span, u_span, l_inc, u_inc) = if lord == Ordering::Less {
238 (lval_span, rval_span, linc, rinc)
240 (rval_span, lval_span, rinc, linc)
245 let (range_type, range_op) = if u_inc {
248 ("RangeInclusive", "..=")
250 let mut applicability = Applicability::MachineApplicable;
251 let name = snippet_with_applicability(cx, name_span, "_", &mut applicability);
252 let lo = snippet_with_applicability(cx, l_span, "_", &mut applicability);
253 let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability);
256 MANUAL_RANGE_CONTAINS,
258 &format!("manual `!{}::contains` implementation", range_type),
260 format!("!({}{}{}).contains(&{})", lo, range_op, hi, name),
267 fn check_range_bounds(cx: &LateContext<'_>, ex: &Expr<'_>) -> Option<(Constant, Ident, Span, Span, Ordering, bool)> {
268 if let ExprKind::Binary(ref op, ref l, ref r) = ex.kind {
269 let (inclusive, ordering) = match op.node {
270 BinOpKind::Gt => (false, Ordering::Greater),
271 BinOpKind::Ge => (true, Ordering::Greater),
272 BinOpKind::Lt => (false, Ordering::Less),
273 BinOpKind::Le => (true, Ordering::Less),
276 if let Some(id) = match_ident(l) {
277 if let Some((c, _)) = constant(cx, cx.typeck_results(), r) {
278 return Some((c, id, l.span, r.span, ordering, inclusive));
280 } else if let Some(id) = match_ident(r) {
281 if let Some((c, _)) = constant(cx, cx.typeck_results(), l) {
282 return Some((c, id, r.span, l.span, ordering.reverse(), inclusive));
289 fn match_ident(e: &Expr<'_>) -> Option<Ident> {
290 if let ExprKind::Path(ref qpath) = e.kind {
291 if let Some(seg) = single_segment_path(qpath) {
292 if seg.args.is_none() {
293 return Some(seg.ident);
300 fn check_range_zip_with_len(cx: &LateContext<'_>, path: &PathSegment<'_>, args: &[Expr<'_>], span: Span) {
301 let name = path.ident.as_str();
302 if name == "zip" && args.len() == 2 {
303 let iter = &args[0].kind;
304 let zip_arg = &args[1];
307 if let ExprKind::MethodCall(ref iter_path, _, ref iter_args, _) = *iter;
308 if iter_path.ident.name == sym::iter;
309 // range expression in `.zip()` call: `0..x.len()`
310 if let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::range(zip_arg);
311 if is_integer_const(cx, start, 0);
313 if let ExprKind::MethodCall(ref len_path, _, ref len_args, _) = end.kind;
314 if len_path.ident.name == sym!(len) && len_args.len() == 1;
315 // `.iter()` and `.len()` called on same `Path`
316 if let ExprKind::Path(QPath::Resolved(_, ref iter_path)) = iter_args[0].kind;
317 if let ExprKind::Path(QPath::Resolved(_, ref len_path)) = len_args[0].kind;
318 if SpanlessEq::new(cx).eq_path_segments(&iter_path.segments, &len_path.segments);
323 &format!("it is more idiomatic to use `{}.iter().enumerate()`",
324 snippet(cx, iter_args[0].span, "_"))
331 // exclusive range plus one: `x..(y+1)`
332 fn check_exclusive_range_plus_one(cx: &LateContext<'_>, expr: &Expr<'_>) {
334 if let Some(higher::Range {
337 limits: RangeLimits::HalfOpen
338 }) = higher::range(expr);
339 if let Some(y) = y_plus_one(cx, end);
341 let span = if expr.span.from_expansion() {
353 "an inclusive range would be more readable",
355 let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").to_string());
356 let end = Sugg::hir(cx, y, "y");
357 if let Some(is_wrapped) = &snippet_opt(cx, span) {
358 if is_wrapped.starts_with('(') && is_wrapped.ends_with(')') {
359 diag.span_suggestion(
362 format!("({}..={})", start, end),
363 Applicability::MaybeIncorrect,
366 diag.span_suggestion(
369 format!("{}..={}", start, end),
370 Applicability::MachineApplicable, // snippet
380 // inclusive range minus one: `x..=(y-1)`
381 fn check_inclusive_range_minus_one(cx: &LateContext<'_>, expr: &Expr<'_>) {
383 if let Some(higher::Range { start, end: Some(end), limits: RangeLimits::Closed }) = higher::range(expr);
384 if let Some(y) = y_minus_one(cx, end);
390 "an exclusive range would be more readable",
392 let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").to_string());
393 let end = Sugg::hir(cx, y, "y");
394 diag.span_suggestion(
397 format!("{}..{}", start, end),
398 Applicability::MachineApplicable, // snippet
406 fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
407 fn inside_indexing_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
409 get_parent_expr(cx, expr),
411 kind: ExprKind::Index(..),
417 fn is_for_loop_arg(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
418 let mut cur_expr = expr;
419 while let Some(parent_expr) = get_parent_expr(cx, cur_expr) {
420 match higher::for_loop(parent_expr) {
421 Some((_, args, _)) if args.hir_id == expr.hir_id => return true,
422 _ => cur_expr = parent_expr,
429 fn is_empty_range(limits: RangeLimits, ordering: Ordering) -> bool {
431 RangeLimits::HalfOpen => ordering != Ordering::Less,
432 RangeLimits::Closed => ordering == Ordering::Greater,
437 if let Some(higher::Range { start: Some(start), end: Some(end), limits }) = higher::range(expr);
438 let ty = cx.typeck_results().expr_ty(start);
439 if let ty::Int(_) | ty::Uint(_) = ty.kind();
440 if let Some((start_idx, _)) = constant(cx, cx.typeck_results(), start);
441 if let Some((end_idx, _)) = constant(cx, cx.typeck_results(), end);
442 if let Some(ordering) = Constant::partial_cmp(cx.tcx, ty, &start_idx, &end_idx);
443 if is_empty_range(limits, ordering);
445 if inside_indexing_expr(cx, expr) {
446 // Avoid linting `N..N` as it has proven to be useful, see #5689 and #5628 ...
447 if ordering != Ordering::Equal {
450 REVERSED_EMPTY_RANGES,
452 "this range is reversed and using it to index a slice will panic at run-time",
455 // ... except in for loop arguments for backwards compatibility with `reverse_range_loop`
456 } else if ordering != Ordering::Equal || is_for_loop_arg(cx, expr) {
459 REVERSED_EMPTY_RANGES,
461 "this range is empty so it will yield no values",
463 if ordering != Ordering::Equal {
464 let start_snippet = snippet(cx, start.span, "_");
465 let end_snippet = snippet(cx, end.span, "_");
466 let dots = match limits {
467 RangeLimits::HalfOpen => "..",
468 RangeLimits::Closed => "..="
471 diag.span_suggestion(
473 "consider using the following if you are attempting to iterate over this \
475 format!("({}{}{}).rev()", end_snippet, dots, start_snippet),
476 Applicability::MaybeIncorrect,
486 fn y_plus_one<'t>(cx: &LateContext<'_>, expr: &'t Expr<'_>) -> Option<&'t Expr<'t>> {
490 node: BinOpKind::Add, ..
495 if is_integer_const(cx, lhs, 1) {
497 } else if is_integer_const(cx, rhs, 1) {
507 fn y_minus_one<'t>(cx: &LateContext<'_>, expr: &'t Expr<'_>) -> Option<&'t Expr<'t>> {
511 node: BinOpKind::Sub, ..
515 ) if is_integer_const(cx, rhs, 1) => Some(lhs),