"using `filter_map` when a more succinct alternative exists"
}
-declare_clippy_lint! {
- /// **What it does:** Checks for `into_iter` calls on types which should be replaced by `iter` or
- /// `iter_mut`.
- ///
- /// **Why is this bad?** Arrays and `PathBuf` do not yet have an `into_iter` method which move out
- /// their content into an iterator. Auto-referencing resolves the `into_iter` call to its reference
- /// instead, like `<&[T; N] as IntoIterator>::into_iter`, which just iterates over item references
- /// like calling `iter` would. Furthermore, when the standard library actually
- /// [implements the `into_iter` method](https://github.com/rust-lang/rust/issues/25725) which moves
- /// the content out of the array, the original use of `into_iter` got inferred with the wrong type
- /// and the code will be broken.
- ///
- /// **Known problems:** None
- ///
- /// **Example:**
- ///
- /// ```rust
- /// let _ = [1, 2, 3].into_iter().map(|x| *x).collect::<Vec<u32>>();
- /// ```
- /// Could be written as:
- /// ```rust
- /// let _ = [1, 2, 3].iter().map(|x| *x).collect::<Vec<u32>>();
- /// ```
- pub INTO_ITER_ON_ARRAY,
- correctness,
- "using `.into_iter()` on an array"
-}
-
declare_clippy_lint! {
/// **What it does:** Checks for `into_iter` calls on references which should be replaced by `iter`
/// or `iter_mut`.
USELESS_ASREF,
UNNECESSARY_FOLD,
UNNECESSARY_FILTER_MAP,
- INTO_ITER_ON_ARRAY,
INTO_ITER_ON_REF,
SUSPICIOUS_MAP,
UNINIT_ASSUMED_INIT,
}
}
-fn ty_has_iter_method(
- cx: &LateContext<'_, '_>,
- self_ref_ty: Ty<'_>,
-) -> Option<(&'static Lint, &'static str, &'static str)> {
+fn ty_has_iter_method(cx: &LateContext<'_, '_>, self_ref_ty: Ty<'_>) -> Option<(&'static str, &'static str)> {
has_iter_method(cx, self_ref_ty).map(|ty_name| {
- let lint = if ty_name == "array" || ty_name == "PathBuf" {
- INTO_ITER_ON_ARRAY
- } else {
- INTO_ITER_ON_REF
- };
let mutbl = match self_ref_ty.kind {
ty::Ref(_, _, mutbl) => mutbl,
_ => unreachable!(),
hir::MutImmutable => "iter",
hir::MutMutable => "iter_mut",
};
- (lint, ty_name, method_name)
+ (ty_name, method_name)
})
}
if !match_trait_method(cx, expr, &paths::INTO_ITERATOR) {
return;
}
- if let Some((lint, kind, method_name)) = ty_has_iter_method(cx, self_ref_ty) {
+ if let Some((kind, method_name)) = ty_has_iter_method(cx, self_ref_ty) {
span_lint_and_sugg(
cx,
- lint,
+ INTO_ITER_ON_REF,
method_span,
&format!(
"this .into_iter() call is equivalent to .{}() and will not move the {}",