|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
| 3 | +use clippy_utils::diagnostics::span_lint; |
| 4 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 5 | +use clippy_utils::visitors::for_each_expr; |
| 6 | +use clippy_utils::{higher, peel_hir_expr_while, SpanlessEq}; |
| 7 | +use rustc_hir::{Expr, ExprKind, UnOp}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_session::declare_lint_pass; |
| 10 | +use rustc_span::symbol::Symbol; |
| 11 | +use rustc_span::{sym, Span}; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for usage of `contains` to see if a value is not |
| 16 | + /// present on `HashSet` followed by a `insert`. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// Using just `insert` and checking the returned `bool` is more efficient. |
| 20 | + /// |
| 21 | + /// ### Known problems |
| 22 | + /// In case the value that wants to be inserted is borrowed and also expensive or impossible |
| 23 | + /// to clone. In such a scenario, the developer might want to check with `contains` before inserting, |
| 24 | + /// to avoid the clone. In this case, it will report a false positive. |
| 25 | + /// |
| 26 | + /// ### Example |
| 27 | + /// ```rust |
| 28 | + /// use std::collections::HashSet; |
| 29 | + /// let mut set = HashSet::new(); |
| 30 | + /// let value = 5; |
| 31 | + /// if !set.contains(&value) { |
| 32 | + /// set.insert(value); |
| 33 | + /// println!("inserted {value:?}"); |
| 34 | + /// } |
| 35 | + /// ``` |
| 36 | + /// Use instead: |
| 37 | + /// ```rust |
| 38 | + /// use std::collections::HashSet; |
| 39 | + /// let mut set = HashSet::new(); |
| 40 | + /// let value = 5; |
| 41 | + /// if set.insert(&value) { |
| 42 | + /// println!("inserted {value:?}"); |
| 43 | + /// } |
| 44 | + /// ``` |
| 45 | + #[clippy::version = "1.80.0"] |
| 46 | + pub SET_CONTAINS_OR_INSERT, |
| 47 | + nursery, |
| 48 | + "call to `HashSet::contains` followed by `HashSet::insert`" |
| 49 | +} |
| 50 | + |
| 51 | +declare_lint_pass!(HashsetInsertAfterContains => [SET_CONTAINS_OR_INSERT]); |
| 52 | + |
| 53 | +impl<'tcx> LateLintPass<'tcx> for HashsetInsertAfterContains { |
| 54 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 55 | + if !expr.span.from_expansion() |
| 56 | + && let Some(higher::If { |
| 57 | + cond: cond_expr, |
| 58 | + then: then_expr, |
| 59 | + .. |
| 60 | + }) = higher::If::hir(expr) |
| 61 | + && let Some(contains_expr) = try_parse_op_call(cx, cond_expr, sym!(contains))//try_parse_contains(cx, cond_expr) |
| 62 | + && let Some(insert_expr) = find_insert_calls(cx, &contains_expr, then_expr) |
| 63 | + { |
| 64 | + span_lint( |
| 65 | + cx, |
| 66 | + SET_CONTAINS_OR_INSERT, |
| 67 | + vec![contains_expr.span, insert_expr.span], |
| 68 | + "usage of `HashSet::insert` after `HashSet::contains`", |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +struct OpExpr<'tcx> { |
| 75 | + receiver: &'tcx Expr<'tcx>, |
| 76 | + value: &'tcx Expr<'tcx>, |
| 77 | + span: Span, |
| 78 | +} |
| 79 | + |
| 80 | +fn try_parse_op_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, symbol: Symbol) -> Option<OpExpr<'tcx>> { |
| 81 | + let expr = peel_hir_expr_while(expr, |e| { |
| 82 | + if let ExprKind::Unary(UnOp::Not, e) = e.kind { |
| 83 | + Some(e) |
| 84 | + } else { |
| 85 | + None |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + if let ExprKind::MethodCall(path, receiver, [value], span) = expr.kind { |
| 90 | + let value = value.peel_borrows(); |
| 91 | + let value = peel_hir_expr_while(value, |e| { |
| 92 | + if let ExprKind::Unary(UnOp::Deref, e) = e.kind { |
| 93 | + Some(e) |
| 94 | + } else { |
| 95 | + None |
| 96 | + } |
| 97 | + }); |
| 98 | + let receiver = receiver.peel_borrows(); |
| 99 | + let receiver_ty = cx.typeck_results().expr_ty(receiver).peel_refs(); |
| 100 | + if value.span.eq_ctxt(expr.span) |
| 101 | + && is_type_diagnostic_item(cx, receiver_ty, sym::HashSet) |
| 102 | + && path.ident.name == symbol |
| 103 | + { |
| 104 | + return Some(OpExpr { receiver, value, span }); |
| 105 | + } |
| 106 | + } |
| 107 | + None |
| 108 | +} |
| 109 | + |
| 110 | +fn find_insert_calls<'tcx>( |
| 111 | + cx: &LateContext<'tcx>, |
| 112 | + contains_expr: &OpExpr<'tcx>, |
| 113 | + expr: &'tcx Expr<'_>, |
| 114 | +) -> Option<OpExpr<'tcx>> { |
| 115 | + for_each_expr(cx, expr, |e| { |
| 116 | + if let Some(insert_expr) = try_parse_op_call(cx, e, sym!(insert)) |
| 117 | + && SpanlessEq::new(cx).eq_expr(contains_expr.receiver, insert_expr.receiver) |
| 118 | + && SpanlessEq::new(cx).eq_expr(contains_expr.value, insert_expr.value) |
| 119 | + { |
| 120 | + ControlFlow::Break(insert_expr) |
| 121 | + } else { |
| 122 | + ControlFlow::Continue(()) |
| 123 | + } |
| 124 | + }) |
| 125 | +} |
0 commit comments