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-fnThe [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
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 therust-versionfield inCargo.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
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- iter_with_drainChecks for usage of .drain(..) on Vec and VecDeque for iteration
- large_stack_framesChecks for functions that use a lot of stack space. This often happens when constructing a large type, such as an array with a lot of elements, or constructing many smaller-but-still-large structs, or copying around a lot of large types. This lint is a more general version of large_stack_arrays that is intended to look at functions as a whole instead of only individual array expressions inside of a function
- literal_string_with_formatting_argsthis looks like a formatting argument but it is not part of a formatting macro
- needless_collectChecks for functions collecting an iterator when collect is not needed
- needless_pass_by_ref_mutthis parameter is a mutable reference but is not used mutably
- needless_type_castChecks for bindings (constants, statics, or let bindings) that are defined with one numeric type but are consistently cast to a different type in all usages
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?