driver core: Move true expression out of if condition in 3 device finding APIs
authorZijun Hu <quic_zijuhu@quicinc.com>
Sun, 5 Jan 2025 08:34:04 +0000 (16:34 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 10 Jan 2025 14:26:12 +0000 (15:26 +0100)
For bus_find_device(), driver_find_device(), and device_find_child(), all
of their function body have pattern below:

{
struct klist_iter i;
struct device *dev;

...
while ((dev = next_device(&i)))
if (match(dev, data) && get_device(dev))
break;
...
}

The expression 'get_device(dev)' in the if condition always returns true
since @dev != NULL.

Move the expression to if body to make logic of these APIs more clearer.

Reviewed-by: Fan Ni <fan.ni@samsung.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
Link: https://lore.kernel.org/r/20250105-class_fix-v6-3-3a2f1768d4d4@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/base/bus.c
drivers/base/core.c
drivers/base/driver.c

index 657c93c38b0dc2a2247e5f482fadd3a9376a58e8..73a56f376d3a05962ce0931a2fe8b4d8839157f2 100644 (file)
@@ -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;
index d4c20e9b23da71e9afb11108cf4353ed1b47f591..a83a1350fb5b2baa5e4ee0f5e5805a5bee536ec7 100644 (file)
@@ -4089,9 +4089,12 @@ struct device *device_find_child(struct device *parent, const void *data,
                return NULL;
 
        klist_iter_init(&parent->p->klist_children, &i);
-       while ((child = next_device(&i)))
-               if (match(child, data) && get_device(child))
+       while ((child = next_device(&i))) {
+               if (match(child, data)) {
+                       get_device(child);
                        break;
+               }
+       }
        klist_iter_exit(&i);
        return child;
 }
index b4eb5b89c4ee7bc35458fc75730b16a6d1e804d3..6f033a741aa7ce6138d1c61e49e72b2a3eb85e06 100644 (file)
@@ -160,9 +160,12 @@ struct device *driver_find_device(const struct device_driver *drv,
 
        klist_iter_init_node(&drv->p->klist_devices, &i,
                             (start ? &start->p->knode_driver : 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);
        return dev;
 }