|
| 1 | +use clippy_utils::{ |
| 2 | + diagnostics::{multispan_sugg_with_applicability, span_lint_and_then}, |
| 3 | + match_def_path, paths, |
| 4 | + source::snippet, |
| 5 | + SpanlessEq, |
| 6 | +}; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{Expr, ExprKind, Pat, Stmt, StmtKind, UnOp}; |
| 9 | +use rustc_lint::LateContext; |
| 10 | +use rustc_span::Span; |
| 11 | +use std::borrow::Cow; |
| 12 | + |
| 13 | +use super::WHILE_POP_UNWRAP; |
| 14 | + |
| 15 | +/// The kind of statement that the `pop()` call appeared in. |
| 16 | +/// |
| 17 | +/// Depending on whether the value was assigned to a variable or not changes what pattern |
| 18 | +/// we use for the suggestion. |
| 19 | +enum PopStmt<'hir> { |
| 20 | + /// `x.pop().unwrap()` was and assigned to a variable. |
| 21 | + /// The pattern of this local variable will be used and the local statement |
| 22 | + /// is deleted in the suggestion. |
| 23 | + Local(&'hir Pat<'hir>), |
| 24 | + /// `x.pop().unwrap()` appeared in an arbitrary expression and was not assigned to a variable. |
| 25 | + /// The suggestion will use some placeholder identifier and the `x.pop().unwrap()` expression |
| 26 | + /// is replaced with that identifier. |
| 27 | + Anonymous, |
| 28 | +} |
| 29 | + |
| 30 | +fn report_lint(cx: &LateContext<'_>, pop_span: Span, pop_stmt_kind: PopStmt<'_>, loop_span: Span, receiver_span: Span) { |
| 31 | + span_lint_and_then( |
| 32 | + cx, |
| 33 | + WHILE_POP_UNWRAP, |
| 34 | + pop_span, |
| 35 | + "you seem to be trying to pop elements from a `Vec` in a loop", |
| 36 | + |diag| { |
| 37 | + let (pat, pop_replacement) = match &pop_stmt_kind { |
| 38 | + PopStmt::Local(pat) => (snippet(cx, pat.span, ".."), String::new()), |
| 39 | + PopStmt::Anonymous => (Cow::Borrowed("element"), "element".into()), |
| 40 | + }; |
| 41 | + |
| 42 | + let loop_replacement = format!("while let Some({}) = {}.pop()", pat, snippet(cx, receiver_span, "..")); |
| 43 | + multispan_sugg_with_applicability( |
| 44 | + diag, |
| 45 | + "consider using a `while..let` loop", |
| 46 | + Applicability::MachineApplicable, |
| 47 | + [(loop_span, loop_replacement), (pop_span, pop_replacement)], |
| 48 | + ); |
| 49 | + }, |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +fn match_method_call(cx: &LateContext<'_>, expr: &Expr<'_>, method: &[&str]) -> bool { |
| 54 | + if let ExprKind::MethodCall(..) = expr.kind |
| 55 | + && let Some(id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) |
| 56 | + { |
| 57 | + match_def_path(cx, id, method) |
| 58 | + } else { |
| 59 | + false |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn is_vec_pop_unwrap(cx: &LateContext<'_>, expr: &Expr<'_>, is_empty_recv: &Expr<'_>) -> bool { |
| 64 | + if (match_method_call(cx, expr, &paths::OPTION_UNWRAP) || match_method_call(cx, expr, &paths::OPTION_EXPECT)) |
| 65 | + && let ExprKind::MethodCall(_, unwrap_recv, ..) = expr.kind |
| 66 | + && match_method_call(cx, unwrap_recv, &paths::VEC_POP) |
| 67 | + && let ExprKind::MethodCall(_, pop_recv, ..) = unwrap_recv.kind |
| 68 | + { |
| 69 | + // make sure they're the same `Vec` |
| 70 | + SpanlessEq::new(cx).eq_expr(pop_recv, is_empty_recv) |
| 71 | + } else { |
| 72 | + false |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn check_local(cx: &LateContext<'_>, stmt: &Stmt<'_>, is_empty_recv: &Expr<'_>, loop_span: Span) { |
| 77 | + if let StmtKind::Local(local) = stmt.kind |
| 78 | + && let Some(init) = local.init |
| 79 | + && is_vec_pop_unwrap(cx, init, is_empty_recv) |
| 80 | + { |
| 81 | + report_lint(cx, stmt.span, PopStmt::Local(&local.pat), loop_span, is_empty_recv.span); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn check_call_arguments(cx: &LateContext<'_>, stmt: &Stmt<'_>, is_empty_recv: &Expr<'_>, loop_span: Span) { |
| 86 | + if let StmtKind::Semi(expr) | StmtKind::Expr(expr) = stmt.kind { |
| 87 | + if let ExprKind::MethodCall(.., args, _) | ExprKind::Call(_, args) = expr.kind { |
| 88 | + let offending_arg = args |
| 89 | + .iter() |
| 90 | + .find_map(|arg| is_vec_pop_unwrap(cx, arg, is_empty_recv).then_some(arg.span)); |
| 91 | + |
| 92 | + if let Some(offending_arg) = offending_arg { |
| 93 | + report_lint(cx, offending_arg, PopStmt::Anonymous, loop_span, is_empty_recv.span); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, full_cond: &'tcx Expr<'_>, body: &'tcx Expr<'_>, loop_span: Span) { |
| 100 | + if let ExprKind::Unary(UnOp::Not, cond) = full_cond.kind |
| 101 | + && let ExprKind::MethodCall(_, is_empty_recv, _, _) = cond.kind |
| 102 | + && match_method_call(cx, cond, &paths::VEC_IS_EMPTY) |
| 103 | + && let ExprKind::Block(body, _) = body.kind |
| 104 | + && let Some(stmt) = body.stmts.first() |
| 105 | + { |
| 106 | + check_local(cx, stmt, is_empty_recv, loop_span); |
| 107 | + check_call_arguments(cx, stmt, is_empty_recv, loop_span); |
| 108 | + } |
| 109 | +} |
0 commit comments