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