|
| 1 | +use crate::{LateContext, LateLintPass, LintContext}; |
| 2 | + |
| 3 | +use rustc_ast::ast::LitKind; |
| 4 | +use rustc_errors::fluent; |
| 5 | +use rustc_hir::{Expr, ExprField, ExprKind, MatchSource, PatKind, StmtKind}; |
| 6 | +use rustc_span::Span; |
| 7 | + |
| 8 | +declare_lint! { |
| 9 | + /// The `empty_iterator_range` lint checks for empty iterator ranges. |
| 10 | + /// |
| 11 | + /// ### Example |
| 12 | + /// |
| 13 | + /// ```rust |
| 14 | + /// for i in 10..0 { /* ... */ } |
| 15 | + /// ``` |
| 16 | + /// |
| 17 | + /// {{produces}} |
| 18 | + /// |
| 19 | + /// ### Explanation |
| 20 | + /// |
| 21 | + /// It is usually a mistake to have a statement that has no effect. |
| 22 | + pub EMPTY_ITERATOR_RANGE, |
| 23 | + Warn, |
| 24 | + "empty iterator range" |
| 25 | +} |
| 26 | + |
| 27 | +declare_lint_pass!(EmptyIteratorRange => [EMPTY_ITERATOR_RANGE]); |
| 28 | + |
| 29 | +impl<'tcx> LateLintPass<'tcx> for EmptyIteratorRange { |
| 30 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 31 | + let Some(for_expr) = extract_for_loop(expr) else { return }; |
| 32 | + let span = expr.span.with_hi(for_expr.span.hi()); |
| 33 | + if let ExprKind::Struct(_, expr_fields, _) = &for_expr.kind { |
| 34 | + is_empty_range(cx, span, expr_fields); |
| 35 | + } else if let ExprKind::MethodCall(_, expr, _, _) = &for_expr.kind { |
| 36 | + if let ExprKind::Struct(_, expr_fields, _) = &expr.kind { |
| 37 | + is_empty_range(cx, span, expr_fields); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +fn extract_for_loop<'tcx>(expr: &Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> { |
| 44 | + if let ExprKind::DropTemps(e) = expr.kind |
| 45 | + && let ExprKind::Match(iterexpr, [arm], MatchSource::ForLoopDesugar) = e.kind |
| 46 | + && let ExprKind::Call(_, [arg]) = iterexpr.kind |
| 47 | + && let ExprKind::Loop(block, ..) = arm.body.kind |
| 48 | + && let [stmt] = block.stmts |
| 49 | + && let StmtKind::Expr(e) = stmt.kind |
| 50 | + && let ExprKind::Match(_, [_, some_arm], _) = e.kind |
| 51 | + && let PatKind::Struct(..) = some_arm.pat.kind |
| 52 | + { |
| 53 | + Some(arg) |
| 54 | + } else { |
| 55 | + None |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fn is_empty_range(cx: &LateContext<'_>, span: Span, expr_fields: &[ExprField<'_>]) { |
| 60 | + let mut prev = 0u128; |
| 61 | + for (index, expr_field) in expr_fields.iter().enumerate() { |
| 62 | + if let ExprKind::Lit(lit) = &expr_field.expr.kind { |
| 63 | + if let LitKind::Int(u, _) = lit.node { |
| 64 | + if index == 0 { |
| 65 | + prev = u; |
| 66 | + } else if prev > u { |
| 67 | + cx.struct_span_lint( |
| 68 | + EMPTY_ITERATOR_RANGE, |
| 69 | + span, |
| 70 | + fluent::lint::empty_iter_ranges, |
| 71 | + |lint| { |
| 72 | + lint.span_label(span, fluent::lint::clarification) |
| 73 | + .note(fluent::lint::note) |
| 74 | + .help(fluent::lint::help) |
| 75 | + }, |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments