Skip to content

False positive for unnecessary_lazy_evaluations #6343

Closed
@digama0

Description

@digama0

A minimal example:

fn main() {
    let arr = [(Some(1),)];
    Some(&0).and_then(|&i| arr[i].0);
}

When clippy::unnecessary_lazy_evaluations is on, you get:

warning: unnecessary closure used to substitute value for `Option::None`
 --> src/main.rs:3:5
  |
3 |     Some(&0).and_then(|&i| arr[i].0);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `and` instead: `Some(&0).and(arr[i].0)`
  |
  = note: `#[warn(clippy::unnecessary_lazy_evaluations)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations

The suggested fix is not well formed because it refers to variable i. A fix should check that the expression in the closure does not depend on any variables in the arguments. (This also arguably isn't a candidate for the lint, because the indexing expression has side effects and a possible computational cost as well.)

Meta

  • cargo clippy -V: clippy 0.0.212 (1773f60 2020-11-08)
  • rustc -Vv:
    rustc 1.49.0-nightly (1773f60ea 2020-11-08)
    binary: rustc
    commit-hash: 1773f60ea5d42e86b8fdf78d2fc5221ead222bc1
    commit-date: 2020-11-08
    host: x86_64-unknown-linux-gnu
    release: 1.49.0-nightly
    

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 applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions