Fix redundant_closure_for_method_calls suggestion

Certain types must be enclosed in angle brackets and must have generic
arguments substituted to create a working suggestion. For example, if
`s` has type `&[u8]`, then `|s| s.len()` may be replaced with
`<[u8]>::len`. Previously, Clippy erroneously suggested `[T]::len`.
This commit is contained in:
Matthew Ingwersen 2022-10-28 15:48:04 -04:00
parent 33137dd612
commit 329dc4715b
4 changed files with 81 additions and 4 deletions

View file

@ -316,3 +316,25 @@ pub fn mutable_impl_fn_mut(mut f: impl FnMut(), mut f_used_once: impl FnMut()) -
move || takes_fn_mut(&mut f_used_once)
}
impl dyn TestTrait + '_ {
fn method_on_dyn(&self) -> bool {
false
}
}
// https://github.com/rust-lang/rust-clippy/issues/7746
fn angle_brackets_and_substs() {
let array_opt: Option<&[u8; 3]> = Some(&[4, 8, 7]);
array_opt.map(<[u8; 3]>::as_slice);
let slice_opt: Option<&[u8]> = Some(b"slice");
slice_opt.map(<[u8]>::len);
let ptr_opt: Option<*const usize> = Some(&487);
ptr_opt.map(<*const usize>::is_null);
let test_struct = TestStruct { some_ref: &487 };
let dyn_opt: Option<&dyn TestTrait> = Some(&test_struct);
dyn_opt.map(<dyn TestTrait>::method_on_dyn);
}

View file

@ -316,3 +316,25 @@ pub fn mutable_impl_fn_mut(mut f: impl FnMut(), mut f_used_once: impl FnMut()) -
move || takes_fn_mut(|| f_used_once())
}
impl dyn TestTrait + '_ {
fn method_on_dyn(&self) -> bool {
false
}
}
// https://github.com/rust-lang/rust-clippy/issues/7746
fn angle_brackets_and_substs() {
let array_opt: Option<&[u8; 3]> = Some(&[4, 8, 7]);
array_opt.map(|a| a.as_slice());
let slice_opt: Option<&[u8]> = Some(b"slice");
slice_opt.map(|s| s.len());
let ptr_opt: Option<*const usize> = Some(&487);
ptr_opt.map(|p| p.is_null());
let test_struct = TestStruct { some_ref: &487 };
let dyn_opt: Option<&dyn TestTrait> = Some(&test_struct);
dyn_opt.map(|d| d.method_on_dyn());
}

View file

@ -134,5 +134,29 @@ error: redundant closure
LL | move || takes_fn_mut(|| f_used_once())
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut f_used_once`
error: aborting due to 22 previous errors
error: redundant closure
--> $DIR/eta.rs:329:19
|
LL | array_opt.map(|a| a.as_slice());
| ^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8; 3]>::as_slice`
error: redundant closure
--> $DIR/eta.rs:332:19
|
LL | slice_opt.map(|s| s.len());
| ^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8]>::len`
error: redundant closure
--> $DIR/eta.rs:335:17
|
LL | ptr_opt.map(|p| p.is_null());
| ^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<*const usize>::is_null`
error: redundant closure
--> $DIR/eta.rs:339:17
|
LL | dyn_opt.map(|d| d.method_on_dyn());
| ^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<dyn TestTrait>::method_on_dyn`
error: aborting due to 26 previous errors