Skip to content

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

Closed
@matthiaskrgr

Description

@matthiaskrgr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-complexityLint: Belongs in the complexity lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions