Skip to content

manual_slice_fill wrongly suggest non-const initializers #14192

@samueltardieu

Description

@samueltardieu

Summary

manual_slice_fill does not check:

  • that the loop variable is used to index the slice being initialized
  • that the expression doesn't reference the slice content (which may have been modified earlier in the loop) or the index (which is modified for every element)

Lint Name

manual_slice_fill

Reproducer

I tried this code:

fn main() {
    let mut tmp = vec![0; 3];

    for i in 0..tmp.len() {
        tmp[i] = i;
    }

    for i in 0..tmp.len() {
        tmp[0] = i;
    }
}

I saw this happen, for both loops:

warning: manually filling a slice
  --> /tmp/t.rs:8:5
   |
8  | /     for i in 0..tmp.len() {
9  | |         tmp[0] = i;
10 | |     }
   | |_____^ help: try: `tmp.fill(i);`

I expected to see this happen: no lint, since in the first loop the initializer depends on the loop variable, and in the second loop the loop variable is not used to index the slice.

Version

rustc 1.86.0-nightly (a9730c3b5 2025-02-05)
binary: rustc
commit-hash: a9730c3b5f84a001c052c60c97ed0765e9ceac04
commit-date: 2025-02-05
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    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