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

new lint: functions that return unneeded options/results #5969

Closed
matthiaskrgr opened this issue Aug 27, 2020 · 1 comment · Fixed by #6070
Closed

new lint: functions that return unneeded options/results #5969

matthiaskrgr opened this issue Aug 27, 2020 · 1 comment · Fixed by #6070
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-complexity Lint: Belongs in the complexity lint group

Comments

@matthiaskrgr
Copy link
Member

What it does

I wonder if it makes sense to have a lint that checks for functions that wrap their return types in Option or Result unnecessarily i.e. functions that always return Ok() or Some().

This could indicate there once was some refactoring which removed the Err() or None case and the function can be simplified to directly return the wrapped type instead of the Option/Result.

Categories (optional)

This could be complexity or pedantic lint.

What is the advantage of the recommended code over the original code

fn get_cool_number(a: bool, b: bool) -> Option<i32> {
    if a && b {
        return Some(42);
    }
    if a {
        Some(0)
    } else if b {
        Some(1)
    } else {
        Some(1337)
    }
}

could would be warned about and could be refactored to

fn get_cool_number(a: bool, b: bool) -> i32 {
    if a && b {
        return 42;
    }
    if a {
        0
    } else if b {
        1
    } else {
        1337
    }
}

Drawbacks

This would probably "push" option-wrapping into other functions which might be annoying, i.e.

let x = get_cool_number();
foo(x)

could become

let x = Some(get_cool_number());
foo(x)

if foo requires an Option

The lint should not probably not apply to pub fns as it suggests changing function signatures.

@matthiaskrgr matthiaskrgr added the A-lint Area: New lints label Aug 27, 2020
@flip1995 flip1995 added L-complexity Lint: Belongs in the complexity lint group E-medium Call for participation: Medium difficulty level problem and requires some initial experience. labels Aug 31, 2020
@hkmatsumoto
Copy link
Member

I'd love to work on this.

bors added a commit that referenced this issue Nov 17, 2020
Add new lint `unnecessary_wrap`

Fixes #5969

changelog: New lint [`unnecessary_wraps`]
bors added a commit that referenced this issue Nov 17, 2020
Add new lint `unnecessary_wrap`

Fixes #5969

changelog: New lint [`unnecessary_wraps`]
@bors bors closed this as completed in 44d9445 Nov 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-complexity Lint: Belongs in the complexity lint group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants