File tree Expand file tree Collapse file tree 4 files changed +27
-19
lines changed
Expand file tree Collapse file tree 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<'_>) {
9292 let ex = & body. value ;
9393
9494 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+ }
106108 }
107109 // skip `foo(|| macro!())`
108110 return ;
Original file line number Diff line number Diff line change @@ -48,6 +48,9 @@ fn main() {
4848 // See #515
4949 let a: Option<Box<dyn (::std::ops::Deref<Target = [i32]>)>> =
5050 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![]);
5154}
5255
5356trait TestTrait {
Original file line number Diff line number Diff line change @@ -48,6 +48,9 @@ fn main() {
4848 // See #515
4949 let a: Option < Box < dyn ( :: std:: ops:: Deref < Target = [ i32 ] > ) > > =
5050 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 ! [ ] ) ;
5154}
5255
5356trait TestTrait {
Original file line number Diff line number Diff line change @@ -33,51 +33,51 @@ LL | let e = Some(1u8).map(|a| generic(a));
3333 | ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `generic`
3434
3535error: redundant closure
36- --> $DIR/eta.rs:89 :51
36+ --> $DIR/eta.rs:92 :51
3737 |
3838LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
3939 | ^^^^^^^^^^^ help: replace the closure with the method itself: `TestStruct::foo`
4040 |
4141 = note: `-D clippy::redundant-closure-for-method-calls` implied by `-D warnings`
4242
4343error: redundant closure
44- --> $DIR/eta.rs:91 :51
44+ --> $DIR/eta.rs:94 :51
4545 |
4646LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo());
4747 | ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `TestTrait::trait_foo`
4848
4949error: redundant closure
50- --> $DIR/eta.rs:94 :42
50+ --> $DIR/eta.rs:97 :42
5151 |
5252LL | let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear());
5353 | ^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::vec::Vec::clear`
5454
5555error: redundant closure
56- --> $DIR/eta.rs:99 :29
56+ --> $DIR/eta.rs:102 :29
5757 |
5858LL | let e = Some("str").map(|s| s.to_string());
5959 | ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::string::ToString::to_string`
6060
6161error: redundant closure
62- --> $DIR/eta.rs:101 :27
62+ --> $DIR/eta.rs:104 :27
6363 |
6464LL | let e = Some('a').map(|s| s.to_uppercase());
6565 | ^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `char::to_uppercase`
6666
6767error: redundant closure
68- --> $DIR/eta.rs:104 :65
68+ --> $DIR/eta.rs:107 :65
6969 |
7070LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
7171 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `char::to_ascii_uppercase`
7272
7373error: redundant closure
74- --> $DIR/eta.rs:187 :27
74+ --> $DIR/eta.rs:190 :27
7575 |
7676LL | let a = Some(1u8).map(|a| foo_ptr(a));
7777 | ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `foo_ptr`
7878
7979error: redundant closure
80- --> $DIR/eta.rs:192 :27
80+ --> $DIR/eta.rs:195 :27
8181 |
8282LL | let a = Some(1u8).map(|a| closure(a));
8383 | ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `closure`
You can’t perform that action at this time.
0 commit comments