Skip to content

zero_repeat_side_effects mishandles let-stmts without let #15825

@ada4a

Description

@ada4a

Summary

It replaces

_ = [f(); 0];

with

f(); let _: [(); 0] = [];

-- notice the added let .

Detecting this would probably involve checking whether the source field of LetStmt is LocalSource::AssignDesugar.

Note that that by itself won't be enough, i.e. the following wouldn't compile:

f(); _: [(); 0] = [];

That's because type ascriptions are apparently not allowed on LetStmts without a let -- so one would need to suggest f(); _ = [] as [(); 0]; instead.

Reproducer

I tried this code:

#![allow(unused)]
#![warn(clippy::zero_repeat_side_effects)]

fn f() {}

fn foo() {
    _ = [f(); 0];
}

I expected to see this happen:

warning: function or method calls as the initial value in zero-sized array initializers may cause side effects
 --> src/main.rs:7:5
  |
7 |     _ = [f(); 0];
  |     ^^^^^^^^^^^^ help: consider using: `f(); _ = [] as [(); 0];`
  |

Instead, this happened:

warning: function or method calls as the initial value in zero-sized array initializers may cause side effects
 --> src/main.rs:7:5
  |
7 |     _ = [f(); 0];
  |     ^^^^^^^^^^^^ help: consider using: `f(); let _: [(); 0] = [];`
  |

Version


Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions