Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Needless / unnecessary Result #6265

Closed
emilk opened this issue Oct 29, 2020 · 3 comments
Closed

Needless / unnecessary Result #6265

emilk opened this issue Oct 29, 2020 · 3 comments
Labels
A-lint Area: New lints

Comments

@emilk
Copy link

emilk commented Oct 29, 2020

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
}
@emilk emilk added the A-lint Area: New lints label Oct 29, 2020
@llogiq
Copy link
Contributor

llogiq commented Oct 29, 2020

Let's only lint this on non-exported fns, as public items are part of the API and we don't want to have clippy suggest breaking changes.

Linting this needs to check for any possible exit point (returnor ?) out of the function, and the return type.

@matthiaskrgr
Copy link
Member

Duplicate of #5969

@matthiaskrgr matthiaskrgr marked this as a duplicate of #5969 Oct 29, 2020
@emilk
Copy link
Author

emilk commented Oct 29, 2020

@matthiaskrgr thanks - I searched for "unnecessary result" and "needless result", but not "unneeded result" 🤦‍♂️

Anyway, very happy to see this being worked on in #6070 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants