rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`missing-const-for-fn` when `const fn` would require unstable features

Open clubby789 opened this issue 2 years ago • 1 comments

Summary

missing-const-for-fn warns 'this could be a const fn' on code that cannot be due to the required feature being unstable.

Lint Name

missing-const-for-fn

Reproducer

I tried this code:

pub struct Holder {
    val: *const u8
}
impl Holder {
    pub fn func(self) -> usize {
        unsafe {*self.val as usize}
    }
}

I saw this happen:

> cargo +1.54 clippy -- -W clippy::missing_const_for_fn
    Checking repro v0.1.0 (/tmp/repro)
warning: this could be a `const fn`
 --> src/lib.rs:5:5
  |
5 | /     pub fn func(self) -> usize {
6 | |         unsafe {*self.val as usize}
7 | |     }
  | |_____^
  |
  = note: requested on the command line with `-W clippy::missing-const-for-fn`

When adding the suggested const header:

> cargo +1.54 build
   Compiling repro v0.1.0 (/tmp/repro)
error[E0658]: dereferencing raw pointers in constant functions is unstable
 --> src/lib.rs:6:17
  |
6 |         unsafe {*self.val as usize}
  |                 ^^^^^^^^^
  |
  = note: see issue #51911 <https://github.com/rust-lang/rust/issues/51911> for more information

Version

rustc 1.54.0 (a178d0322 2021-07-26)
binary: rustc
commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
commit-date: 2021-07-26
host: x86_64-unknown-linux-gnu
release: 1.54.0
LLVM version: 12.0.1

Additional Labels

@rustbot label +I-suggestion-causes-error

clubby789 avatar May 21 '22 14:05 clubby789

This needs msrv: https://github.com/rust-lang/rust/blob/stable/RELEASES.md#version-1580-2022-01-13

giraffate avatar May 23 '22 23:05 giraffate