Skip to content

New lint suggestion: unused call to enumerate() #7527

@dbr

Description

@dbr

What it does

Warn in cases where enumerate is called, but the enumerated value is ignored (_i)

Categories (optional)

  • Kind: I guess "complexity"

What is the advantage of the recommended code over the original code

Removes an unnecessary call, which introduces more code in what could otherwise be a simple for loop.

This can happen if .enumerate() is needed, then in the process of refactoring the index is no longer used - someone might silence the "unused variable warning" by putting an underscore in front of the first item in the tuple (something I think cargo fix will do automatically)

Drawbacks

None.

Example

let things = vec!["a", "b", "c"];
for (_i, item) in things.iter().enumerate() {
}

Could be written as:

let things = vec!["a", "b", "c"];
for item in things {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions