Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive for unnecessary_lazy_evaluations #6343

Closed
digama0 opened this issue Nov 18, 2020 · 2 comments · Fixed by #6370
Closed

False positive for unnecessary_lazy_evaluations #6343

digama0 opened this issue Nov 18, 2020 · 2 comments · Fixed by #6370
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@digama0
Copy link
Contributor

digama0 commented Nov 18, 2020

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
    
@digama0 digama0 added the C-bug Category: Clippy is not doing the correct thing label Nov 18, 2020
@giraffate
Copy link
Contributor

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

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Nov 19, 2020
@giraffate
Copy link
Contributor

I will prepare a fix.

bors added a commit that referenced this issue Dec 7, 2020
…s, r=llogiq

Fix FP in `unnecessary_lazy_evaluations`

Fix #6343

changelog: Fix FP in `unnecessary_lazy_evaluations`
@bors bors closed this as completed in aaed9d9 Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants