Skip to content

Needless / unnecessary Result #6265

Closed
Closed
@emilk

Description

@emilk

What it does

fn foo() -> Result<i32, Error> {
    Ok(42)
}

Suggested clippy lint: "Needless use of Result (function never returns Err). Consider changing the return type to i32 ."

(Unsure if it should be "needless" or "unnecessary", see #2845)

Categories (optional)

clippy::complexity (code that does something simple but in a complex way)

The use of this lint would most often come up after refactoring some code, when a function that used to have a ? in it no longer thus, and so now never returns an error.

Removing unnecessary uses of Result makes your code cleaner and clearer.

Drawbacks

Sometimes the user wants to return a Result because they plan on adding a failure condition later.

Example

fn foo() -> Result<i32, Error> {
    Ok(42)
}

Could be written as:

fn foo() -> i32 {
    42
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions