Skip to content

Commit 3bcf67c

Browse files
committed
Auto merge of #3610 - phansch:method_rs_cleanup, r=flip1995
UI test cleanup: Extract lint from methods.rs test Extracts the `result_map_unwrap_or_else` lint into a separate test file. This also extracts the `IteratorFalsePositives` struct and impl into `auxiliary/option_helpers.rs`. cc #2038
2 parents 488b8c5 + a5d3f37 commit 3bcf67c

7 files changed

+154
-169
lines changed

tests/ui/auxiliary/option_helpers.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![allow(dead_code, unused_variables)]
2+
3+
/// Utility macro to test linting behavior in `option_methods()`
4+
/// The lints included in `option_methods()` should not lint if the call to map is partially
5+
/// within a macro
6+
#[macro_export]
7+
macro_rules! opt_map {
8+
($opt:expr, $map:expr) => {
9+
($opt).map($map)
10+
};
11+
}
12+
13+
/// Struct to generate false positive for Iterator-based lints
14+
#[derive(Copy, Clone)]
15+
pub struct IteratorFalsePositives {
16+
pub foo: u32,
17+
}
18+
19+
impl IteratorFalsePositives {
20+
pub fn filter(self) -> IteratorFalsePositives {
21+
self
22+
}
23+
24+
pub fn next(self) -> IteratorFalsePositives {
25+
self
26+
}
27+
28+
pub fn find(self) -> Option<u32> {
29+
Some(self.foo)
30+
}
31+
32+
pub fn position(self) -> Option<u32> {
33+
Some(self.foo)
34+
}
35+
36+
pub fn rposition(self) -> Option<u32> {
37+
Some(self.foo)
38+
}
39+
40+
pub fn nth(self, n: usize) -> Option<u32> {
41+
Some(self.foo)
42+
}
43+
44+
pub fn skip(self, _: usize) -> IteratorFalsePositives {
45+
self
46+
}
47+
}

tests/ui/iter_skip_next.rs

+4-34
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,14 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10+
// aux-build:option_helpers.rs
11+
1012
#![warn(clippy::iter_skip_next)]
1113
#![allow(clippy::blacklisted_name)]
1214

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-
}
15+
extern crate option_helpers;
3116

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-
}
17+
use option_helpers::IteratorFalsePositives;
4818

4919
/// Checks implementation of `ITER_SKIP_NEXT` lint
5020
fn iter_skip_next() {

tests/ui/iter_skip_next.stderr

+4-4
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:22: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:23: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:24: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:25:14
2323
|
2424
LL | let _ = &some_vec[..].iter().skip(3).next();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/methods.rs

+7-66
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10+
// aux-build:option_helpers.rs
11+
1012
#![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
1113
#![allow(
1214
clippy::blacklisted_name,
@@ -22,6 +24,9 @@
2224
clippy::useless_format
2325
)]
2426

27+
#[macro_use]
28+
extern crate option_helpers;
29+
2530
use std::collections::BTreeMap;
2631
use std::collections::HashMap;
2732
use std::collections::HashSet;
@@ -31,6 +36,8 @@ use std::iter::FromIterator;
3136
use std::rc::{self, Rc};
3237
use std::sync::{self, Arc};
3338

39+
use option_helpers::IteratorFalsePositives;
40+
3441
pub struct T;
3542

3643
impl T {
@@ -101,13 +108,6 @@ impl Mul<T> for T {
101108
fn mul(self, other: T) -> T { self } // no error, obviously
102109
}
103110

104-
/// Utility macro to test linting behavior in `option_methods()`
105-
/// The lints included in `option_methods()` should not lint if the call to map is partially
106-
/// within a macro
107-
macro_rules! opt_map {
108-
($opt:expr, $map:expr) => {($opt).map($map)};
109-
}
110-
111111
/// Checks implementation of the following lints:
112112
/// * `OPTION_MAP_UNWRAP_OR`
113113
/// * `OPTION_MAP_UNWRAP_OR_ELSE`
@@ -169,29 +169,6 @@ fn option_methods() {
169169
);
170170
}
171171

172-
/// Checks implementation of the following lints:
173-
/// * `RESULT_MAP_UNWRAP_OR_ELSE`
174-
fn result_methods() {
175-
let res: Result<i32, ()> = Ok(1);
176-
177-
// Check RESULT_MAP_UNWRAP_OR_ELSE
178-
// single line case
179-
let _ = res.map(|x| x + 1)
180-
181-
.unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
182-
// multi line cases
183-
let _ = res.map(|x| {
184-
x + 1
185-
}
186-
).unwrap_or_else(|e| 0);
187-
let _ = res.map(|x| x + 1)
188-
.unwrap_or_else(|e|
189-
0
190-
);
191-
// macro case
192-
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint
193-
}
194-
195172
/// Struct to generate false positives for things with .iter()
196173
#[derive(Copy, Clone)]
197174
struct HasIter;
@@ -206,42 +183,6 @@ impl HasIter {
206183
}
207184
}
208185

209-
/// Struct to generate false positive for Iterator-based lints
210-
#[derive(Copy, Clone)]
211-
struct IteratorFalsePositives {
212-
foo: u32,
213-
}
214-
215-
impl IteratorFalsePositives {
216-
fn filter(self) -> IteratorFalsePositives {
217-
self
218-
}
219-
220-
fn next(self) -> IteratorFalsePositives {
221-
self
222-
}
223-
224-
fn find(self) -> Option<u32> {
225-
Some(self.foo)
226-
}
227-
228-
fn position(self) -> Option<u32> {
229-
Some(self.foo)
230-
}
231-
232-
fn rposition(self) -> Option<u32> {
233-
Some(self.foo)
234-
}
235-
236-
fn nth(self, n: usize) -> Option<u32> {
237-
Some(self.foo)
238-
}
239-
240-
fn skip(self, _: usize) -> IteratorFalsePositives {
241-
self
242-
}
243-
}
244-
245186
/// Checks implementation of `FILTER_NEXT` lint
246187
fn filter_next() {
247188
let v = vec![3, 2, 1, 0, -1, -2, -3];

0 commit comments

Comments
 (0)