Skip to content

unnecessary_lazy_evaluations: Shouldn't warn about values with significant Drop impl #9427

Closed
@sdroege

Description

@sdroege

Summary

The case in question that was wrongly reported is the following

warning: unnecessary closure used with `bool::then`
   --> glib/src/convert.rs:122:9
    |
122 |         (iconv as isize != -1).then(|| Self(iconv))
    |         ^^^^^^^^^^^^^^^^^^^^^^^--------------------
    |                                |
    |                                help: use `then_some(..)` instead: `then_some(Self(iconv))`
    |
    = 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

Self here has a significant Drop impl, specifically it will close the isize there (it's basically like a fd and closing -1 is UB).

Lint Name

unnecessary_lazy_evaluations

Reproducer

I tried this code:

struct Foo(i32);

impl Drop for Foo {
    fn drop(&mut self) {
        println!("{}", self.0);
    }
}

fn main() {
    (0 == 1).then(|| Foo(0));
}

I saw this happen:

warning: unnecessary closure used with `bool::then`
  --> src/main.rs:10:5
   |
10 |     (0 == 1).then(|| Foo(0));
   |     ^^^^^^^^^---------------
   |              |
   |              help: use `then_some(..)` instead: `then_some(Foo(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

I expected to see this happen: nothing because the suggestion gives different behaviour

Version

rustc 1.65.0-nightly (289279de1 2022-09-04)
binary: rustc
commit-hash: 289279de116707f28cf9c18e4bbb8c6ec84ad75b
commit-date: 2022-09-04
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 15.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-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