Re: checkpatch.pl warning for "return" with value

From: Joe Perches
Date: Fri Apr 17 2020 - 13:34:23 EST


On Fri, 2020-04-17 at 13:20 -0400, Luben Tuikov wrote:
> Hi guys,
>
> I get this warning:
>
> :32: WARNING: else is not generally useful after a break or return
> #32: FILE: drivers/gpu/drm/amd/amdgpu/amdgpu_job.c:55:
> + return 0;
> + } else {
>
> for the following code, at the bottom of a function:
>
> if (amdgpu_device_should_recover_gpu(ring->adev)) {
> amdgpu_device_gpu_recover(ring->adev, job);
> return 0;
> } else {
> drm_sched_suspend_timeout(&ring->sched);
> return 1;
> }
> }
>
> It seems like a false positive--I mean, if the else branch was
> taken, we'd return a different result.

There is an existing checkpatch exception for single line
if/else returns
like:

if (foo)
return bar;
else
return baz;

because that's a pretty common code style.

But I personally don't think that your example fits the
same style.

I think when unexpected condition should be separated from
the expected condition which should typically be the last
block of a function like:


if (<atypical_condition>) {
...;
return <atypical_result>;
}

...;
return <typical_result>;
}

If you want to code it, and it works, go ahead, but I
won't attempt it because I think it's not appropriate.

cheers, Joe