Skip to content

Commit 0c54913

Browse files
committed
Extract IteratorFalsePositives into option_helpers.rs
This was previously duplicated in #3605
1 parent eaaee23 commit 0c54913

File tree

5 files changed

+69
-103
lines changed

5 files changed

+69
-103
lines changed

tests/auxiliary/option_helpers.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,39 @@
44
macro_rules! opt_map {
55
($opt:expr, $map:expr) => {($opt).map($map)};
66
}
7+
8+
/// Struct to generate false positive for Iterator-based lints
9+
#[derive(Copy, Clone)]
10+
struct IteratorFalsePositives {
11+
foo: u32,
12+
}
13+
14+
impl IteratorFalsePositives {
15+
fn filter(self) -> IteratorFalsePositives {
16+
self
17+
}
18+
19+
fn next(self) -> IteratorFalsePositives {
20+
self
21+
}
22+
23+
fn find(self) -> Option<u32> {
24+
Some(self.foo)
25+
}
26+
27+
fn position(self) -> Option<u32> {
28+
Some(self.foo)
29+
}
30+
31+
fn rposition(self) -> Option<u32> {
32+
Some(self.foo)
33+
}
34+
35+
fn nth(self, n: usize) -> Option<u32> {
36+
Some(self.foo)
37+
}
38+
39+
fn skip(self, _: usize) -> IteratorFalsePositives {
40+
self
41+
}
42+
}

tests/ui/iter_skip_next.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,7 @@
1010
#![warn(clippy::iter_skip_next)]
1111
#![allow(clippy::blacklisted_name)]
1212

13-
/// Struct to generate false positive for Iterator-based lints
14-
#[derive(Copy, Clone)]
15-
struct IteratorFalsePositives {
16-
foo: u32,
17-
}
18-
19-
impl IteratorFalsePositives {
20-
fn filter(self) -> IteratorFalsePositives {
21-
self
22-
}
23-
24-
fn next(self) -> IteratorFalsePositives {
25-
self
26-
}
27-
28-
fn find(self) -> Option<u32> {
29-
Some(self.foo)
30-
}
31-
32-
fn position(self) -> Option<u32> {
33-
Some(self.foo)
34-
}
35-
36-
fn rposition(self) -> Option<u32> {
37-
Some(self.foo)
38-
}
39-
40-
fn nth(self, n: usize) -> Option<u32> {
41-
Some(self.foo)
42-
}
43-
44-
fn skip(self, _: usize) -> IteratorFalsePositives {
45-
self
46-
}
47-
}
13+
include!("../auxiliary/option_helpers.rs");
4814

