|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
| 3 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 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::{sym, Span}; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for usage of `contains` to see if a value is not |
| 15 | + /// present on `HashSet` followed by a `insert`. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// Using just `insert` and checking the returned `bool` is more efficient. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```rust |
| 22 | + /// use std::collections::HashSet; |
| 23 | + /// let mut set = HashSet::new(); |
| 24 | + /// let value = 5; |
| 25 | + /// if !set.contains(&value) { |
| 26 | + /// set.insert(value); |
| 27 | + /// println!("inserted {value:?}"); |
| 28 | + /// } |
| 29 | + /// ``` |
| 30 | + /// Use instead: |
| 31 | + /// ```rust |
| 32 | + /// use std::collections::HashSet; |
| 33 | + /// let mut set = HashSet::new(); |
| 34 | + /// let value = 5; |
| 35 | + /// if set.insert(&value) { |
| 36 | + /// println!("inserted {value:?}"); |
| 37 | + /// } |
| 38 | + /// ``` |
| 39 | + #[clippy::version = "1.80.0"] |
| 40 | + pub HASHSET_INSERT_AFTER_CONTAINS, |
| 41 | + nursery, |
| 42 | + "unnecessary call to `HashSet::contains` followed by `HashSet::insert`" |
| 43 | +} |
| 44 | + |
| 45 | +declare_lint_pass!(HashsetInsertAfterContains => [HASHSET_INSERT_AFTER_CONTAINS]); |
| 46 | + |
| 47 | +impl<'tcx> LateLintPass<'tcx> for HashsetInsertAfterContains { |
| 48 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 49 | + if !expr.span.from_expansion() |
| 50 | + && let Some(higher::If { |
| 51 | + cond: cond_expr, |
| 52 | + then: then_expr, |
| 53 | + .. |
| 54 | + }) = higher::If::hir(expr) |
| 55 | + && let Some(contains_expr) = try_parse_contains(cx, cond_expr) |
| 56 | + && find_insert_calls(cx, &contains_expr, then_expr) |
| 57 | + { |
| 58 | + span_lint_and_then( |
| 59 | + cx, |
| 60 | + HASHSET_INSERT_AFTER_CONTAINS, |
| 61 | + expr.span, |
| 62 | + "usage of `HashSet::insert` after `HashSet::contains`", |
| 63 | + |diag| { |
| 64 | + diag.note("`HashSet::insert` returns whether it was inserted") |
| 65 | + .span_help(contains_expr.span, "remove the `HashSet::contains` call"); |
| 66 | + }, |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +struct ContainsExpr<'tcx> { |
| 73 | + receiver: &'tcx Expr<'tcx>, |
| 74 | + value: &'tcx Expr<'tcx>, |
| 75 | + span: Span, |
| 76 | +} |
| 77 | +fn try_parse_contains<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Option<ContainsExpr<'tcx>> { |
| 78 | + let expr = peel_hir_expr_while(expr, |e| { |
| 79 | + if let ExprKind::Unary(UnOp::Not, e) = e.kind { |
| 80 | + Some(e) |
| 81 | + } else { |
| 82 | + None |
| 83 | + } |
| 84 | + }); |
| 85 | + if let ExprKind::MethodCall(path, receiver, [value], span) = expr.kind { |
| 86 | + let value = value.peel_borrows(); |
| 87 | + let receiver = receiver.peel_borrows(); |
| 88 | + let receiver_ty = cx.typeck_results().expr_ty(receiver).peel_refs(); |
| 89 | + if value.span.eq_ctxt(expr.span) |
| 90 | + && is_type_diagnostic_item(cx, receiver_ty, sym::HashSet) |
| 91 | + && path.ident.name == sym!(contains) |
| 92 | + { |
| 93 | + return Some(ContainsExpr { receiver, value, span }); |
| 94 | + } |
| 95 | + } |
| 96 | + None |
| 97 | +} |
| 98 | + |
| 99 | +struct InsertExpr<'tcx> { |
| 100 | + receiver: &'tcx Expr<'tcx>, |
| 101 | + value: &'tcx Expr<'tcx>, |
| 102 | +} |
| 103 | +fn try_parse_insert<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<InsertExpr<'tcx>> { |
| 104 | + if let ExprKind::MethodCall(path, receiver, [value], _) = expr.kind { |
| 105 | + let value = value.peel_borrows(); |
| 106 | + let value = peel_hir_expr_while(value, |e| { |
| 107 | + if let ExprKind::Unary(UnOp::Deref, e) = e.kind { |
| 108 | + Some(e) |
| 109 | + } else { |
| 110 | + None |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + let receiver_ty = cx.typeck_results().expr_ty(receiver).peel_refs(); |
| 115 | + if is_type_diagnostic_item(cx, receiver_ty, sym::HashSet) && path.ident.name == sym!(insert) { |
| 116 | + Some(InsertExpr { receiver, value }) |
| 117 | + } else { |
| 118 | + None |
| 119 | + } |
| 120 | + } else { |
| 121 | + None |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +fn find_insert_calls<'tcx>(cx: &LateContext<'tcx>, contains_expr: &ContainsExpr<'tcx>, expr: &'tcx Expr<'_>) -> bool { |
| 126 | + for_each_expr(expr, |e| { |
| 127 | + if let Some(insert_expr) = try_parse_insert(cx, e) |
| 128 | + && SpanlessEq::new(cx).eq_expr(contains_expr.receiver, insert_expr.receiver) |
| 129 | + && SpanlessEq::new(cx).eq_expr(contains_expr.value, insert_expr.value) |
| 130 | + { |
| 131 | + ControlFlow::Break(()) |
| 132 | + } else { |
| 133 | + ControlFlow::Continue(()) |
| 134 | + } |
| 135 | + }) |
| 136 | + .is_some() |
| 137 | +} |
0 commit comments