Skip to content

missing-const-for-fn clippy lint regression in Rust 1.86 #139338

Closed
@joshka

Description

@joshka

I tried this code:

#![allow(unused)]
#![deny(clippy::missing_const_for_fn)]
struct Buffer {
    content: Vec<u8>,
}

impl Buffer {
    pub fn content(&self) -> &[u8] {
        &self.content
    }
}

I expected to see this happen: No clippy errors

Instead, this happened:

error: this could be a `const fn`
  --> src/lib.rs:8:5
   |
8  | /     pub fn content(&self) -> &[u8] {
9  | |         &self.content
10 | |     }
   | |_____^
   |
   = 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
  --> src/lib.rs:2:9
   |
2  | #![deny(clippy::missing_const_for_fn)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: make the function `const`
   |
8  |     pub const fn content(&self) -> &[u8] {
   |         +++++

Changing the code as suggested gives a compiler error:

❯ cargo check              
    Checking test-missing-const-for-fn v0.1.0 (/Users/joshka/local/test-missing-const-for-fn)
error[E0015]: cannot perform non-const deref coercion on `Vec<u8>` in constant functions
    --> src/lib.rs:9:9
     |
9    |         &self.content
     |         ^^^^^^^^^^^^^
     |
     = note: attempting to deref into `[u8]`
note: deref defined here
    --> /Users/joshka/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:3262:5
     |
3262 |     type Target = [T];
     |     ^^^^^^^^^^^
note: impl defined here, but it is not `const`
    --> /Users/joshka/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:3261:1
     |
3261 | impl<T, A: Allocator> ops::Deref for Vec<T, A> {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

For more information about this error, try `rustc --explain E0015`.
error: could not compile `test-missing-const-for-fn` (lib) due to 1 previous error

Meta

In beta the clippy error does not show:

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: aarch64-apple-darwin
release: 1.86.0
LLVM version: 19.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions