Skip to content

New lint: iter_overeager_cloned #8202

Closed
@pmnoxx

Description

@pmnoxx

What it does

Calling .iter().cloned.last() involved doing unnecessary work.
It's better to write it as .iter().last().cloned()

Same thing can rule can be applied to:

  • .iter().cloned().next()
  • .iter().cloned().count()
  • .iter().cloned().flatten()
  • .iter().cloned().take(...)
  • .iter().cloned().skip(...)
  • .iter().cloned().nth(...)

Lint Name

cloned_last

Category

style, perf

Advantage

  • More efficient code

Drawbacks

  • I don't see any

Example

    let _: Option<String> = vec!["1".to_string(), "2".to_string(), "3".to_string()]
        .iter().cloned().last()

Could be written as:

    let _: Option<String> = vec!["1".to_string(), "2".to_string(), "3".to_string()]
        .iter().last().cloned()

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