Skip to content

missing_const_for_fn should respect MSRV for trait bounds #15079

Closed
@ctz

Description

@ctz

Summary

I have a project with MSRV 1.60.0, which notably predates trait bounds on const fns. missing_const_for_fn makes a number of suggestions that aren't compatible with the projects MSRV. This is not a major issue for me, as all the error messages are pretty understandable. But nonetheless I'd like to be able to warn on this lint for future changes.

Lint Name

missing_const_for_fn

Reproducer

I tried this code:

#![warn(clippy::missing_const_for_fn)]

pub trait Trait {}

pub struct Struct<T: Trait> {
    _t: T,
}

impl<T: Trait> Struct<T> {
    pub fn new(_t: T) -> Self {
        Self { _t }
    }
}

with Cargo.toml:

[package]
name = "huh6"
version = "0.1.0"
edition = "2021"
rust-version = "1.60"

[dependencies]

I saw this happen:

$ cargo +nightly clippy
warning: this could be a `const fn`
  --> src/lib.rs:10:5
   |
10 | /     pub fn new(_t: T) -> Self {
11 | |         Self { _t }
12 | |     }
   | |_____^
   |
   = 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:1:9
   |
1  | #![warn(clippy::missing_const_for_fn)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: make the function `const`
   |
10 |     pub const fn new(_t: T) -> Self {
   |         +++++

warning: `huh6` (lib) generated 1 warning (run `cargo clippy --fix --lib -p huh6` to apply 1 suggestion)

Accepting that change, I see:

$ cargo +1.60 check
    Checking huh6 v0.1.0 (/home/jbp/tmp/huh6)
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
  --> src/lib.rs:9:6
   |
9  | impl<T: Trait> Struct<T> {
   |      ^
10 |     pub const fn new(_t: T) -> Self {
   |     ------------------------------- function declared as const here
   |
   = note: see issue #93706 <https://github.com/rust-lang/rust/issues/93706> for more information

For more information about this error, try `rustc --explain E0658`.
error: could not compile `huh6` due to previous error

Instead I would expect no missing_const_for_fn lint.

Version

rustc 1.89.0-nightly (c68340350 2025-06-18)
binary: rustc
commit-hash: c68340350c78eea402c4a85f8d9c1b7d3d607635
commit-date: 2025-06-18
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions