[PATCH] btrfs: Use min() instead of doing it manually

Jiapeng Chong posted 1 patch 4 years, 6 months ago
fs/btrfs/volumes.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] btrfs: Use min() instead of doing it manually
Posted by Jiapeng Chong 4 years, 6 months ago
Eliminate following coccicheck warning:

./fs/btrfs/volumes.c:7768:13-14: WARNING opportunity for min().

Reported-by: Abaci Robot <abaci@linux.alibaba.com>
Signed-off-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
---
 fs/btrfs/volumes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 730355b55b42..dca3f0cedff9 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -7765,7 +7765,7 @@ static int btrfs_device_init_dev_stats(struct btrfs_device *device,
 			btrfs_dev_stat_set(device, i, 0);
 		device->dev_stats_valid = 1;
 		btrfs_release_path(path);
-		return ret < 0 ? ret : 0;
+		return min(ret, 0);
 	}
 	slot = path->slots[0];
 	eb = path->nodes[0];
-- 
2.20.1.7.g153144c

Re: [PATCH] btrfs: Use min() instead of doing it manually
Posted by Qu Wenruo 4 years, 6 months ago

On 2021/12/27 19:34, Jiapeng Chong wrote:
> Eliminate following coccicheck warning:
> 
> ./fs/btrfs/volumes.c:7768:13-14: WARNING opportunity for min().
> 
> Reported-by: Abaci Robot <abaci@linux.alibaba.com>
> Signed-off-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
> ---
>   fs/btrfs/volumes.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index 730355b55b42..dca3f0cedff9 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -7765,7 +7765,7 @@ static int btrfs_device_init_dev_stats(struct btrfs_device *device,
>   			btrfs_dev_stat_set(device, i, 0);
>   		device->dev_stats_valid = 1;
>   		btrfs_release_path(path);
> -		return ret < 0 ? ret : 0;
> +		return min(ret, 0);

Nope, please don't blindly follow whatever the static checker reports, 
but spend sometime on the code.

In this particular case, min(ret, 0) is not really making the code any 
easier to read.

The "if (ret)" branch means, either we got a critical error (ret < 0) or 
we didn't find the dev status item

For no dev status item case, it's no big deal and we can continue 
returning 0. For fatal error case, it mostly means the device tree is 
corrupted, and we return @ret directly.

Are you really thinking we're calculating a minimal value between 0 and ret?


And I have already stated that, there is no need to CC maintainers.
Especially you didn't even bother to check who is the one pushing the 
code to Linus.

Thanks,
Qu
>   	}
>   	slot = path->slots[0];
>   	eb = path->nodes[0];

Re: [PATCH] btrfs: Use min() instead of doing it manually
Posted by David Sterba 4 years, 5 months ago
On Mon, Dec 27, 2021 at 07:49:01PM +0800, Qu Wenruo wrote:
> 
> 
> On 2021/12/27 19:34, Jiapeng Chong wrote:
> > Eliminate following coccicheck warning:
> > 
> > ./fs/btrfs/volumes.c:7768:13-14: WARNING opportunity for min().
> > 
> > Reported-by: Abaci Robot <abaci@linux.alibaba.com>
> > Signed-off-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
> > ---
> >   fs/btrfs/volumes.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> > index 730355b55b42..dca3f0cedff9 100644
> > --- a/fs/btrfs/volumes.c
> > +++ b/fs/btrfs/volumes.c
> > @@ -7765,7 +7765,7 @@ static int btrfs_device_init_dev_stats(struct btrfs_device *device,
> >   			btrfs_dev_stat_set(device, i, 0);
> >   		device->dev_stats_valid = 1;
> >   		btrfs_release_path(path);
> > -		return ret < 0 ? ret : 0;
> > +		return min(ret, 0);
> 
> Nope, please don't blindly follow whatever the static checker reports, 
> but spend sometime on the code.
> 
> In this particular case, min(ret, 0) is not really making the code any 
> easier to read.
> 
> The "if (ret)" branch means, either we got a critical error (ret < 0) or 
> we didn't find the dev status item
> 
> For no dev status item case, it's no big deal and we can continue 
> returning 0. For fatal error case, it mostly means the device tree is 
> corrupted, and we return @ret directly.
> 
> Are you really thinking we're calculating a minimal value between 0 and ret?

That's probably the most important point. Although the expression could
be equivalent to calculating minimum, the code should read "if there was
an error, return it, otherwise return 0". Using min() for that obscures
that.