Skip to contentSkip to conversation

w1: Replace usage of found with dedicated list iterator variable

PATCH Applied Jakob Koschel 2 participants 2 messages May 12, 2023 – May 13, 2023 W1 dallas's 1-wire bus

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 w1: Replace usage of found with dedicated list iterator variable

To move the list iterator variable into the list_for_each_entry_*() macro in the future it should be avoided to use the list iterator variable after the loop body.

To *never* use the list iterator variable after the loop it was concluded to use a separate iterator variable instead of a found boolean [1].

This removes the need to use a found variable and simply checking if the variable was set, can determine if the break/goto was hit.

---
Changes in v2:
- fix checkpatch intentation issues

Changes in v3:
- actually fix checkpatch intentation issues

Note: I've changed my email address to contributions to [email protected], I hope that's not an issue since the v1 was still on my old email.

---
 drivers/w1/w1.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
drivers/w1/w1.c +15 -17
index 9d199fed9628..a5e18de7a25a 100644
@@ -830,49 +830,47 @@ int w1_slave_detach(struct w1_slave *sl)
 
 struct w1_master *w1_search_master_id(u32 id)
 {
-	struct w1_master *dev;
-	int found = 0;
+	struct w1_master *dev = NULL, *iter;
 
 	mutex_lock(&w1_mlock);
-	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
-		if (dev->id == id) {
-			found = 1;
-			atomic_inc(&dev->refcnt);
+	list_for_each_entry(iter, &w1_masters, w1_master_entry) {
+		if (iter->id == id) {
+			dev = iter;
+			atomic_inc(&iter->refcnt);
 			break;
 		}
 	}
 	mutex_unlock(&w1_mlock);
 
-	return (found)?dev:NULL;
+	return dev;
 }
 
 struct w1_slave *w1_search_slave(struct w1_reg_num *id)
 {
 	struct w1_master *dev;
-	struct w1_slave *sl = NULL;
-	int found = 0;
+	struct w1_slave *sl = NULL, *iter;
 
 	mutex_lock(&w1_mlock);
 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
 		mutex_lock(&dev->list_mutex);
-		list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
-			if (sl->reg_num.family == id->family &&
-					sl->reg_num.id == id->id &&
-					sl->reg_num.crc == id->crc) {
-				found = 1;
+		list_for_each_entry(iter, &dev->slist, w1_slave_entry) {
+			if (iter->reg_num.family == id->family &&
+			    iter->reg_num.id == id->id &&
+			    iter->reg_num.crc == id->crc) {
+				sl = iter;
 				atomic_inc(&dev->refcnt);
-				atomic_inc(&sl->refcnt);
+				atomic_inc(&iter->refcnt);
 				break;
 			}
 		}
 		mutex_unlock(&dev->list_mutex);
 
-		if (found)
+		if (sl)
 			break;
 	}
 	mutex_unlock(&w1_mlock);
 
-	return (found)?sl:NULL;
+	return sl;
 }
 
 void w1_reconnect_slaves(struct w1_family *f, int attach)

---
base-commit: cc3c44c9fda264c6d401be04e95449a57c1231c6
change-id: 20230509-w1-replace-usage-of-found-with-tmp-list-iterator-variable-c56393ff0339

Best regards,

Signature · 1 line
Jakob Koschel <[email protected]>
#1 lore raw
Quoted 9 lines · Jakob Koschel

To move the list iterator variable into the list_for_each_entry_*() macro in the future it should be avoided to use the list iterator variable after the loop body.

To *never* use the list iterator variable after the loop it was concluded to use a separate iterator variable instead of a found boolean [1].

[...]

Applied, thanks!

[1/1] w1: Replace usage of found with dedicated list iterator variable

      https://git.kernel.org/krzk/linux-w1/c/b332af5398a3aa1a2fdd69bb6968a8f866cc39aa

Best regards,

Signature · 1 line
Krzysztof Kozlowski <[email protected]>