Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::implicit_return: missing `return` statement
What it meansChecks for missing return statements at the end of a block. It is in the restriction group and off by default.
What you see
warning: missing `return` statement --> main.rs:4:5 | 4 | x | ^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#implicit_return = note: requested on the command line with `-W clippy::implicit-return` help: add `return` as shown | 4 | return x | ++++++
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?
Omitting the return keyword whenever possible is idiomatic Rust code, but: Programmers coming from other languages might prefer the expressiveness of return. It’s possible to miss the last returning statement because the only difference is a missing ;. Especially in bigger code with multiple return paths, having a return keyword makes it easier to find the corresponding statements.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
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 foo(x: usize) -> usize {
x
}
Write this instead
fn foo(x: usize) -> usize {
return x;
}
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]
implicit_return = "warn"In code
#![warn(clippy::implicit_return)]One run
cargo clippy -- -W clippy::implicit-returnThe [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.
Clippy's own documentation
What it does
Checks for missing return statements at the end of a block.
Why restrict this?
Omitting the return keyword whenever possible is idiomatic Rust code, but:
- Programmers coming from other languages might prefer the expressiveness of
return. - It’s possible to miss the last returning statement because the only difference is a missing
;. - Especially in bigger code with multiple return paths, having a
returnkeyword makes it easier to find the corresponding statements.
Example
fn foo(x: usize) -> usize {
x
}
add return
fn foo(x: usize) -> usize {
return x;
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- host_endian_bytesuse of `to_ne_bytes`
- if_then_some_else_noneChecks for if-else that could be written using either bool::then or bool::then_some
- impl_trait_in_paramsLints when impl Trait is being used in a function’s parameters
- indexing_slicingindexing may panic
- infinite_loopinfinite loop detected
- inline_asm_x86_att_syntaxChecks for usage of AT&T x86 assembly syntax
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?