credo icon indicating copy to clipboard operation
credo copied to clipboard

New readability check: OkWithoutErrorReturnValue

Open blagh opened this issue 3 months ago • 1 comments

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.

blagh avatar Nov 29 '25 16:11 blagh

Hi, I think this is a great idea for a check.

As you already hint at, the difficult part could be to trace these cases accurately via static analysis 👍

rrrene avatar Dec 01 '25 14:12 rrrene