Email me

Clippy lints / Nursery

Clippy lint · Nursery · checked with Clippy 0.1.99

clippy::missing_const_for_fn: this could be a `const fn`

What it meansSuggests the use of const in functions and methods where possible. It is in the nursery group and off by default.

What you see

warning: this could be a `const fn`
 --> main.rs:3:1
  |
3 | / fn a() -> i32 {
4 | |     0
5 | | }
  | |_^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#missing_const_for_fn
  = note: `-W clippy::missing-const-for-fn` implied by `-W clippy::nursery`
help: make the function `const`
  |
3 | const fn a() -> i32 {
  | +++++

Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.

Does it matter?

Not having the function const prevents callers of the function from being const as well.

New lints still being finished. Off by default and may have false positives.

How to fix it

Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.

Flagged

fn a() -> i32 {
    0
}
fn b() -> i32 {
    a()
}

Write this instead

fn new() -> Self {
    Self { random_number: 42 }
}

Turn it off, or on

It is off by default. To use it, set it to warn or deny:

Whole crate (Cargo.toml)

[lints.clippy]
missing_const_for_fn = "warn"

In code

#![warn(clippy::missing_const_for_fn)]

One run

cargo clippy -- -W clippy::missing-const-for-fn

The [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.

Settings in clippy.toml

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Nurserygroup
allowdefault level
yesfixed by cargo clippy --fix
1.34.0added in Rust

Clippy's own documentation

What it does

Suggests the use of const in functions and methods where possible.

Why is this bad?

Not having the function const prevents callers of the function from being const as well.

Known problems

Const functions are currently still being worked on, with some features only being available on nightly. This lint does not consider all edge cases currently and the suggestions may be incorrect if you are using this lint on stable.

Also, the lint only runs one pass over the code. Consider these two non-const functions:

fn a() -> i32 {
    0
}
fn b() -> i32 {
    a()
}

When running Clippy, the lint will only suggest to make a const, because b at this time can’t be const as it calls a non-const function. Making a const and running Clippy again, will suggest to make b const, too.

If you are marking a public function with const, removing it again will break API compatibility.

Example

fn new() -> Self {
    Self { random_number: 42 }
}

Could be a const fn:

const fn new() -> Self {
    Self { random_number: 42 }
}

Configuration

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml

    (default: current version)

From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.

Related lints

All 847 Clippy lints · Rust compiler errors · Cargo.toml checker

Up next

Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