File tree 4 files changed +27
-19
lines changed 4 files changed +27
-19
lines changed Original file line number Diff line number Diff line change @@ -92,17 +92,19 @@ fn check_closure(cx: &LateContext<'_>, expr: &Expr<'_>) {
92
92
let ex = & body. value ;
93
93
94
94
if ex. span . ctxt ( ) != expr. span . ctxt ( ) {
95
- if let Some ( VecArgs :: Vec ( & [ ] ) ) = higher:: vec_macro ( cx, ex) {
96
- // replace `|| vec![]` with `Vec::new`
97
- span_lint_and_sugg (
98
- cx,
99
- REDUNDANT_CLOSURE ,
100
- expr. span ,
101
- "redundant closure" ,
102
- "replace the closure with `Vec::new`" ,
103
- "std::vec::Vec::new" . into ( ) ,
104
- Applicability :: MachineApplicable ,
105
- ) ;
95
+ if decl. inputs . is_empty ( ) {
96
+ if let Some ( VecArgs :: Vec ( & [ ] ) ) = higher:: vec_macro ( cx, ex) {
97
+ // replace `|| vec![]` with `Vec::new`
98
+ span_lint_and_sugg (
99
+ cx,
100
+ REDUNDANT_CLOSURE ,
101
+ expr. span ,
102
+ "redundant closure" ,
103
+ "replace the closure with `Vec::new`" ,
104
+ "std::vec::Vec::new" . into ( ) ,
105
+ Applicability :: MachineApplicable ,
106
+ ) ;
107
+ }
106
108
}
107
109
// skip `foo(|| macro!())`
108
110
return ;
Original file line number Diff line number Diff line change @@ -48,6 +48,9 @@ fn main() {
48
48
// See #515
49
49
let a: Option<Box<dyn (::std::ops::Deref<Target = [i32]>)>> =
50
50
Some(vec![1i32, 2]).map(|v| -> Box<dyn (::std::ops::Deref<Target = [i32]>)> { Box::new(v) });
51
+
52
+ // issue #7224
53
+ let _: Option<Vec<u32>> = Some(0).map(|_| vec![]);
51
54
}
52
55
53
56
trait TestTrait {
Original file line number Diff line number Diff line change @@ -48,6 +48,9 @@ fn main() {
48
48
// See #515
49
49
let a: Option < Box < dyn ( :: std:: ops:: Deref < Target = [ i32 ] > ) > > =
50
50
Some ( vec ! [ 1i32 , 2 ] ) . map ( |v| -> Box < dyn ( :: std:: ops:: Deref < Target = [ i32 ] > ) > { Box :: new ( v) } ) ;
51
+
52
+ // issue #7224
53
+ let _: Option < Vec < u32 > > = Some ( 0 ) . map ( |_| vec ! [ ] ) ;
51
54
}
52
55
53
56
trait TestTrait {
Original file line number Diff line number Diff line change @@ -33,51 +33,51 @@ LL | let e = Some(1u8).map(|a| generic(a));
33
33
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `generic`
34
34
35
35
error: redundant closure
36
- --> $DIR/eta.rs:89 :51
36
+ --> $DIR/eta.rs:92 :51
37
37
|
38
38
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
39
39
| ^^^^^^^^^^^ help: replace the closure with the method itself: `TestStruct::foo`
40
40
|
41
41
= note: `-D clippy::redundant-closure-for-method-calls` implied by `-D warnings`
42
42
43
43
error: redundant closure
44
- --> $DIR/eta.rs:91 :51
44
+ --> $DIR/eta.rs:94 :51
45
45
|
46
46
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo());
47
47
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `TestTrait::trait_foo`
48
48
49
49
error: redundant closure
50
- --> $DIR/eta.rs:94 :42
50
+ --> $DIR/eta.rs:97 :42
51
51
|
52
52
LL | let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear());
53
53
| ^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::vec::Vec::clear`
54
54
55
55
error: redundant closure
56
- --> $DIR/eta.rs:99 :29
56
+ --> $DIR/eta.rs:102 :29
57
57
|
58
58
LL | let e = Some("str").map(|s| s.to_string());
59
59
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::string::ToString::to_string`
60
60
61
61
error: redundant closure
62
- --> $DIR/eta.rs:101 :27
62
+ --> $DIR/eta.rs:104 :27
63
63
|
64
64
LL | let e = Some('a').map(|s| s.to_uppercase());
65
65
| ^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `char::to_uppercase`
66
66
67
67
error: redundant closure
68
- --> $DIR/eta.rs:104 :65
68
+ --> $DIR/eta.rs:107 :65
69
69
|
70
70
LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
71
71
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `char::to_ascii_uppercase`
72
72
73
73
error: redundant closure
74
- --> $DIR/eta.rs:187 :27
74
+ --> $DIR/eta.rs:190 :27
75
75
|
76
76
LL | let a = Some(1u8).map(|a| foo_ptr(a));
77
77
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `foo_ptr`
78
78
79
79
error: redundant closure
80
- --> $DIR/eta.rs:192 :27
80
+ --> $DIR/eta.rs:195 :27
81
81
|
82
82
LL | let a = Some(1u8).map(|a| closure(a));
83
83
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `closure`
You can’t perform that action at this time.
0 commit comments