-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
A-lintArea: New lintsArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.Call for participation: Medium difficulty level problem and requires some initial experience.
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 caseMetadata
Metadata
Assignees
Labels
A-lintArea: New lintsArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.Call for participation: Medium difficulty level problem and requires some initial experience.