Skip to contentSkip to conversation

Remove unreachable code and cleanup code with fixed return

PATCH In review Andrew Goodbody 2 participants 4 messages Jul 16, 2025 – Jul 24, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover Remove unreachable code and cleanup code with fixed return

Smatch reported unreachable code in blk-uclass.c which led to noticing that uclass_find_next_device always returned 0 which meant it had no need of any return value.

---
Changes in v3:
- Address review comments to make more readable and prevent for loop testing of a variable that will not change
- Remove those for loops that reduce to being empty
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- make uclass_find_next_device a void and update callers
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (2):
block: Remove unreachable code
uclass: Cleanup uclass_find_next_device

 board/emulation/qemu-ppce500/qemu-ppce500.c |   4 +-
 boot/bootdev-uclass.c                       |   8 +-
 cmd/regulator.c                             |   8 +-
 drivers/block/blk-uclass.c                  |  14 +--
 drivers/core/uclass.c                       |  16 +---
 drivers/power/regulator/regulator-uclass.c  |  21 ++--
 drivers/remoteproc/rproc-uclass.c           |   9 +-
 include/dm/uclass-internal.h                |   4 +-
 test/dm/core.c                              | 143 +++++++++++-----------------
 9 files changed, 94 insertions(+), 133 deletions(-)

---
base-commit: cd79a2839b01571b6f2915c219f71c05e3c97482
change-id: 20250714-blk-uclass-34684c01f60c

Best regards,

Signature · 1 line
Andrew Goodbody <[email protected]>
#1 lore raw

PATCH v3 1/2 block: Remove unreachable code

The two functions blk_find_first and blk_find_next use a for loop with the content being a 'return 0' which means that the 'increment' code is unreachable so remove it and also remove the variable ret which is assigned to but its value is never used.

This issue found by Smatch.

---
 drivers/block/blk-uclass.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/block/blk-uclass.c +4 -10