4915
/// Checks implementation of `ITER_SKIP_NEXT` lint
5016
fn iter_skip_next() {

tests/ui/iter_skip_next.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
2-
--> $DIR/iter_skip_next.rs:52:13
2+
--> $DIR/iter_skip_next.rs:18:13
33
|
44
LL | let _ = some_vec.iter().skip(42).next();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::iter-skip-next` implied by `-D warnings`
88

99
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
10-
--> $DIR/iter_skip_next.rs:53:13
10+
--> $DIR/iter_skip_next.rs:19:13
1111
|
1212
LL | let _ = some_vec.iter().cycle().skip(42).next();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
16-
--> $DIR/iter_skip_next.rs:54:13
16+
--> $DIR/iter_skip_next.rs:20:13
1717
|
1818
LL | let _ = (1..10).skip(10).next();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^
2020

2121
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
22-
--> $DIR/iter_skip_next.rs:55:14
22+
--> $DIR/iter_skip_next.rs:21:14
2323
|
2424
LL | let _ = &some_vec[..].iter().skip(3).next();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/methods.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -178,42 +178,6 @@ impl HasIter {
178178
}
179179
}
180180

181-
/// Struct to generate false positive for Iterator-based lints
182-
#[derive(Copy, Clone)]
183-
struct IteratorFalsePositives {
184-
foo: u32,
185-
}
186-
187-
impl IteratorFalsePositives {
188-
fn filter(self) -> IteratorFalsePositives {
189-
self
190-
}
191-
192-
fn next(self) -> IteratorFalsePositives {
193-
self
194-
}
195-
196-
fn find(self) -> Option<u32> {
197-
Some(self.foo)
198-
}
199-
200-
fn position(self) -> Option<u32> {
201-
Some(self.foo)
202-
}
203-
204-
fn rposition(self) -> Option<u32> {
205-
Some(self.foo)
206-
}
207-
208-
fn nth(self, n: usize) -> Option<u32> {
209-
Some(self.foo)
210-
}
211-
212-
fn skip(self, _: usize) -> IteratorFalsePositives {
213-
self
214-
}
215-
}
216-
217181
/// Checks implementation of `FILTER_NEXT` lint
218182
fn filter_next() {
219183
let v = vec![3, 2, 1, 0, -1, -2, -3];

tests/ui/methods.stderr

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ LL | });
144144
|
145145

146146
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
147-
--> $DIR/methods.rs:222:13
147+
--> $DIR/methods.rs:186:13
148148
|
149149
LL | let _ = v.iter().filter(|&x| *x < 0).next();
150150
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -153,7 +153,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
153153
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`
154154

155155
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
156-
--> $DIR/methods.rs:225:13
156+
--> $DIR/methods.rs:189:13
157157
|
158158
LL | let _ = v.iter().filter(|&x| {
159159
| _____________^
@@ -163,7 +163,7 @@ LL | | ).next();
163163
| |___________________________^
164164

165165
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
166-
--> $DIR/methods.rs:240:13
166+
--> $DIR/methods.rs:204:13
167167
|
168168
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
169169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -172,7 +172,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
172172
= note: replace `find(|&x| *x < 0).is_some()` with `any(|&x| *x < 0)`
173173

174174
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
175-
--> $DIR/methods.rs:243:13
175+
--> $DIR/methods.rs:207:13
176176
|
177177
LL | let _ = v.iter().find(|&x| {
178178
| _____________^
@@ -182,15 +182,15 @@ LL | | ).is_some();
182182
| |______________________________^
183183

184184
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
185-
--> $DIR/methods.rs:249:13
185+
--> $DIR/methods.rs:213:13
186186
|
187187
LL | let _ = v.iter().position(|&x| x < 0).is_some();
188188
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
189189
|
190190
= note: replace `position(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
191191

192192
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
193-
--> $DIR/methods.rs:252:13
193+
--> $DIR/methods.rs:216:13
194194
|
195195
LL | let _ = v.iter().position(|&x| {
196196
| _____________^
@@ -200,15 +200,15 @@ LL | | ).is_some();
200200
| |______________________________^
201201

202202
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
203-
--> $DIR/methods.rs:258:13
203+
--> $DIR/methods.rs:222:13
204204
|
205205
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
206206
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
207207
|
208208
= note: replace `rposition(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
209209

210210
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
211-
--> $DIR/methods.rs:261:13
211+
--> $DIR/methods.rs:225:13
212212
|
213213
LL | let _ = v.iter().rposition(|&x| {
214214
| _____________^
@@ -218,125 +218,125 @@ LL | | ).is_some();
218218
| |______________________________^
219219

220220
error: use of `unwrap_or` followed by a function call
221-
--> $DIR/methods.rs:296:22
221+
--> $DIR/methods.rs:260:22
222222
|
223223
LL | with_constructor.unwrap_or(make());
224224
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)`
225225
|
226226
= note: `-D clippy::or-fun-call` implied by `-D warnings`
227227

228228
error: use of `unwrap_or` followed by a call to `new`
229-
--> $DIR/methods.rs:299:5
229+
--> $DIR/methods.rs:263:5
230230
|
231231
LL | with_new.unwrap_or(Vec::new());
232232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`
233233

234234
error: use of `unwrap_or` followed by a function call
235-
--> $DIR/methods.rs:302:21
235+
--> $DIR/methods.rs:266:21
236236
|
237237
LL | with_const_args.unwrap_or(Vec::with_capacity(12));
238238
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`
239239

240240
error: use of `unwrap_or` followed by a function call
241-
--> $DIR/methods.rs:305:14
241+
--> $DIR/methods.rs:269:14
242242
|
243243
LL | with_err.unwrap_or(make());
244244
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`
245245

246246
error: use of `unwrap_or` followed by a function call
247-
--> $DIR/methods.rs:308:19
247+
--> $DIR/methods.rs:272:19
248248
|
249249
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
250250
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`
251251

252252
error: use of `unwrap_or` followed by a call to `default`
253-
--> $DIR/methods.rs:311:5
253+
--> $DIR/methods.rs:275:5
254254
|
255255
LL | with_default_trait.unwrap_or(Default::default());
256256
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`
257257

258258
error: use of `unwrap_or` followed by a call to `default`
259-
--> $DIR/methods.rs:314:5
259+
--> $DIR/methods.rs:278:5
260260
|
261261
LL | with_default_type.unwrap_or(u64::default());
262262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
263263

264264
error: use of `unwrap_or` followed by a function call
265-
--> $DIR/methods.rs:317:14
265+
--> $DIR/methods.rs:281:14
266266
|
267267
LL | with_vec.unwrap_or(vec![]);
268268
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`
269269

270270
error: use of `unwrap_or` followed by a function call
271-
--> $DIR/methods.rs:322:21
271+
--> $DIR/methods.rs:286:21
272272
|
273273
LL | without_default.unwrap_or(Foo::new());
274274
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
275275

276276
error: use of `or_insert` followed by a function call
277-
--> $DIR/methods.rs:325:19
277+
--> $DIR/methods.rs:289:19
278278
|
279279
LL | map.entry(42).or_insert(String::new());
280280
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
281281

282282
error: use of `or_insert` followed by a function call
283-
--> $DIR/methods.rs:328:21
283+
--> $DIR/methods.rs:292:21
284284
|
285285
LL | btree.entry(42).or_insert(String::new());
286286
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
287287

288288
error: use of `unwrap_or` followed by a function call
289-
--> $DIR/methods.rs:331:21
289+
--> $DIR/methods.rs:295:21
290290
|
291291
LL | let _ = stringy.unwrap_or("".to_owned());
292292
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
293293

294294
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
295-
--> $DIR/methods.rs:342:23
295+
--> $DIR/methods.rs:306:23
296296
|
297297
LL | let bad_vec = some_vec.iter().nth(3);
298298
| ^^^^^^^^^^^^^^^^^^^^^^
299299
|
300300
= note: `-D clippy::iter-nth` implied by `-D warnings`
301301

302302
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
303-
--> $DIR/methods.rs:343:26
303+
--> $DIR/methods.rs:307:26
304304
|
305305
LL | let bad_slice = &some_vec[..].iter().nth(3);
306306
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
307307

308308
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
309-
--> $DIR/methods.rs:344:31
309+
--> $DIR/methods.rs:308:31
310310
|
311311
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
312312
| ^^^^^^^^^^^^^^^^^^^^^^^^^
313313

314314
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
315-
--> $DIR/methods.rs:345:29
315+
--> $DIR/methods.rs:309:29
316316
|
317317
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
318318
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
319319

320320
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
321-
--> $DIR/methods.rs:350:23
321+
--> $DIR/methods.rs:314:23
322322
|
323323
LL | let bad_vec = some_vec.iter_mut().nth(3);
324324
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
325325

326326
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
327-
--> $DIR/methods.rs:353:26
327+
--> $DIR/methods.rs:317:26
328328
|
329329
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
330330
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
331331

332332
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
333-
--> $DIR/methods.rs:356:29
333+
--> $DIR/methods.rs:320:29
334334
|
335335
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
336336
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
337337

338338
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
339-
--> $DIR/methods.rs:368:13
339+
--> $DIR/methods.rs:332:13
340340
|
341341
LL | let _ = opt.unwrap();
342342
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)