rust-lang / rust-lang/rust-clippy
Suggest ! return type for functions that never return a value
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Rust's ! type, also known as the never type, signifies that a function never returns normally. Clippy currently doesn't have a lint to suggest using ! when a function never returns a value or will result into diverging computation(eg., an infinite loop or panic), which could lead to potential type safety issues and misleading code patterns.
Advantage
- Improved type safety by preventing accidental assignment of panic-only functions to variables expecting a value.
- Clearer code intent, making it more obvious when functions don't return normally.
- Potential for better error handling and code analysis.
Drawbacks
nevertype is still in nightly, and still will take time before it's stabilized, so better to wait in that case. (https://doc.rust-lang.org/nightly/std/primitive.never.html)- It could be challenging to write a foolproof lint that accurately distinguishes functions that always panic from those that may return under some conditions.
Example
fn will_error_out() {
panic!("It returns nothing, but it's not known to compiler")
}
This function always panics, but its return type is implicitly () (unit), suggesting it might return a value. Adding ! as the return type would clarify its behavior:
fn will_error_out() -> ! {
panic!("It returns nothing, but it's now explicit")
}
Specifying a never return type will make it easier for its usage in where other functions are dependent on this function.
let some_value: bool = will_error_out(); // will not error out for second case
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing Rust's nightly ! type documentation and the issue's will_error_out examples. Determine how a Clippy lint could reliably identify functions that always diverge without false positives; done would require a defined scope and tests covering panic-only, infinite-loop, and conditionally returning functions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100