Skip to content

Commit 6d1ccbf

Browse files
committed
Correct suggestion when dereferencing enough, calling a function
1 parent ac45a83 commit 6d1ccbf

7 files changed

+47
-8
lines changed

clippy_lints/src/methods/search_is_some.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
248248
let expr = self.cx.tcx.hir().expect_expr(cmt.hir_id);
249249
let arg_ty_kind = self.cx.typeck_results().expr_ty(expr).kind();
250250

251-
// Note: this should always be true, as `find` only gives us a reference which are not mutable
252251
if matches!(arg_ty_kind, ty::Ref(_, _, Mutability::Not)) {
253252
let start_span = Span::new(self.next_pos, span.lo(), span.ctxt());
254253
let start_snip =
@@ -261,10 +260,10 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
261260
};
262261
self.suggestion_start.push_str(&ident_sugg);
263262
self.next_pos = span.hi();
263+
return;
264264
} else {
265265
self.applicability = Applicability::Unspecified;
266266
}
267-
return;
268267
}
269268
}
270269

tests/ui/search_is_some_fixable_none.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,17 @@ mod issue7392 {
102102
*x == 9
103103
}
104104

105+
fn simple_fn(x: u32) -> bool {
106+
x == 78
107+
}
108+
105109
fn more_projections() {
106110
let x = 19;
107111
let ppx: &u32 = &x;
108112
let _ = ![ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
109113
let _ = ![String::from("Hey hey")].iter().any(|s| s.len() == 2);
114+
115+
let v = vec![3, 2, 1, 0];
116+
let _ = !v.iter().any(|x| simple_fn(*x));
110117
}
111118
}

tests/ui/search_is_some_fixable_none.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,17 @@ mod issue7392 {
106106
*x == 9
107107
}
108108

109+
fn simple_fn(x: u32) -> bool {
110+
x == 78
111+
}
112+
109113
fn more_projections() {
110114
let x = 19;
111115
let ppx: &u32 = &x;
112116
let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_none();
113117
let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_none();
118+
119+
let v = vec![3, 2, 1, 0];
120+
let _ = v.iter().find(|x| simple_fn(**x)).is_none();
114121
}
115122
}

tests/ui/search_is_some_fixable_none.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,22 @@ LL | let _ = vfoo.iter().find(|sub| sub[1..4].len() == 3).is_none();
170170
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!vfoo.iter().any(|sub| sub[1..4].len() == 3)`
171171

172172
error: called `is_none()` after searching an `Iterator` with `find`
173-
--> $DIR/search_is_some_fixable_none.rs:112:17
173+
--> $DIR/search_is_some_fixable_none.rs:116:17
174174
|
175175
LL | let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_none();
176176
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `![ppx].iter().any(|ppp_x: &&u32| please(ppp_x))`
177177

178178
error: called `is_none()` after searching an `Iterator` with `find`
179-
--> $DIR/search_is_some_fixable_none.rs:113:17
179+
--> $DIR/search_is_some_fixable_none.rs:117:17
180180
|
181181
LL | let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_none();
182182
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `![String::from("Hey hey")].iter().any(|s| s.len() == 2)`
183183

184-
error: aborting due to 28 previous errors
184+
error: called `is_none()` after searching an `Iterator` with `find`
185+
--> $DIR/search_is_some_fixable_none.rs:120:17
186+
|
187+
LL | let _ = v.iter().find(|x| simple_fn(**x)).is_none();
188+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| simple_fn(*x))`
189+
190+
error: aborting due to 29 previous errors
185191

tests/ui/search_is_some_fixable_some.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,17 @@ mod issue7392 {
103103
*x == 9
104104
}
105105

106+
fn simple_fn(x: u32) -> bool {
107+
x == 78
108+
}
109+
106110
fn more_projections() {
107111
let x = 19;
108112
let ppx: &u32 = &x;
109113
let _ = [ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
110114
let _ = [String::from("Hey hey")].iter().any(|s| s.len() == 2);
115+
116+
let v = vec![3, 2, 1, 0];
117+
let _ = v.iter().any(|x| simple_fn(*x));
111118
}
112119
}

tests/ui/search_is_some_fixable_some.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,17 @@ mod issue7392 {
105105
*x == 9
106106
}
107107

108+
fn simple_fn(x: u32) -> bool {
109+
x == 78
110+
}
111+
108112
fn more_projections() {
109113
let x = 19;
110114
let ppx: &u32 = &x;
111115
let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_some();
112116
let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_some();
117+
118+
let v = vec![3, 2, 1, 0];
119+
let _ = v.iter().find(|x| simple_fn(**x)).is_some();
113120
}
114121
}

tests/ui/search_is_some_fixable_some.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,22 @@ LL | let _ = vfoo.iter().find(|sub| sub[1..4].len() == 3).is_some();
161161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|sub| sub[1..4].len() == 3)`
162162

163163
error: called `is_some()` after searching an `Iterator` with `find`
164-
--> $DIR/search_is_some_fixable_some.rs:111:30
164+
--> $DIR/search_is_some_fixable_some.rs:115:30
165165
|
166166
LL | let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_some();
167167
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|ppp_x: &&u32| please(ppp_x))`
168168

169169
error: called `is_some()` after searching an `Iterator` with `find`
170-
--> $DIR/search_is_some_fixable_some.rs:112:50
170+
--> $DIR/search_is_some_fixable_some.rs:116:50
171171
|
172172
LL | let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_some();
173173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|s| s.len() == 2)`
174174

175-
error: aborting due to 28 previous errors
175+
error: called `is_some()` after searching an `Iterator` with `find`
176+
--> $DIR/search_is_some_fixable_some.rs:119:26
177+
|
178+
LL | let _ = v.iter().find(|x| simple_fn(**x)).is_some();
179+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| simple_fn(*x))`
180+
181+
error: aborting due to 29 previous errors
176182

0 commit comments

Comments
 (0)