Skip to content

zero_repeat_side_effects grossly mishandles a very particular pattern #15824

@ada4a

Description

@ada4a

Summary

The lint has a suggestion template for when the array/vec initializer is a part of a LetStmt, but in this particular case:

match 0 {
    0 => _ = [f(); 0],
    _ => {},
}

its suggestion:

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

doesn't work for multiple reasons:

  • Despite this being a Stmt in a Block, it has no surrounding braces, so f(); _: [(); 0] = [];, which introduces a second statement, is invalid
  • The _: [(); 0] = []; part won't compile anyway -- the LetStmt is lacking a let, and so a type ascription is not allowed
    • The following would work though: _ = [] as [(); 0];
  • Lastly, it adds a let that wasn't there to begin with

(EDIT: the last two happen on regular _ = [f(); 0];s as well, opened a separate issue for that: #15825)

Reproducer

I tried this code:

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

fn f() {}

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

This happened:

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

I expected this to happen:
No suggestion

Version

rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: x86_64-unknown-linux-gnu
release: 1.90.0
LLVM version: 20.1.8

Additional Labels

@rustbot label I-suggestion-causes-error

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