Skip to content

[False Positive] Regarding adding const to a function that can't be const #14658

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 · 0 comments · May be fixed by #14759
Open

[False Positive] Regarding adding const to a function that can't be const #14658

leonardo-m opened this issue Apr 20, 2025 · 0 comments · May be fixed by #14759
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

Summary

If I have this little Rust program (probably it's not minimized, but it's short enough):

#![feature(const_trait_impl)]
#![feature(nonzero_internals)]
#![allow(internal_features)]

#![warn(clippy::all)]
#![warn(clippy::missing_const_for_fn)]

use std::num::{NonZero, ZeroablePrimitive};

//- - - - - - - -

trait NarrowRem<Out: ZeroablePrimitive> {
    fn narrowing_rem(&self, den: NonZero<Out>) -> Out;
}


impl NarrowRem<u32> for u64 {
    #[inline(always)]
    fn narrowing_rem(&self, den: NonZero<u32>) -> u32 {
        (*self % NonZero::from(den)) as _
    }
}

//- - - - - - - -

#[const_trait]
trait MulModCast where Self: ZeroablePrimitive {
    fn mul_mod(self, b: Self, m: NonZero<Self>) -> Self;
}

impl MulModCast for u32 {
    #[inline]
    fn mul_mod(self, b: Self, m: NonZero<Self>) -> Self {
        (u64::from(self) * u64::from(b)).narrowing_rem(m)
    }
}

//- - - - - - - -

const fn pow_mod(mut a: u32, mut x: u32, nz: NonZero<u32>) -> u32
where u32: MulModCast {
    let mut r: u32 = 1;
    while x != 0 {
        if (x & 1) == 1 {
            r = a.mul_mod(r, nz);
        }
        x >>= 1;
        a = a.mul_mod(a, nz);
    }
    r
}

fn main() {
    assert_eq!(pow_mod(19, 11, NonZero::new(7).unwrap()), 3);
}

Clippy gives me this warning:

  --> ...\main.rs:40:1
   |
40 | / fn pow_mod(mut a: u32, mut x: u32, nz: NonZero<u32>) -> u32
41 | | where u32: MulModCast {
42 | |     let mut r: u32 = 1;
43 | |     while x != 0 {
...  |
50 | |     r
51 | | }
   | |_^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
note: the lint level is defined here
  --> ...\main.rs:6:9
   |
6  | #![warn(clippy::missing_const_for_fn)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: make the function `const`
   |
40 | const fn pow_mod(mut a: u32, mut x: u32, nz: NonZero<u32>) -> u32
   | +++++

But if I add const to pow_mod, the rustc compiler (1.88.0-nightly 2025-04-19) gives me:

error[E0277]: the trait bound `u32: ~const MulModCast` is not satisfied
  --> C:\lavoro\metti_a_posto\progettini_in_corso\eulers\many_solutions\test1\src\main.rs:45:17
   |
45 |             r = a.mul_mod(r, nz);
   |                 ^

error[E0277]: the trait bound `u32: ~const MulModCast` is not satisfied
  --> C:\lavoro\metti_a_posto\progettini_in_corso\eulers\many_solutions\test1\src\main.rs:48:13
   |
48 |         a = a.mul_mod(a, nz);
   |             ^

(I have other examples of false positives of clippy::missing_const_for_fn).

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
@samueltardieu samueltardieu self-assigned this May 8, 2025
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