Skip to content

New readability check: OkWithoutErrorReturnValue #1227

@blagh

Description

@blagh

The overall idea is a new Readability check when a function returns the {:ok, value} version of a result tuple, but never returns the {:error, error} version.

As a dead-simple example:

def my_method do
  {:ok, "value"} 
end

I don't see what value returning an :ok tuple brings to this case, excepting perhaps the possibility of future changes. But in my experience, this mainly serves to make it harder to trace down where an :error clause might be coming from, especially if it's stacked into a with statement.

A couple of cases that I can think of that should not generate a warning:

  • returning a bare :ok in cases where it doesn't make sense to return some other kind of value
  • implementing a protocol or behaviour that requires a tuple return value

As a follow-up or alternative, I'd also put forth that there should be a style warning when a clause of a with statement is using pattern-matching when there isn't actually any pattern-matching happening.

But it's possible that I'm missing some nuances (perhaps this would be too difficult to do via static analysis, unless a typespec is available), so please consider this an idea rather than a demand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions