Skip to content

manual_slice_fill wrongly suggest non-const initializers #14192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
samueltardieu opened this issue Feb 10, 2025 · 6 comments · Fixed by #14193
Closed

manual_slice_fill wrongly suggest non-const initializers #14192

samueltardieu opened this issue Feb 10, 2025 · 6 comments · Fixed by #14193
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@samueltardieu
Copy link
Contributor

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

@samueltardieu samueltardieu added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 10, 2025
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Feb 10, 2025
@samueltardieu
Copy link
Contributor Author

samueltardieu commented Feb 10, 2025

Ping @lapla-cogito

Note that #14191 fixes another bug, when the slice is initialized from an iterator.

@lapla-cogito
Copy link
Contributor

Thank you for letting me know, I submitted #14193.

@samueltardieu
Copy link
Contributor Author

That was fast!

github-merge-queue bot pushed a commit that referenced this issue Feb 12, 2025
fix #14192

changelog: [`manual_slice_fill`]: resolve FP caused by missing index
checks for the slice
@marxin
Copy link

marxin commented Apr 4, 2025

Don't know how it's possible, but it seems this issue somehow sneaked into 1.86.0:

❯ cargo +stable clippy
warning: manually filling a slice
   --> linker-utils/src/elf.rs:719:9
    |
719 | /         for b in &mut mask {
720 | |             *b = !*b;
721 | |         }
    | |_________^ help: try: `mask.fill(!*b);`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_slice_fill
    = note: `#[warn(clippy::manual_slice_fill)]` on by default

CI run: https://github.com/davidlattimore/wild/actions/runs/14258683395/job/39965815664?pr=642

@y21
Copy link
Member

y21 commented Apr 4, 2025

Don't know how it's possible, but it seems this issue somehow sneaked into 1.86.0:

1.86 branched from master on Feb 14, and the fix only made it into the rust repository on Mar 2, so it missed the 1.86 release. It should be fixed on beta and will be in the next release 1.87.

@marxin
Copy link

marxin commented Apr 4, 2025

I can confirm it's OK with the current nightly. I think the issue is pretty common, and I would consider backporting the fix to 1.86.1, what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants