rust-lang / rust-lang/rust-clippy

`zero_repeat_side_effects` generic type needed

Open
#16,474 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Using the following flags

--force-warn clippy::zero_repeat_side_effects

this code:

use std::sync::{Arc, Mutex};

#[derive(Default, Clone)]
struct Entry<T> {
    id: usize,
    data: Arc<Mutex<T>>,
}

pub fn main() {
    let mut entries = vec![Entry::default(); 0];

    for (i, e) in entries.iter_mut().enumerate() {
        e.id = i;
        *e.data.lock().unwrap() = i;
    }

    let entry_0 = &entries[0];

    assert_eq!(entry_0.id, *entry_0.data.lock().unwrap());
}

caused the following diagnostics:

    Checking _1699114377166c9e733bf58721b0988034c0befe v0.1.0 (/tmp/icemaker_global_tempdir.dESjtcUkYIKG/icemaker_clippyfix_tempdir.SY1eTlU0cljd/_1699114377166c9e733bf58721b0988034c0befe)
warning: expression with side effects as the initial value in a zero-sized array initializer
  --> src/main.rs:10:5
   |
10 |     let mut entries = vec![Entry::default(); 0];
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#zero_repeat_side_effects
   = note: requested on the command line with `--force-warn clippy::zero-repeat-side-effects`
help: consider performing the side effect separately
   |
10 ~     Entry::default();
11 +     let mut entries: std::vec::Vec<Entry<usize>> = vec![];
   |

warning: `_1699114377166c9e733bf58721b0988034c0befe` (bin "_1699114377166c9e733bf58721b0988034c0befe") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.34s

However after applying these diagnostics, the resulting code:

use std::sync::{Arc, Mutex};

#[derive(Default, Clone)]
struct Entry<T> {
    id: usize,
    data: Arc<Mutex<T>>,
}

pub fn main() {
    Entry::default();
    let mut entries: std::vec::Vec<Entry<usize>> = vec![];

    for (i, e) in entries.iter_mut().enumerate() {
        e.id = i;
        *e.data.lock().unwrap() = i;
    }

    let entry_0 = &entries[0];

    assert_eq!(entry_0.id, *entry_0.data.lock().unwrap());
}

no longer compiled:

    Checking _1699114377166c9e733bf58721b0988034c0befe v0.1.0 (/tmp/icemaker_global_tempdir.dESjtcUkYIKG/icemaker_clippyfix_tempdir.SY1eTlU0cljd/_1699114377166c9e733bf58721b0988034c0befe)
error[E0283]: type annotations needed
  --> src/main.rs:10:5
   |
10 |     Entry::default();
   |     ^^^^^ cannot infer type for type parameter `T`
   |
   = note: cannot satisfy `_: std::default::Default`
note: required for `Entry<_>` to implement `std::default::Default`
  --> src/main.rs:3:10
   |
 3 | #[derive(Default, Clone)]
   |          ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro

For more information about this error, try `rustc --explain E0283`.
error: could not compile `_1699114377166c9e733bf58721b0988034c0befe` (bin "_1699114377166c9e733bf58721b0988034c0befe") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_1699114377166c9e733bf58721b0988034c0befe` (bin "_1699114377166c9e733bf58721b0988034c0befe" test) due to 1 previous error

Version:

rustc 1.95.0-nightly (94a0cd15f 2026-01-27)
binary: rustc
commit-hash: 94a0cd15f5976fa35e5e6784e621c04e9f958e57
commit-date: 2026-01-27
host: x86_64-unknown-linux-gnu
release: 1.95.0-nightly
LLVM version: 21.1.8

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided Rust example with --force-warn clippy::zero_repeat_side_effects, then trace the zero_repeat_side_effects lint's help suggestion. The fix is done when the suggested rewrite compiles despite the unconstrained generic type and still addresses the reported side-effect warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.