Skip to content

Suggest ! return type for functions that never return a value #12290

Open
@rootCircle

Description

@rootCircle

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

  • never type 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions