-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add BTreeSet
detection to the set_contains_or_insert
lint
#13053
Conversation
c3a561b
to
f82f3de
Compare
set_contains_or_insert
to contains_or_insert
BTreeSet
in set_contains_or_insert
and rename to contains_or_insert
Continuing from #12873 (comment) The name of this lint could either focus on let mut map = HashMap::new();
if map.contains_key(&"a") {
map.insert("a", 1);
} The issue here is that the lint would need to detect if the key has the same value in both calls, i.e. refer to the same const or variable, and that variable hasn't been modified between the calls... |
I would have expected the old name to be registered as renamed, but I haven't looked into our machinery for that for a while, so I may be wrong. Otherwise this looks good. |
We have the |
f3aa8df
to
f4a9bb3
Compare
BTreeSet
in set_contains_or_insert
and rename to contains_or_insert
BTreeSet
, rename set_contains_or_insert
to contains_or_insert
@llogiq I just did a bit of a cleanup, but I am not sure where |
Oh, I think you meant to use |
f4a9bb3
to
014800a
Compare
No I meant for comparing two exprs. |
@llogiq in that case I don't follow - could you comment in the review tab - which lines you think should be changed? thx! |
014800a
to
cb237de
Compare
@@ -113,7 +118,7 @@ fn find_insert_calls<'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)) | |||
if let Some((insert_expr, _)) = try_parse_op_call(cx, e, sym!(insert)) | |||
&& SpanlessEq::new(cx).eq_expr(contains_expr.receiver, insert_expr.receiver) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we should use eq_expr_value
to avoid linting on e.g. my_vec.pop().contains(key)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@llogiq i must still be missing something. I did not change any comparison longic, but instead simply ignore one extra param. If you think this lint should be modified to use eq_expr_value
somewhere where my PR is not touching, lets perhaps do it in a separate PR? Especially if you think there is a false positive in it that we should add to a test.
In the mean time, I reverted the lint rename - the cut off day has passed i think, so it would be better to keep the lint as is, and perhaps introduce another lint that focuses on map
style interfaces (e.g. if !hashmap.contains(key) { hashmap.add(key, value) }
)
cb237de
to
7019504
Compare
* Detect `BTreeSet::contains` + `BTreeSet::insert` usage in the same way as with the `HashSet`.
7019504
to
9964b4e
Compare
BTreeSet
, rename set_contains_or_insert
to contains_or_insert
BTreeSet
detection to the set_contains_or_insert
lint
Ok, let's try this. We can add more PRs with changes later. @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
BTreeSet::contains
+BTreeSet::insert
usage in the same way as with theHashSet
.CC: @lochetti @bitfield
changelog: [
set_contains_or_insert
]: HandleBTreeSet
in addition toHashSet