fix: check if receiver's hir_id matches expr's hir_id

This commit is contained in:
Yuxiang Qiu 2024-01-04 20:29:41 -05:00
parent b2ea5eef44
commit 9d1f824878
No known key found for this signature in database
4 changed files with 29 additions and 4 deletions

View File

@ -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;
}

View File

@ -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);
@ -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));
}
}

View File

@ -225,6 +225,13 @@ fn computations() -> u32 {
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);
@ -233,5 +240,7 @@ fn test_then() {
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());
}
}

View File

@ -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