Skip to content

Unfixable warning in needless_range_loop when iter() is not implemented #3788

@chrisduerr

Description

@chrisduerr

In the following example, it is not possible to fix the clippy lint, since the iter() trait is not implemented:

struct Test {
    inner: Vec<usize>,
}

impl std::ops::Index<usize> for Test {
    type Output = usize;
    fn index(&self, index: usize) -> &Self::Output {
        &self.inner[index]
    }
}

fn main() {
    let test = Test {
        inner: vec![1, 2, 3, 4],
    };
    for i in 0..2 {
        println!("{}", test[i]);
    }
}

It is likely not always possible to implement Iterator for a struct that implements Index, so there are some cases where it will be impossible to fix this lint.

I'd assume this is something that should either be fixed by checking if Iterator is implemented for usize, or it should be added to the Known Issues section.

This is likely a rare issue since usually both Index and Iterator are implemented, however I don't think the assumption can be made that this is always the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-documentationArea: Adding or improving documentationC-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions