[PATCH v3 3/9] driver core: bus: Move true expression out of if condition in API bus_find_device()

Zijun Hu posted 9 patches 1 year ago
There is a newer version of this series
[PATCH v3 3/9] driver core: bus: Move true expression out of if condition in API bus_find_device()
Posted by Zijun Hu 1 year ago
From: Zijun Hu <quic_zijuhu@quicinc.com>

For bus_find_device(), get_device() in the if condition always returns
true, move it to if body to make the API's logic more clearer.

Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
 drivers/base/bus.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 657c93c38b0dc2a2247e5f482fadd3a9376a58e8..73a56f376d3a05962ce0931a2fe8b4d8839157f2 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -402,9 +402,12 @@ struct device *bus_find_device(const struct bus_type *bus,
 
 	klist_iter_init_node(&sp->klist_devices, &i,
 			     (start ? &start->p->knode_bus : NULL));
-	while ((dev = next_device(&i)))
-		if (match(dev, data) && get_device(dev))
+	while ((dev = next_device(&i))) {
+		if (match(dev, data)) {
+			get_device(dev);
 			break;
+		}
+	}
 	klist_iter_exit(&i);
 	subsys_put(sp);
 	return dev;

-- 
2.34.1
Re: [PATCH v3 3/9] driver core: bus: Move true expression out of if condition in API bus_find_device()
Posted by Jonathan Cameron 1 year ago
On Thu, 12 Dec 2024 21:38:39 +0800
Zijun Hu <zijun_hu@icloud.com> wrote:

> From: Zijun Hu <quic_zijuhu@quicinc.com>
> 
> For bus_find_device(), get_device() in the if condition always returns
> true, move it to if body to make the API's logic more clearer.
> 
> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
I agree this is easier to read and the reasoning is sound.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> ---
>  drivers/base/bus.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/base/bus.c b/drivers/base/bus.c
> index 657c93c38b0dc2a2247e5f482fadd3a9376a58e8..73a56f376d3a05962ce0931a2fe8b4d8839157f2 100644
> --- a/drivers/base/bus.c
> +++ b/drivers/base/bus.c
> @@ -402,9 +402,12 @@ struct device *bus_find_device(const struct bus_type *bus,
>  
>  	klist_iter_init_node(&sp->klist_devices, &i,
>  			     (start ? &start->p->knode_bus : NULL));
> -	while ((dev = next_device(&i)))
> -		if (match(dev, data) && get_device(dev))
> +	while ((dev = next_device(&i))) {
> +		if (match(dev, data)) {
> +			get_device(dev);
>  			break;
> +		}
> +	}
>  	klist_iter_exit(&i);
>  	subsys_put(sp);
>  	return dev;
>