-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint hashset_insert_after_contains
#11296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,127 @@ | ||||||||||||
use std::ops::ControlFlow; | ||||||||||||
|
||||||||||||
use clippy_utils::diagnostics::span_lint_and_then; | ||||||||||||
use clippy_utils::ty::is_type_diagnostic_item; | ||||||||||||
use clippy_utils::visitors::for_each_expr; | ||||||||||||
use clippy_utils::{higher, peel_hir_expr_while, SpanlessEq}; | ||||||||||||
use rustc_hir::{Expr, ExprKind, UnOp}; | ||||||||||||
use rustc_lint::{LateContext, LateLintPass}; | ||||||||||||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||||||||||||
use rustc_span::{sym, Span}; | ||||||||||||
|
||||||||||||
declare_clippy_lint! { | ||||||||||||
/// ### What it does | ||||||||||||
/// Checks for usage of `contains` to see if a value is not | ||||||||||||
/// present on `HashSet` followed by a `insert`. | ||||||||||||
/// | ||||||||||||
/// ### Why is this bad? | ||||||||||||
/// Using just `insert` and checking the returned `bool` is more efficient. | ||||||||||||
/// | ||||||||||||
/// ### 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.73.0"] | ||||||||||||
pub HASHSET_INSERT_AFTER_CONTAINS, | ||||||||||||
perf, | ||||||||||||
"unnecessary call to `HashSet::contains` followed by `HashSet::insert`" | ||||||||||||
} | ||||||||||||
declare_lint_pass!(HashsetInsertAfterContains => [HASHSET_INSERT_AFTER_CONTAINS]); | ||||||||||||
|
||||||||||||
impl<'tcx> LateLintPass<'tcx> for HashsetInsertAfterContains { | ||||||||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||||||||||||
if !expr.span.from_expansion() | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably also needs an |
||||||||||||
&& let Some(higher::If { | ||||||||||||
cond: cond_expr, | ||||||||||||
then: then_expr, | ||||||||||||
.. | ||||||||||||
}) = higher::If::hir(expr) | ||||||||||||
&& let Some(contains_expr) = try_parse_contains(cx, cond_expr) | ||||||||||||
&& find_insert_calls(cx, &contains_expr, then_expr) { | ||||||||||||
span_lint_and_then( | ||||||||||||
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nitpicky, but:
Suggested change
|
||||||||||||
cx, | ||||||||||||
HASHSET_INSERT_AFTER_CONTAINS, | ||||||||||||
expr.span, | ||||||||||||
"usage of `HashSet::insert` after `HashSet::contains`", | ||||||||||||
|diag| { | ||||||||||||
diag.note("`HashSet::insert` returns whether it was inserted") | ||||||||||||
.span_help(contains_expr.span, "remove the `HashSet::contains` call"); | ||||||||||||
}, | ||||||||||||
); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
struct ContainsExpr<'tcx> { | ||||||||||||
receiver: &'tcx Expr<'tcx>, | ||||||||||||
value: &'tcx Expr<'tcx>, | ||||||||||||
span: Span, | ||||||||||||
} | ||||||||||||
fn try_parse_contains<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Option<ContainsExpr<'tcx>> { | ||||||||||||
let expr = peel_hir_expr_while(expr, |e| match e.kind { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be an |
||||||||||||
ExprKind::Unary(UnOp::Not, e) => Some(e), | ||||||||||||
_ => None, | ||||||||||||
}); | ||||||||||||
if let ExprKind::MethodCall(path, receiver, [value], span) = expr.kind { | ||||||||||||
let value = value.peel_borrows(); | ||||||||||||
let receiver = receiver.peel_borrows(); | ||||||||||||
let receiver_ty = cx.typeck_results().expr_ty(receiver).peel_refs(); | ||||||||||||
if value.span.ctxt() == expr.span.ctxt() | ||||||||||||
&& is_type_diagnostic_item(cx, receiver_ty, sym::HashSet) | ||||||||||||
&& path.ident.name == sym!(contains) | ||||||||||||
{ | ||||||||||||
return Some(ContainsExpr { receiver, value, span }); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
None | ||||||||||||
} | ||||||||||||
|
||||||||||||
struct InsertExpr<'tcx> { | ||||||||||||
receiver: &'tcx Expr<'tcx>, | ||||||||||||
value: &'tcx Expr<'tcx>, | ||||||||||||
} | ||||||||||||
fn try_parse_insert<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<InsertExpr<'tcx>> { | ||||||||||||
if let ExprKind::MethodCall(path, receiver, [value], _) = expr.kind { | ||||||||||||
let value = value.peel_borrows(); | ||||||||||||
let value = peel_hir_expr_while(value, |e| match e.kind { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||||||||||||
ExprKind::Unary(UnOp::Deref, e) => Some(e), | ||||||||||||
_ => None, | ||||||||||||
}); | ||||||||||||
let receiver_ty = cx.typeck_results().expr_ty(receiver).peel_refs(); | ||||||||||||
if is_type_diagnostic_item(cx, receiver_ty, sym::HashSet) && path.ident.name == sym!(insert) { | ||||||||||||
Some(InsertExpr { receiver, value }) | ||||||||||||
} else { | ||||||||||||
None | ||||||||||||
} | ||||||||||||
} else { | ||||||||||||
None | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
fn find_insert_calls<'tcx>(cx: &LateContext<'tcx>, contains_expr: &ContainsExpr<'tcx>, expr: &'tcx Expr<'_>) -> bool { | ||||||||||||
for_each_expr(expr, |e| { | ||||||||||||
if let Some(insert_expr) = try_parse_insert(cx, e) | ||||||||||||
&& SpanlessEq::new(cx).eq_expr(contains_expr.receiver, insert_expr.receiver) | ||||||||||||
Centri3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
&& SpanlessEq::new(cx).eq_expr(contains_expr.value, insert_expr.value){ | ||||||||||||
ControlFlow::Break(()) | ||||||||||||
} else{ | ||||||||||||
ControlFlow::Continue(()) | ||||||||||||
} | ||||||||||||
}) | ||||||||||||
.is_some() | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#![allow(unused)] | ||
#![allow(clippy::nonminimal_bool)] | ||
#![allow(clippy::needless_borrow)] | ||
#![warn(clippy::hashset_insert_after_contains)] | ||
|
||
use std::collections::HashSet; | ||
|
||
fn main() { | ||
should_warn_cases(); | ||
|
||
should_not_warn_cases(); | ||
} | ||
|
||
fn should_warn_cases() { | ||
let mut set = HashSet::new(); | ||
let value = 5; | ||
|
||
if !set.contains(&value) { | ||
set.insert(value); | ||
println!("Just a comment"); | ||
} | ||
|
||
if set.contains(&value) { | ||
set.insert(value); | ||
println!("Just a comment"); | ||
} | ||
|
||
if !set.contains(&value) { | ||
set.insert(value); | ||
} | ||
|
||
if !!set.contains(&value) { | ||
set.insert(value); | ||
println!("Just a comment"); | ||
} | ||
|
||
if (&set).contains(&value) { | ||
set.insert(value); | ||
} | ||
|
||
let borrow_value = &6; | ||
if !set.contains(borrow_value) { | ||
set.insert(*borrow_value); | ||
} | ||
|
||
let borrow_set = &mut set; | ||
if !borrow_set.contains(&value) { | ||
borrow_set.insert(value); | ||
} | ||
} | ||
|
||
fn should_not_warn_cases() { | ||
let mut set = HashSet::new(); | ||
let value = 5; | ||
let another_value = 6; | ||
|
||
if !set.contains(&value) { | ||
set.insert(another_value); | ||
} | ||
|
||
if !set.contains(&value) { | ||
println!("Just a comment"); | ||
} | ||
|
||
if simply_true() { | ||
set.insert(value); | ||
} | ||
|
||
if !set.contains(&value) { | ||
set.replace(value); //it is not insert | ||
println!("Just a comment"); | ||
} | ||
} | ||
|
||
fn simply_true() -> bool { | ||
true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
error: usage of `HashSet::insert` after `HashSet::contains` | ||
--> $DIR/hashset_insert_after_contains.rs:18:5 | ||
| | ||
LL | / if !set.contains(&value) { | ||
LL | | set.insert(value); | ||
LL | | println!("Just a comment"); | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: `HashSet::insert` returns whether it was inserted | ||
help: remove the `HashSet::contains` call | ||
--> $DIR/hashset_insert_after_contains.rs:18:13 | ||
| | ||
LL | if !set.contains(&value) { | ||
| ^^^^^^^^^^^^^^^^ | ||
= note: `-D clippy::hashset-insert-after-contains` implied by `-D warnings` | ||
|
||
error: usage of `HashSet::insert` after `HashSet::contains` | ||
--> $DIR/hashset_insert_after_contains.rs:23:5 | ||
| | ||
LL | / if set.contains(&value) { | ||
LL | | set.insert(value); | ||
LL | | println!("Just a comment"); | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: `HashSet::insert` returns whether it was inserted | ||
help: remove the `HashSet::contains` call | ||
--> $DIR/hashset_insert_after_contains.rs:23:12 | ||
| | ||
LL | if set.contains(&value) { | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: usage of `HashSet::insert` after `HashSet::contains` | ||
--> $DIR/hashset_insert_after_contains.rs:28:5 | ||
| | ||
LL | / if !set.contains(&value) { | ||
LL | | set.insert(value); | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: `HashSet::insert` returns whether it was inserted | ||
help: remove the `HashSet::contains` call | ||
--> $DIR/hashset_insert_after_contains.rs:28:13 | ||
| | ||
LL | if !set.contains(&value) { | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: usage of `HashSet::insert` after `HashSet::contains` | ||
--> $DIR/hashset_insert_after_contains.rs:32:5 | ||
| | ||
LL | / if !!set.contains(&value) { | ||
LL | | set.insert(value); | ||
LL | | println!("Just a comment"); | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: `HashSet::insert` returns whether it was inserted | ||
help: remove the `HashSet::contains` call | ||
--> $DIR/hashset_insert_after_contains.rs:32:14 | ||
| | ||
LL | if !!set.contains(&value) { | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: usage of `HashSet::insert` after `HashSet::contains` | ||
--> $DIR/hashset_insert_after_contains.rs:37:5 | ||
| | ||
LL | / if (&set).contains(&value) { | ||
LL | | set.insert(value); | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: `HashSet::insert` returns whether it was inserted | ||
help: remove the `HashSet::contains` call | ||
--> $DIR/hashset_insert_after_contains.rs:37:15 | ||
| | ||
LL | if (&set).contains(&value) { | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: usage of `HashSet::insert` after `HashSet::contains` | ||
--> $DIR/hashset_insert_after_contains.rs:42:5 | ||
| | ||
LL | / if !set.contains(borrow_value) { | ||
LL | | set.insert(*borrow_value); | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: `HashSet::insert` returns whether it was inserted | ||
help: remove the `HashSet::contains` call | ||
--> $DIR/hashset_insert_after_contains.rs:42:13 | ||
| | ||
LL | if !set.contains(borrow_value) { | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: usage of `HashSet::insert` after `HashSet::contains` | ||
--> $DIR/hashset_insert_after_contains.rs:47:5 | ||
| | ||
LL | / if !borrow_set.contains(&value) { | ||
LL | | borrow_set.insert(value); | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: `HashSet::insert` returns whether it was inserted | ||
help: remove the `HashSet::contains` call | ||
--> $DIR/hashset_insert_after_contains.rs:47:20 | ||
| | ||
LL | if !borrow_set.contains(&value) { | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 7 previous errors | ||
|
Uh oh!
There was an error while loading. Please reload this page.