Skip to content

New lint: Unnecessarily complex types #4844

Open
@jyn514

Description

@jyn514

It would be nice to lint on return types that are more complex than necessary. Take for example the following function (playground):

fn unnecessary_result() -> Result<usize, ()> {
    Ok(1)
}

The Result here is not necessary, because the function always returns an Ok variant.
I would also like to see a similar lint for fn f() -> Option<T> where f always returns Some.

Note this is not the same as https://rust-lang.github.io/rust-clippy/current/index.html#type_complexity, which is about return types that are hard to read, not return types that are unnecessary.

Possible output for the lint (just making this up here):

warning: unnecessary `Result` in return type
 --> src/lib.rs:1:1
   |
1  |  fn unnecessary_result() -> Result<usize, ()> {
   |                             ^^^^^^^^^^^^^^^^^ help: try removing the `Result` type: `usize`
2  |         Ok(1)
   |         ^^^^^ note: only return value is an `Ok` variant

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-hardCall for participation: This a hard problem and requires more experience or effort to work onL-unnecessaryLint: Warn about unnecessary code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions