Skip to content

Commit 6104aef

Browse files
committed
Auto merge of #3938 - phansch:more_uicleanup, r=oli-obk
UI test cleanup: Extract or_fun_call and iter_nth tests cc #2038
2 parents bad3e08 + 25e2aff commit 6104aef

File tree

6 files changed

+257
-236
lines changed

6 files changed

+257
-236
lines changed

tests/ui/iter_nth.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// aux-build:option_helpers.rs
2+
3+
#![warn(clippy::iter_nth)]
4+
5+
#[macro_use]
6+
extern crate option_helpers;
7+
8+
use option_helpers::IteratorFalsePositives;
9+
use std::collections::VecDeque;
10+
11+
/// Struct to generate false positives for things with `.iter()`.
12+
#[derive(Copy, Clone)]
13+
struct HasIter;
14+
15+
impl HasIter {
16+
fn iter(self) -> IteratorFalsePositives {
17+
IteratorFalsePositives { foo: 0 }
18+
}
19+
20+
fn iter_mut(self) -> IteratorFalsePositives {
21+
IteratorFalsePositives { foo: 0 }
22+
}
23+
}
24+
25+
/// Checks implementation of `ITER_NTH` lint.
26+
fn iter_nth() {
27+
let mut some_vec = vec![0, 1, 2, 3];
28+
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
29+
let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
30+
31+
{
32+
// Make sure we lint `.iter()` for relevant types.
33+
let bad_vec = some_vec.iter().nth(3);
34+
let bad_slice = &some_vec[..].iter().nth(3);
35+
let bad_boxed_slice = boxed_slice.iter().nth(3);
36+
let bad_vec_deque = some_vec_deque.iter().nth(3);
37+
}
38+
39+
{
40+
// Make sure we lint `.iter_mut()` for relevant types.
41+
let bad_vec = some_vec.iter_mut().nth(3);
42+
}
43+
{
44+
let bad_slice = &some_vec[..].iter_mut().nth(3);
45+
}
46+
{
47+
let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
48+
}
49+
50+
// Make sure we don't lint for non-relevant types.
51+
let false_positive = HasIter;
52+
let ok = false_positive.iter().nth(3);
53+
let ok_mut = false_positive.iter_mut().nth(3);
54+
}
55+
56+
fn main() {}

tests/ui/iter_nth.stderr

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
2+
--> $DIR/iter_nth.rs:33:23
3+
|
4+
LL | let bad_vec = some_vec.iter().nth(3);
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::iter-nth` implied by `-D warnings`
8+
9+
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
10+
--> $DIR/iter_nth.rs:34:26
11+
|
12+
LL | let bad_slice = &some_vec[..].iter().nth(3);
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
16+
--> $DIR/iter_nth.rs:35:31
17+
|
18+
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
22+
--> $DIR/iter_nth.rs:36:29
23+
|
24+
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
28+
--> $DIR/iter_nth.rs:41:23
29+
|
30+
LL | let bad_vec = some_vec.iter_mut().nth(3);
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
34+
--> $DIR/iter_nth.rs:44:26
35+
|
36+
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
40+
--> $DIR/iter_nth.rs:47:29
41+
|
42+
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
error: aborting due to 7 previous errors
46+

tests/ui/methods.rs

-108
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,6 @@ fn option_methods() {
215215
);
216216
}
217217

218-
/// Struct to generate false positives for things with `.iter()`.
219-
#[derive(Copy, Clone)]
220-
struct HasIter;
221-
222-
impl HasIter {
223-
fn iter(self) -> IteratorFalsePositives {
224-
IteratorFalsePositives { foo: 0 }
225-
}
226-
227-
fn iter_mut(self) -> IteratorFalsePositives {
228-
IteratorFalsePositives { foo: 0 }
229-
}
230-
}
231-
232218
/// Checks implementation of `FILTER_NEXT` lint.
233219
#[rustfmt::skip]
234220
fn filter_next() {
@@ -287,100 +273,6 @@ fn search_is_some() {
287273
let _ = foo.rposition().is_some();
288274
}
289275

290-
/// Checks implementation of the `OR_FUN_CALL` lint.
291-
fn or_fun_call() {
292-
struct Foo;
293-
294-
impl Foo {
295-
fn new() -> Foo {
296-
Foo
297-
}
298-
}
299-
300-
enum Enum {
301-
A(i32),
302-
}
303-
304-
fn make<T>() -> T {
305-
unimplemented!();
306-
}
307-
308-
let with_enum = Some(Enum::A(1));
309-
with_enum.unwrap_or(Enum::A(5));
310-
311-
let with_const_fn = Some(::std::time::Duration::from_secs(1));
312-
with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
313-
314-
let with_constructor = Some(vec![1]);
315-
with_constructor.unwrap_or(make());
316-
317-
let with_new = Some(vec![1]);
318-
with_new.unwrap_or(Vec::new());
319-
320-
let with_const_args = Some(vec![1]);
321-
with_const_args.unwrap_or(Vec::with_capacity(12));
322-
323-
let with_err: Result<_, ()> = Ok(vec![1]);
324-
with_err.unwrap_or(make());
325-
326-
let with_err_args: Result<_, ()> = Ok(vec![1]);
327-
with_err_args.unwrap_or(Vec::with_capacity(12));
328-
329-
let with_default_trait = Some(1);
330-
with_default_trait.unwrap_or(Default::default());
331-
332-
let with_default_type = Some(1);
333-
with_default_type.unwrap_or(u64::default());
334-
335-
let with_vec = Some(vec![1]);
336-
with_vec.unwrap_or(vec![]);
337-
338-
// FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
339-
340-
let without_default = Some(Foo);
341-
without_default.unwrap_or(Foo::new());
342-
343-
let mut map = HashMap::<u64, String>::new();
344-
map.entry(42).or_insert(String::new());
345-
346-
let mut btree = BTreeMap::<u64, String>::new();
347-
btree.entry(42).or_insert(String::new());
348-
349-
let stringy = Some(String::from(""));
350-
let _ = stringy.unwrap_or("".to_owned());
351-
}
352-
353-
/// Checks implementation of `ITER_NTH` lint.
354-
fn iter_nth() {
355-
let mut some_vec = vec![0, 1, 2, 3];
356-
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
357-
let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
358-
359-
{
360-
// Make sure we lint `.iter()` for relevant types.
361-
let bad_vec = some_vec.iter().nth(3);
362-
let bad_slice = &some_vec[..].iter().nth(3);
363-
let bad_boxed_slice = boxed_slice.iter().nth(3);
364-
let bad_vec_deque = some_vec_deque.iter().nth(3);
365-
}
366-
367-
{
368-
// Make sure we lint `.iter_mut()` for relevant types.
369-
let bad_vec = some_vec.iter_mut().nth(3);
370-
}
371-
{
372-
let bad_slice = &some_vec[..].iter_mut().nth(3);
373-
}
374-
{
375-
let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
376-
}
377-
378-
// Make sure we don't lint for non-relevant types.
379-
let false_positive = HasIter;
380-
let ok = false_positive.iter().nth(3);
381-
let ok_mut = false_positive.iter_mut().nth(3);
382-
}
383-
384276
#[allow(clippy::similar_names)]
385277
fn main() {
386278
let opt = Some(0);

0 commit comments

Comments
 (0)