What it does
This lint would detect redundant Vec::as_slice() and similar calls when the resulting slice is only used to call a method that would also resolve directly on the Vec through deref coercion.
Advantage
- Cleaner, shorter, easier to read code
Drawbacks
No response
Example
All usages of .as_slice() and .as_mut_slice() can be removed. Note that as_mut_slice() is a extended case because it does not actually need mut either.
#![warn(clippy::pedantic)]
trait SliceExt {
fn my_len(&self) -> usize;
}
impl SliceExt for [u32] {
fn my_len(&self) -> usize {
self.len()
}
}
fn as_ref(vals: &mut Vec<u32>) {
assert_eq!(vals.as_slice().len(), vals.len()); //~ as_slice() not needed
assert_eq!(vals.as_mut_slice().len(), vals.len()); //~ as_mut_slice() not needed
}
fn as_owned_ex(mut vals: Vec<u32>) {
assert_eq!(vals.as_slice().my_len(), vals.my_len()); //~ as_slice() not needed
assert_eq!(vals.as_mut_slice().my_len(), vals.my_len()); //~ as_mut_slice() not needed
}
fn as_ref_ex(vals: &mut Vec<u32>) {
assert_eq!(vals.as_slice().my_len(), vals.my_len()); //~ as_slice() not needed
assert_eq!(vals.as_mut_slice().my_len(), vals.my_len()); //~ as_mut_slice() not needed
}
fn main() {
as_ref(&mut vec![1_u32, 2, 3]);
as_owned_ex(vec![1_u32, 2, 3]);
as_ref_ex(&mut vec![1_u32, 2, 3]);
}
Comparison with existing lints
No response
Additional Context
No response
What it does
This lint would detect redundant
Vec::as_slice()and similar calls when the resulting slice is only used to call a method that would also resolve directly on the Vec through deref coercion.Advantage
Drawbacks
No response
Example
All usages of
.as_slice()and.as_mut_slice()can be removed. Note thatas_mut_slice()is a extended case because it does not actually needmuteither.Comparison with existing lints
No response
Additional Context
No response