Skip to content

False positive for unnecessary_lazy_evaluations #6343

Closed

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
    

Activity

  1. added
    C-bugCategory: Clippy is not doing the correct thing
    on Nov 18, 2020
  2. giraffate commented on Nov 19, 2020

    @giraffate
    Contributor

    @rustbot modify labels: +L-suggestion-causes-error

  3. added
    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
    on Nov 19, 2020
  4. giraffate commented on Nov 20, 2020

    @giraffate
    Contributor

    I will prepare a fix.

  5. added 2 commits that reference this issue on Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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