Skip to content

Commit d143b26

Browse files
committed
fix: check if receiver's hir_id matches expr's hir_id
1 parent a74b4b9 commit d143b26

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

clippy_lints/src/methods/search_is_some.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ pub(super) fn check<'tcx>(
159159

160160
fn is_receiver_of_method_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
161161
if let Some(parent_expr) = get_parent_expr(cx, expr)
162-
&& let ExprKind::MethodCall(..) = parent_expr.kind
162+
&& let ExprKind::MethodCall(_, receiver, ..) = parent_expr.kind
163+
&& receiver.hir_id == expr.hir_id
163164
{
164165
return true;
165166
}

tests/ui/search_is_some_fixable_none.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ mod issue_11910 {
219219
0
220220
}
221221

222+
struct Foo;
223+
impl Foo{
224+
fn bar(&self, _ : bool) {
225+
226+
}
227+
}
228+
222229
fn test_then() {
223230
let v = vec![3, 2, 1, 0, -1, -2, -3];
224231
(!v.iter().any(|x| *x == 42)).then(computations);
@@ -227,5 +234,7 @@ mod issue_11910 {
227234
fn test_then_some() {
228235
let v = vec![3, 2, 1, 0, -1, -2, -3];
229236
(!v.iter().any(|x| *x == 42)).then_some(0);
237+
238+
Foo.bar(!v.iter().any(|x| *x == 42));
230239
}
231240
}

tests/ui/search_is_some_fixable_none.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ mod issue_11910 {
225225
0
226226
}
227227

228+
struct Foo;
229+
impl Foo{
230+
fn bar(&self, _ : bool) {
231+
232+
}
233+
}
234+
228235
fn test_then() {
229236
let v = vec![3, 2, 1, 0, -1, -2, -3];
230237
v.iter().find(|x| **x == 42).is_none().then(computations);
@@ -233,5 +240,7 @@ mod issue_11910 {
233240
fn test_then_some() {
234241
let v = vec![3, 2, 1, 0, -1, -2, -3];
235242
v.iter().find(|x| **x == 42).is_none().then_some(0);
243+
244+
Foo.bar(v.iter().find(|x| **x == 42).is_none());
236245
}
237246
}

tests/ui/search_is_some_fixable_none.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,22 @@ LL | let _ = v.iter().find(|fp| test_u32_2(*fp.field)).is_none();
283283
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|fp| test_u32_2(*fp.field))`
284284

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

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

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

0 commit comments

Comments
 (0)