Skip to content

[False Positive] About removing a into_iter() from an ops::Range #14656

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

Open
leonardo-m opened this issue Apr 20, 2025 · 1 comment · May be fixed by #14800
Open

[False Positive] About removing a into_iter() from an ops::Range #14656

leonardo-m opened this issue Apr 20, 2025 · 1 comment · May be fixed by #14800
Assignees
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

Comments

@leonardo-m
Copy link

leonardo-m commented Apr 20, 2025

Summary

If I have this little Rust program:

#![warn(clippy::all)]
use std::ops::Range;

const fn signature(mut n: u32) -> u32 {
    let mut result = 0;
    loop {
        result += 1 << ((n % 10) * 4);
        n /= 10;
        if n == 0 { return result; }
    }
}

fn main() {
    const R: Range<u32> = 2 .. 7; // Input.
    let g = |n, sn| R.into_iter().all(|m| signature(n * m) == sn);
    let res = (1 .. u32::MAX).find(|&n| g(n, signature(n)));
    assert_eq!(res, Some(142_857));
}

Clippy gives me this warning:

warning: useless conversion to the same type: `std::ops::Range<u32>`
  --> src/main.rs:15:21
   |
15 |     let g = |n, sn| R.into_iter().all(|m| signature(n * m) == sn);
   |                     ^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `R`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::all)]
   |         ^^^^^^^^^^^
   = note: `#[warn(clippy::useless_conversion)]` implied by `#[warn(clippy::all)]`

But if I remove ".into_iter()" from the Rust program, the rustc compiler (1.88.0-nightly 2025-04-19) gives me:

warning: taking a mutable reference to a `const` item
  --> ...\bug1.rs:15:21
   |
15 |     let g = |n, sn| R.all(|m| signature(n * m) == sn);
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: each usage of a `const` item creates a new temporary
   = note: the mutable reference will refer to this temporary, not the original `const` item
note: mutable reference created due to call to this method
  --> /rustc/077cedc2afa8ac0b727b7a6cbe012940ba228deb\library\core\src\iter\traits\iterator.rs:2719:5
note: `const` item defined here
  --> ...\bug1.rs:14:5
   |
14 |     const R: Range<u32> = 2 .. 7; // Input.
   |     ^^^^^^^^^^^^^^^^^^^
   = note: `#[warn(const_item_mutation)]` on by default

Lint Name

No response

Reproducer

I tried this code:

<code>

I saw this happen:

<output>

I expected to see this happen:

Version


Additional Labels

No response

@leonardo-m leonardo-m 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 Apr 20, 2025
@relaxcn
Copy link
Contributor

relaxcn commented May 14, 2025

@rustbot claim

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants