|
| 1 | +use crate::consts::{constant, Constant}; |
1 | 2 | use if_chain::if_chain;
|
2 | 3 | use rustc_ast::ast::RangeLimits;
|
3 | 4 | use rustc_errors::Applicability;
|
4 | 5 | use rustc_hir::{BinOpKind, Expr, ExprKind, QPath};
|
5 | 6 | use rustc_lint::{LateContext, LateLintPass};
|
| 7 | +use rustc_middle::ty; |
6 | 8 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
7 | 9 | use rustc_span::source_map::Spanned;
|
| 10 | +use std::cmp::Ordering; |
8 | 11 |
|
9 | 12 | use crate::utils::sugg::Sugg;
|
| 13 | +use crate::utils::{get_parent_expr, is_integer_const, snippet, snippet_opt, span_lint, span_lint_and_then}; |
10 | 14 | use crate::utils::{higher, SpanlessEq};
|
11 |
| -use crate::utils::{is_integer_const, snippet, snippet_opt, span_lint, span_lint_and_then}; |
12 | 15 |
|
13 | 16 | declare_clippy_lint! {
|
14 | 17 | /// **What it does:** Checks for zipping a collection with the range of
|
@@ -84,10 +87,44 @@ declare_clippy_lint! {
|
84 | 87 | "`x..=(y-1)` reads better as `x..y`"
|
85 | 88 | }
|
86 | 89 |
|
| 90 | +declare_clippy_lint! { |
| 91 | + /// **What it does:** Checks for range expressions `x..y` where both `x` and `y` |
| 92 | + /// are constant and `x` is greater or equal to `y`. |
| 93 | + /// |
| 94 | + /// **Why is this bad?** Empty ranges yield no values so iterating them is a no-op. |
| 95 | + /// Moreover, trying to use a reversed range to index a slice will panic at run-time. |
| 96 | + /// |
| 97 | + /// **Known problems:** None. |
| 98 | + /// |
| 99 | + /// **Example:** |
| 100 | + /// |
| 101 | + /// ```rust |
| 102 | + /// fn main() { |
| 103 | + /// (10..=0).for_each(|x| println!("{}", x)); |
| 104 | + /// |
| 105 | + /// let arr = [1, 2, 3, 4, 5]; |
| 106 | + /// let sub = &arr[3..1]; |
| 107 | + /// } |
| 108 | + /// ``` |
| 109 | + /// Use instead: |
| 110 | + /// ```rust |
| 111 | + /// fn main() { |
| 112 | + /// (0..=10).rev().for_each(|x| println!("{}", x)); |
| 113 | + /// |
| 114 | + /// let arr = [1, 2, 3, 4, 5]; |
| 115 | + /// let sub = &arr[1..3]; |
| 116 | + /// } |
| 117 | + /// ``` |
| 118 | + pub REVERSED_EMPTY_RANGES, |
| 119 | + correctness, |
| 120 | + "reversing the limits of range expressions, resulting in empty ranges" |
| 121 | +} |
| 122 | + |
87 | 123 | declare_lint_pass!(Ranges => [
|
88 | 124 | RANGE_ZIP_WITH_LEN,
|
89 | 125 | RANGE_PLUS_ONE,
|
90 |
| - RANGE_MINUS_ONE |
| 126 | + RANGE_MINUS_ONE, |
| 127 | + REVERSED_EMPTY_RANGES, |
91 | 128 | ]);
|
92 | 129 |
|
93 | 130 | impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Ranges {
|
@@ -124,6 +161,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Ranges {
|
124 | 161 |
|
125 | 162 | check_exclusive_range_plus_one(cx, expr);
|
126 | 163 | check_inclusive_range_minus_one(cx, expr);
|
| 164 | + check_reversed_empty_range(cx, expr); |
127 | 165 | }
|
128 | 166 | }
|
129 | 167 |
|
@@ -202,6 +240,76 @@ fn check_inclusive_range_minus_one(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
|
202 | 240 | }
|
203 | 241 | }
|
204 | 242 |
|
| 243 | +fn check_reversed_empty_range(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { |
| 244 | + fn inside_indexing_expr(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool { |
| 245 | + matches!( |
| 246 | + get_parent_expr(cx, expr), |
| 247 | + Some(Expr { |
| 248 | + kind: ExprKind::Index(..), |
| 249 | + .. |
| 250 | + }) |
| 251 | + ) |
| 252 | + } |
| 253 | + |
| 254 | + fn is_empty_range(limits: RangeLimits, ordering: Ordering) -> bool { |
| 255 | + match limits { |
| 256 | + RangeLimits::HalfOpen => ordering != Ordering::Less, |
| 257 | + RangeLimits::Closed => ordering == Ordering::Greater, |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + if_chain! { |
| 262 | + if let Some(higher::Range { start: Some(start), end: Some(end), limits }) = higher::range(cx, expr); |
| 263 | + let ty = cx.tables.expr_ty(start); |
| 264 | + if let ty::Int(_) | ty::Uint(_) = ty.kind; |
| 265 | + if let Some((start_idx, _)) = constant(cx, cx.tables, start); |
| 266 | + if let Some((end_idx, _)) = constant(cx, cx.tables, end); |
| 267 | + if let Some(ordering) = Constant::partial_cmp(cx.tcx, ty, &start_idx, &end_idx); |
| 268 | + if is_empty_range(limits, ordering); |
| 269 | + then { |
| 270 | + if inside_indexing_expr(cx, expr) { |
| 271 | + let (reason, outcome) = if ordering == Ordering::Equal { |
| 272 | + ("empty", "always yield an empty slice") |
| 273 | + } else { |
| 274 | + ("reversed", "panic at run-time") |
| 275 | + }; |
| 276 | + |
| 277 | + span_lint( |
| 278 | + cx, |
| 279 | + REVERSED_EMPTY_RANGES, |
| 280 | + expr.span, |
| 281 | + &format!("this range is {} and using it to index a slice will {}", reason, outcome), |
| 282 | + ); |
| 283 | + } else { |
| 284 | + span_lint_and_then( |
| 285 | + cx, |
| 286 | + REVERSED_EMPTY_RANGES, |
| 287 | + expr.span, |
| 288 | + "this range is empty so it will yield no values", |
| 289 | + |diag| { |
| 290 | + if ordering != Ordering::Equal { |
| 291 | + let start_snippet = snippet(cx, start.span, "_"); |
| 292 | + let end_snippet = snippet(cx, end.span, "_"); |
| 293 | + let dots = match limits { |
| 294 | + RangeLimits::HalfOpen => "..", |
| 295 | + RangeLimits::Closed => "..=" |
| 296 | + }; |
| 297 | + |
| 298 | + diag.span_suggestion( |
| 299 | + expr.span, |
| 300 | + "consider using the following if you are attempting to iterate over this \ |
| 301 | + range in reverse", |
| 302 | + format!("({}{}{}).rev()", end_snippet, dots, start_snippet), |
| 303 | + Applicability::MaybeIncorrect, |
| 304 | + ); |
| 305 | + } |
| 306 | + }, |
| 307 | + ); |
| 308 | + } |
| 309 | + } |
| 310 | + } |
| 311 | +} |
| 312 | + |
205 | 313 | fn y_plus_one<'t>(cx: &LateContext<'_, '_>, expr: &'t Expr<'_>) -> Option<&'t Expr<'t>> {
|
206 | 314 | match expr.kind {
|
207 | 315 | ExprKind::Binary(
|
|
0 commit comments