Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clippy_lints/src/loops/manual_slice_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::{HasSession, snippet_with_applicability};
use clippy_utils::ty::implements_trait;
use clippy_utils::ty::{implements_trait, is_slice_like};
use clippy_utils::visitors::is_local_used;
use clippy_utils::{higher, peel_blocks_with_stmt, span_contains_comment};
use rustc_ast::ast::LitKind;
Expand Down Expand Up @@ -58,6 +58,8 @@ pub(super) fn check<'tcx>(
&& let Res::Local(idx_hir) = idx_path.res
&& !is_local_used(cx, assignval, idx_hir)
&& msrv.meets(cx, msrvs::SLICE_FILL)
&& let slice_ty = cx.typeck_results().expr_ty(slice).peel_refs()
&& is_slice_like(cx, slice_ty)
{
sugg(cx, body, expr, slice.span, assignval.span);
}
Expand Down
7 changes: 7 additions & 0 deletions clippy_utils/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,3 +1422,10 @@ pub fn has_non_owning_mutable_access<'tcx>(cx: &LateContext<'tcx>, iter_ty: Ty<'
let mut phantoms = FxHashSet::default();
has_non_owning_mutable_access_inner(cx, &mut phantoms, iter_ty)
}

/// Check if `ty` is slice-like, i.e., `&[T]`, `[T; N]`, or `Vec<T>`.
pub fn is_slice_like<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
ty.is_slice()
|| ty.is_array()
|| matches!(ty.kind(), ty::Adt(adt_def, _) if cx.tcx.is_diagnostic_item(sym::Vec, adt_def.did()))
}
37 changes: 37 additions & 0 deletions tests/ui/manual_slice_fill.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,40 @@ fn issue14189() {
*b = !*b;
}
}

mod issue14685 {
use std::ops::{Index, IndexMut};

#[derive(Clone)]
struct ZipList<T>(T);

impl<T> ZipList<T> {
fn len(&self) -> usize {
todo!()
}

fn is_empty(&self) -> bool {
todo!()
}
}

impl<T> Index<usize> for ZipList<T> {
type Output = T;

fn index(&self, _: usize) -> &Self::Output {
todo!()
}
}

impl<T> IndexMut<usize> for ZipList<T> {
fn index_mut(&mut self, _: usize) -> &mut Self::Output {
todo!()
}
}

fn index_mut(mut zl: ZipList<usize>) {
for i in 0..zl.len() {
zl[i] = 6;
}
}
}
37 changes: 37 additions & 0 deletions tests/ui/manual_slice_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,40 @@ fn issue14189() {
*b = !*b;
}
}

mod issue14685 {
use std::ops::{Index, IndexMut};

#[derive(Clone)]
struct ZipList<T>(T);

impl<T> ZipList<T> {
fn len(&self) -> usize {
todo!()
}

fn is_empty(&self) -> bool {
todo!()
}
}

impl<T> Index<usize> for ZipList<T> {
type Output = T;

fn index(&self, _: usize) -> &Self::Output {
todo!()
}
}

impl<T> IndexMut<usize> for ZipList<T> {
fn index_mut(&mut self, _: usize) -> &mut Self::Output {
todo!()
}
}

fn index_mut(mut zl: ZipList<usize>) {
for i in 0..zl.len() {
zl[i] = 6;
}
}
}