index f3ac8db9464..b38c21ffbe2 100644
@@ -613,11 +613,8 @@ static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags)
 
 int blk_find_first(enum blk_flag_t flags, struct udevice **devp)
 {
-	int ret;
-
-	for (ret = uclass_find_first_device(UCLASS_BLK, devp);
-	     *devp && !blk_flags_check(*devp, flags);
-	     ret = uclass_find_next_device(devp))
+	for (uclass_find_first_device(UCLASS_BLK, devp);
+	     *devp && !blk_flags_check(*devp, flags);)
 		return 0;
 
 	return -ENODEV;
@@ -625,11 +622,8 @@ int blk_find_first(enum blk_flag_t flags, struct udevice **devp)
 
 int blk_find_next(enum blk_flag_t flags, struct udevice **devp)
 {
-	int ret;
-
-	for (ret = uclass_find_next_device(devp);
-	     *devp && !blk_flags_check(*devp, flags);
-	     ret = uclass_find_next_device(devp))
+	for (uclass_find_next_device(devp);
+	     *devp && !blk_flags_check(*devp, flags);)
 		return 0;
 
 	return -ENODEV;
Signature · 1 line
2.39.5
#2 lore raw

PATCH v3 2/2 uclass: Cleanup uclass_find_next_device

In reply to Andrew Goodbody

uclass_find_next_device always returns 0, so instead make it a void and update calling sites.

---
 board/emulation/qemu-ppce500/qemu-ppce500.c |   4 +-
 boot/bootdev-uclass.c                       |   8 +-
 cmd/regulator.c                             |   8 +-
 drivers/core/uclass.c                       |  16 +---
 drivers/power/regulator/regulator-uclass.c  |  21 ++--
 drivers/remoteproc/rproc-uclass.c           |   9 +-
 include/dm/uclass-internal.h                |   4 +-
 test/dm/core.c                              | 143 +++++++++++-----------------
 8 files changed, 90 insertions(+), 123 deletions(-)
board/emulation/qemu-ppce500/qemu-ppce500.c +2 -2
index 40d295dbf06..58de4a05296 100644
@@ -170,9 +170,9 @@ int misc_init_r(void)
 	 * Detect the presence of the platform bus node, and
 	 * create a virtual memory mapping for it.
 	 */
-	for (ret = uclass_find_first_device(UCLASS_SIMPLE_BUS, &dev);
+	for (uclass_find_first_device(UCLASS_SIMPLE_BUS, &dev);
 	     dev;
-	     ret = uclass_find_next_device(&dev)) {
+	     uclass_find_next_device(&dev)) {
 		if (device_is_compatible(dev, "qemu,platform")) {
 			struct simple_bus_plat *plat = dev_get_uclass_plat(dev);
 
boot/bootdev-uclass.c +5 -3
index 3791ebfcb42..3f8dc2c3c4e 100644
@@ -213,10 +213,12 @@ void bootdev_list(bool probe)
 		       device_active(dev) ? '+' : ' ',
 		       ret ? simple_itoa(-ret) : "OK",
 		       dev_get_uclass_name(dev_get_parent(dev)), dev->name);
-		if (probe)
+		if (probe) {
 			ret = uclass_next_device_check(&dev);
-		else
-			ret = uclass_find_next_device(&dev);
+		} else {
+			uclass_find_next_device(&dev);
+			ret = 0;
+		}
 	}
 	printf("---  ------  ------  --------  ------------------\n");
 	printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
cmd/regulator.c +4 -4
index da298090bb7..8d743c8d269 100644
@@ -96,11 +96,11 @@ static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
 	       LIMIT_OFNAME, LIMIT_OFNAME, "regulator-name",
 	       "Parent");
 
-	for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
-	     ret = uclass_find_next_device(&dev)) {
-		if (ret)
-			continue;
+	ret = uclass_find_first_device(UCLASS_REGULATOR, &dev);
+	if (ret)
+		return ret;
 
+	for (; dev; uclass_find_next_device(&dev)) {
 		uc_pdata = dev_get_uclass_plat(dev);
 		printf("| %-*.*s| %-*.*s| %s\n",
 		       LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
drivers/core/uclass.c +5 -11
index ce5e61bbaa6..5365ac68f9e 100644
@@ -261,17 +261,14 @@ int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
 	return 0;
 }
 
-int uclass_find_next_device(struct udevice **devp)
+void uclass_find_next_device(struct udevice **devp)
 {
 	struct udevice *dev = *devp;
 
 	*devp = NULL;
-	if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
-		return 0;
-
-	*devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
-
-	return 0;
+	if (!list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
+		*devp = list_entry(dev->uclass_node.next, struct udevice,
+				   uclass_node);
 }
 
 int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len,
@@ -675,11 +672,8 @@ int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
 
 int uclass_next_device_check(struct udevice **devp)
 {
-	int ret;
+	uclass_find_next_device(devp);
 
-	ret = uclass_find_next_device(devp);
-	if (ret)
-		return ret;
 	if (!*devp)
 		return 0;
 
drivers/power/regulator/regulator-uclass.c +12 -9
index 09567eb9dbb..2a59a1b79c2 100644
@@ -260,13 +260,13 @@ int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
 
 	*devp = NULL;
 
-	for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
-	     ret = uclass_find_next_device(&dev)) {
-		if (ret) {
-			dev_dbg(dev, "ret=%d\n", ret);
-			continue;
-		}
+	ret = uclass_find_first_device(UCLASS_REGULATOR, &dev);
+	if (ret) {
+		dev_dbg(dev, "ret=%d\n", ret);
+		return ret;
+	}
 
+	for (; dev; uclass_find_next_device(&dev)) {
 		uc_pdata = dev_get_uclass_plat(dev);
 		if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
 			continue;
@@ -410,9 +410,12 @@ static bool regulator_name_is_unique(struct udevice *check_dev,
 	int ret;
 	int len;
 
-	for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
-	     ret = uclass_find_next_device(&dev)) {
-		if (ret || dev == check_dev)
+	ret = uclass_find_first_device(UCLASS_REGULATOR, &dev);
+	if (ret)
+		return true;
+
+	for (; dev; uclass_find_next_device(&dev)) {
+		if (dev == check_dev)
 			continue;
 
 		uc_pdata = dev_get_uclass_plat(dev);
drivers/remoteproc/rproc-uclass.c +6 -3
index 3233ff80419..2dbd3a21cea 100644
@@ -55,9 +55,12 @@ static int for_each_remoteproc_device(int (*fn) (struct udevice *dev,
 	struct dm_rproc_uclass_pdata *uc_pdata;
 	int ret;
 
-	for (ret = uclass_find_first_device(UCLASS_REMOTEPROC, &dev); dev;
-	     ret = uclass_find_next_device(&dev)) {
-		if (ret || dev == skip_dev)
+	ret = uclass_find_first_device(UCLASS_REMOTEPROC, &dev);
+	if (ret)
+		return ret;
+
+	for (; dev; uclass_find_next_device(&dev)) {
+		if (dev == skip_dev)
 			continue;
 		uc_pdata = dev_get_uclass_plat(dev);
 		ret = fn(dev, uc_pdata, data);
include/dm/uclass-internal.h +1 -3
index 3ddcdd21439..9cb3f090271 100644
@@ -149,10 +149,8 @@ int uclass_find_first_device(enum uclass_id id, struct udevice **devp);
  *
  * The device is not prepared for use - this is an internal function.
  * The function uclass_get_device_tail() can be used to probe the device.
- *
- * Return: 0 if OK (found or not found), -ve on error
  */
-int uclass_find_next_device(struct udevice **devp);
+void uclass_find_next_device(struct udevice **devp);
 
 /**
  * uclass_find_device_by_namelen() - Find uclass device based on ID and name
test/dm/core.c +55 -88

215 lines not on this page · raw message

Signature · 1 line
2.39.5
#3 lore raw

Re: (subset) [PATCH v3 0/2] Remove unreachable code and cleanup code with fixed return

In reply to Andrew Goodbody
Quoted 5 lines · Andrew Goodbody

Smatch reported unreachable code in blk-uclass.c which led to noticing that uclass_find_next_device always returned 0 which meant it had no need of any return value.

Applied to u-boot/master, thanks!

[2/2] uclass: Cleanup uclass_find_next_device

      commit: 70bf6e33e15e2ce56f6e3e9a3f725136c9c6f486
Signature · 1 line
Tom