Skip to content

Commit

Permalink
fix: check if receiver's hir_id matches expr's hir_id
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxqiu committed Apr 1, 2024
1 parent b2ea5ee commit 9d1f824
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/methods/search_is_some.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ pub(super) fn check<'tcx>(

fn is_receiver_of_method_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
if let Some(parent_expr) = get_parent_expr(cx, expr)
&& let ExprKind::MethodCall(..) = parent_expr.kind
&& let ExprKind::MethodCall(_, receiver, ..) = parent_expr.kind
&& receiver.hir_id == expr.hir_id
{
return true;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/search_is_some_fixable_none.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ mod issue_11910 {
0
}

struct Foo;
impl Foo{
fn bar(&self, _ : bool) {

}
}

fn test_then() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
(!v.iter().any(|x| *x == 42)).then(computations);
Expand All @@ -227,5 +234,7 @@ mod issue_11910 {
fn test_then_some() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
(!v.iter().any(|x| *x == 42)).then_some(0);

Foo.bar(!v.iter().any(|x| *x == 42));
}
}
9 changes: 9 additions & 0 deletions tests/ui/search_is_some_fixable_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ mod issue_11910 {
0
}

struct Foo;
impl Foo{
fn bar(&self, _ : bool) {

}
}

fn test_then() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
v.iter().find(|x| **x == 42).is_none().then(computations);
Expand All @@ -233,5 +240,7 @@ mod issue_11910 {
fn test_then_some() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
v.iter().find(|x| **x == 42).is_none().then_some(0);

Foo.bar(v.iter().find(|x| **x == 42).is_none());
}
}
12 changes: 9 additions & 3 deletions tests/ui/search_is_some_fixable_none.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,22 @@ LL | let _ = v.iter().find(|fp| test_u32_2(*fp.field)).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `!v.iter().any(|fp| test_u32_2(*fp.field))`

error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable_none.rs:230:9
--> $DIR/search_is_some_fixable_none.rs:237:9
|
LL | v.iter().find(|x| **x == 42).is_none().then(computations);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `(!v.iter().any(|x| *x == 42))`

error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable_none.rs:235:9
--> $DIR/search_is_some_fixable_none.rs:242:9
|
LL | v.iter().find(|x| **x == 42).is_none().then_some(0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `(!v.iter().any(|x| *x == 42))`

error: aborting due to 45 previous errors
error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable_none.rs:244:17
|
LL | Foo.bar(v.iter().find(|x| **x == 42).is_none());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| *x == 42)`

error: aborting due to 46 previous errors

0 comments on commit 9d1f824

Please sign in to comment.