|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_then, match_def_path, paths, source::snippet}; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::*; |
| 4 | +use rustc_lint::LateContext; |
| 5 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 6 | +use rustc_span::{symbol::Ident, Span}; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// Looks for loops that check for emptiness of a `Vec` in the condition and pop an element |
| 11 | + /// in the body as a separate operation. |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// Such loops can be written in a more idiomatic way by using a while..let loop and directly |
| 15 | + /// pattern matching on the return value of `Vec::pop()`. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```rust |
| 19 | + /// let mut numbers = vec![1, 2, 3, 4, 5]; |
| 20 | + /// while !numbers.is_empty() { |
| 21 | + /// let number = numbers.pop().unwrap(); |
| 22 | + /// // use `number` |
| 23 | + /// } |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```rust |
| 27 | + /// let mut numbers = vec![1, 2, 3, 4, 5]; |
| 28 | + /// while let Some(number) = numbers.pop() { |
| 29 | + /// // use `number` |
| 30 | + /// } |
| 31 | + /// ``` |
| 32 | + #[clippy::version = "1.70.0"] |
| 33 | + pub WHILE_POP_UNWRAP, |
| 34 | + style, |
| 35 | + "checking for emptiness of a `Vec` in the loop condition and popping an element in the body" |
| 36 | +} |
| 37 | +declare_lint_pass!(WhilePopUnwrap => [WHILE_POP_UNWRAP]); |
| 38 | + |
| 39 | +fn report_lint<'tcx>( |
| 40 | + cx: &LateContext<'tcx>, |
| 41 | + pop_span: Span, |
| 42 | + ident: Option<Ident>, |
| 43 | + loop_span: Span, |
| 44 | + receiver_span: Span, |
| 45 | +) { |
| 46 | + span_lint_and_then( |
| 47 | + cx, |
| 48 | + WHILE_POP_UNWRAP, |
| 49 | + pop_span, |
| 50 | + "you seem to be trying to pop elements from a `Vec` in a loop", |
| 51 | + |diag| { |
| 52 | + diag.span_suggestion( |
| 53 | + loop_span, |
| 54 | + "try", |
| 55 | + format!( |
| 56 | + "while let Some({}) = {}.pop()", |
| 57 | + ident.as_ref().map(Ident::as_str).unwrap_or("element"), |
| 58 | + snippet(cx, receiver_span, "..") |
| 59 | + ), |
| 60 | + Applicability::MaybeIncorrect, |
| 61 | + ) |
| 62 | + .note("this while loop can be written in a more idiomatic way"); |
| 63 | + }, |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +fn match_method_call(cx: &LateContext<'_>, expr: &Expr<'_>, method: &[&str]) -> bool { |
| 68 | + if let ExprKind::MethodCall(..) = expr.kind |
| 69 | + && let Some(id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) |
| 70 | + && match_def_path(cx, id, method) |
| 71 | + { |
| 72 | + true |
| 73 | + } else { |
| 74 | + false |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +fn is_vec_pop<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> bool { |
| 79 | + match_method_call(cx, expr, &paths::VEC_POP) |
| 80 | +} |
| 81 | + |
| 82 | +fn is_vec_pop_unwrap<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> bool { |
| 83 | + if let ExprKind::MethodCall(_, inner, ..) = expr.kind |
| 84 | + && (match_method_call(cx, expr, &paths::OPTION_UNWRAP) || match_method_call(cx, expr, &paths::OPTION_EXPECT)) |
| 85 | + && is_vec_pop(cx, inner) |
| 86 | + { |
| 87 | + true |
| 88 | + } else { |
| 89 | + false |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +fn check_local<'tcx>(cx: &LateContext<'tcx>, stmt: &Stmt<'_>, loop_span: Span, recv_span: Span) { |
| 94 | + if let StmtKind::Local(local) = stmt.kind |
| 95 | + && let PatKind::Binding(.., ident, _) = local.pat.kind |
| 96 | + && let Some(init) = local.init |
| 97 | + && let ExprKind::MethodCall(_, inner, ..) = init.kind |
| 98 | + && is_vec_pop_unwrap(cx, init) |
| 99 | + { |
| 100 | + report_lint(cx, init.span.to(inner.span), Some(ident), loop_span, recv_span); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +fn check_call_arguments<'tcx>(cx: &LateContext<'tcx>, stmt: &Stmt<'_>, loop_span: Span, recv_span: Span) { |
| 105 | + if let StmtKind::Semi(expr) | StmtKind::Expr(expr) = stmt.kind { |
| 106 | + if let ExprKind::MethodCall(_, _, args, _) | ExprKind::Call(_, args) = expr.kind { |
| 107 | + let offending_arg = args.iter().find_map(|arg| is_vec_pop_unwrap(cx, arg).then(|| arg.span)); |
| 108 | + |
| 109 | + if let Some(offending_arg) = offending_arg { |
| 110 | + report_lint(cx, offending_arg, None, loop_span, recv_span); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, cond: &'tcx Expr<'_>, body: &'tcx Expr<'_>) { |
| 117 | + if let ExprKind::Unary(UnOp::Not, cond) = cond.kind |
| 118 | + && let ExprKind::MethodCall(_, Expr { span: recv_span, .. }, _, _) = cond.kind |
| 119 | + && match_method_call(cx, cond, &paths::VEC_IS_EMPTY) |
| 120 | + && let ExprKind::Block(body, _) = body.kind |
| 121 | + && let Some(stmt) = body.stmts.first() |
| 122 | + { |
| 123 | + check_local(cx, stmt, cond.span, *recv_span); |
| 124 | + check_call_arguments(cx, stmt, cond.span, *recv_span); |
| 125 | + } |
| 126 | +} |
0 commit comments