Skip to content

Structs with non-matching Index and Iter impls cause bad suggestions #2847

Open
@Others

Description

@Others

I'm using the llvm-alt library, which exports a Function type representing a function embedded in an llvm module. Calling iter on a function iterates through its basic blocks, but indexing into it accesses the function parameter values.

Yes, this is a bizarre design decision, but it's a wart on an otherwise great library.

Under this setup I wrote the following code:

for i in 0..llvm_f.get_signature().get_params().len() {
    do_something(llvm_f[i]);
}

When I run clippy on my crate, I get the following diagnostic:

warning: the loop variable `i` is only used to index `llvm_f`.
  --> src/codegen/function.rs:36:14
   |
36 |     for i in 0..llvm_f.get_signature().get_params().len() {
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.207/index.html#needless_range_loop
help: consider using an iterator
   |
36 |     for <item> in llvm_f.iter().take(llvm_f.get_signature().get_params().len()) {
   |         ^^^^^^    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This warning and suggestion do not make sense, since the iter implementation would do something totally different. (Iterate through basic blocks.) Now I know this isn't a common case, or even a useful one. But it would be nice if Clippy didn't produce this spurious diagnostic. (This is bad library code, not badness in my code.)

I think this could probably be avoided if Clippy just compared the existing Iterator and Index implementations on the type, and made sure they both operated on the same kind of value. I don't know how hard this would be to implement. I can have a go at it myself if someone points me in the right direction.

Obviously this isn't a very important bug, but it appears to technically be a spurious diagnostic, so I thought I'd report it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions