Skip to content

Commit 3d469f4

Browse files
committed
Auto merge of #3933 - phansch:add_rustfix_eta, r=oli-obk
Add // run-rustfix for eta.rs test cc #3071, #3630
2 parents eb9f9b1 + 9e4e130 commit 3d469f4

File tree

3 files changed

+148
-11
lines changed

3 files changed

+148
-11
lines changed

tests/ui/eta.fixed

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// run-rustfix
2+
3+
#![allow(
4+
unused,
5+
clippy::no_effect,
6+
clippy::redundant_closure_call,
7+
clippy::many_single_char_names,
8+
clippy::needless_pass_by_value,
9+
clippy::option_map_unit_fn,
10+
clippy::trivially_copy_pass_by_ref
11+
)]
12+
#![warn(clippy::redundant_closure, clippy::needless_borrow)]
13+
14+
use std::path::PathBuf;
15+
16+
fn main() {
17+
let a = Some(1u8).map(foo);
18+
meta(foo);
19+
let c = Some(1u8).map({1+2; foo});
20+
let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
21+
all(&[1, 2, 3], &2, |x, y| below(x, y)); //is adjusted
22+
unsafe {
23+
Some(1u8).map(|a| unsafe_fn(a)); // unsafe fn
24+
}
25+
26+
// See #815
27+
let e = Some(1u8).map(|a| divergent(a));
28+
let e = Some(1u8).map(generic);
29+
let e = Some(1u8).map(generic);
30+
// See #515
31+
let a: Option<Box<::std::ops::Deref<Target = [i32]>>> =
32+
Some(vec![1i32, 2]).map(|v| -> Box<::std::ops::Deref<Target = [i32]>> { Box::new(v) });
33+
}
34+
35+
trait TestTrait {
36+
fn trait_foo(self) -> bool;
37+
fn trait_foo_ref(&self) -> bool;
38+
}
39+
40+
struct TestStruct<'a> {
41+
some_ref: &'a i32,
42+
}
43+
44+
impl<'a> TestStruct<'a> {
45+
fn foo(self) -> bool {
46+
false
47+
}
48+
unsafe fn foo_unsafe(self) -> bool {
49+
true
50+
}
51+
}
52+
53+
impl<'a> TestTrait for TestStruct<'a> {
54+
fn trait_foo(self) -> bool {
55+
false
56+
}
57+
fn trait_foo_ref(&self) -> bool {
58+
false
59+
}
60+
}
61+
62+
impl<'a> std::ops::Deref for TestStruct<'a> {
63+
type Target = char;
64+
fn deref(&self) -> &char {
65+
&'a'
66+
}
67+
}
68+
69+
fn test_redundant_closures_containing_method_calls() {
70+
let i = 10;
71+
let e = Some(TestStruct { some_ref: &i }).map(TestStruct::foo);
72+
let e = Some(TestStruct { some_ref: &i }).map(TestStruct::foo);
73+
let e = Some(TestStruct { some_ref: &i }).map(TestTrait::trait_foo);
74+
let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo_ref());
75+
let e = Some(TestStruct { some_ref: &i }).map(TestTrait::trait_foo);
76+
let e = Some(&mut vec![1, 2, 3]).map(std::vec::Vec::clear);
77+
let e = Some(&mut vec![1, 2, 3]).map(std::vec::Vec::clear);
78+
unsafe {
79+
let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo_unsafe());
80+
}
81+
let e = Some("str").map(std::string::ToString::to_string);
82+
let e = Some("str").map(str::to_string);
83+
let e = Some('a').map(char::to_uppercase);
84+
let e = Some('a').map(char::to_uppercase);
85+
let e: std::vec::Vec<usize> = vec!['a', 'b', 'c'].iter().map(|c| c.len_utf8()).collect();
86+
let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(char::to_ascii_uppercase).collect();
87+
let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(char::to_ascii_uppercase).collect();
88+
let p = Some(PathBuf::new());
89+
let e = p.as_ref().and_then(|s| s.to_str());
90+
let c = Some(TestStruct { some_ref: &i })
91+
.as_ref()
92+
.map(|c| c.to_ascii_uppercase());
93+
94+
fn test_different_borrow_levels<T>(t: &[&T])
95+
where
96+
T: TestTrait,
97+
{
98+
t.iter().filter(|x| x.trait_foo_ref());
99+
t.iter().map(|x| x.trait_foo_ref());
100+
}
101+
}
102+
103+
fn meta<F>(f: F)
104+
where
105+
F: Fn(u8),
106+
{
107+
f(1u8)
108+
}
109+
110+
fn foo(_: u8) {}
111+
112+
fn foo2(_: u8) -> u8 {
113+
1u8
114+
}
115+
116+
fn all<X, F>(x: &[X], y: &X, f: F) -> bool
117+
where
118+
F: Fn(&X, &X) -> bool,
119+
{
120+
x.iter().all(|e| f(e, y))
121+
}
122+
123+
fn below(x: &u8, y: &u8) -> bool {
124+
x < y
125+
}
126+
127+
unsafe fn unsafe_fn(_: u8) {}
128+
129+
fn divergent(_: u8) -> ! {
130+
unimplemented!()
131+
}
132+
133+
fn generic<T>(_: T) -> u8 {
134+
0
135+
}

tests/ui/eta.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
13
#![allow(
24
unused,
35
clippy::no_effect,

tests/ui/eta.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
11
error: redundant closure found
2-
--> $DIR/eta.rs:15:27
2+
--> $DIR/eta.rs:17:27
33
|
44
LL | let a = Some(1u8).map(|a| foo(a));
55
| ^^^^^^^^^^ help: remove closure as shown: `foo`
66
|
77
= note: `-D clippy::redundant-closure` implied by `-D warnings`
88

99
error: redundant closure found
10-
--> $DIR/eta.rs:16:10
10+
--> $DIR/eta.rs:18:10
1111
|
1212
LL | meta(|a| foo(a));
1313
| ^^^^^^^^^^ help: remove closure as shown: `foo`
1414

1515
error: redundant closure found
16-
--> $DIR/eta.rs:17:27
16+
--> $DIR/eta.rs:19:27
1717
|
1818
LL | let c = Some(1u8).map(|a| {1+2; foo}(a));
1919
| ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `{1+2; foo}`
2020

2121
error: this expression borrows a reference that is immediately dereferenced by the compiler
22-
--> $DIR/eta.rs:19:21
22+
--> $DIR/eta.rs:21:21
2323
|
2424
LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
2525
| ^^^ help: change this to: `&2`
2626
|
2727
= note: `-D clippy::needless-borrow` implied by `-D warnings`
2828

2929
error: redundant closure found
30-
--> $DIR/eta.rs:26:27
30+
--> $DIR/eta.rs:28:27
3131
|
3232
LL | let e = Some(1u8).map(|a| generic(a));
3333
| ^^^^^^^^^^^^^^ help: remove closure as shown: `generic`
3434

3535
error: redundant closure found
36-
--> $DIR/eta.rs:69:51
36+
--> $DIR/eta.rs:71:51
3737
|
3838
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
3939
| ^^^^^^^^^^^ help: remove closure as shown: `TestStruct::foo`
4040

4141
error: redundant closure found
42-
--> $DIR/eta.rs:71:51
42+
--> $DIR/eta.rs:73:51
4343
|
4444
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo());
4545
| ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `TestTrait::trait_foo`
4646

4747
error: redundant closure found
48-
--> $DIR/eta.rs:74:42
48+
--> $DIR/eta.rs:76:42
4949
|
5050
LL | let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear());
5151
| ^^^^^^^^^^^^^ help: remove closure as shown: `std::vec::Vec::clear`
5252

5353
error: redundant closure found
54-
--> $DIR/eta.rs:79:29
54+
--> $DIR/eta.rs:81:29
5555
|
5656
LL | let e = Some("str").map(|s| s.to_string());
5757
| ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `std::string::ToString::to_string`
5858

5959
error: redundant closure found
60-
--> $DIR/eta.rs:81:27
60+
--> $DIR/eta.rs:83:27
6161
|
6262
LL | let e = Some('a').map(|s| s.to_uppercase());
6363
| ^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_uppercase`
6464

6565
error: redundant closure found
66-
--> $DIR/eta.rs:84:65
66+
--> $DIR/eta.rs:86:65
6767
|
6868
LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_ascii_uppercase`

0 commit comments

Comments
 (0)