Skip to contentSkip to conversation

iio: addac: ad74115: remove redundant if statement

PATCH In review Colin Ian King 3 participants 4 messages Mar 28, 2024

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

PATCH 1/1 iio: addac: ad74115: remove redundant if statement

The if statement is redundant because the variable i being assigned in the statement is never read afterwards. Remove it.

Cleans up clang scan build warning:
drivers/iio/addac/ad74115.c:570:3: warning: Value stored to 'i' is never read [deadcode.DeadStores]

---
 drivers/iio/addac/ad74115.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/iio/addac/ad74115.c +0 -3
index e6bc5eb3788d..d31d4adb017e 100644
@@ -566,9 +566,6 @@ static int ad74115_set_comp_debounce(struct ad74115_state *st, unsigned int val)
 		if (val <= ad74115_debounce_tbl[i])
 			break;
 
-	if (i == len)
-		i = len - 1;
-
 	return regmap_update_bits(st->regmap, AD74115_DIN_CONFIG1_REG,
 				  AD74115_DIN_DEBOUNCE_MASK,
 				  FIELD_PREP(AD74115_DIN_DEBOUNCE_MASK, val));
Signature · 1 line
2.39.2
#1 lore raw

Hi Colin,

Thanks for your patch...

Quoted 21 lines · Colin Ian King

The if statement is redundant because the variable i being assigned in the statement is never read afterwards. Remove it.

Cleans up clang scan build warning:
drivers/iio/addac/ad74115.c:570:3: warning: Value stored to 'i' is never read [deadcode.DeadStores]

---
 drivers/iio/addac/ad74115.c | 3 ---

 1 file changed, 3 deletions(-)
drivers/iio/addac/ad74115.c +0 -0
index e6bc5eb3788d..d31d4adb017e 100644
@@ -566,9 +566,6 @@ static int ad74115_set_comp_debounce(struct ad74115_state *st,
 unsigned int val)
  		if (val <= ad74115_debounce_tbl[i])
  			break;

drivers/iio/addac/ad74115.c static int ad74115_set_comp_debounce(struct ad74115_state *st, unsigned int val) · line 571

Quoted 3 lines
drivers/iio/addac/ad74115.c +0 -3
-	if (i == len)
-		i = len - 1;
-

Hmm, this change is clearly good but I think we're actually missing the proper fix in here. I'm being lazy and not checking the datasheet and Cosmin can further comment. But I'm fairly sure that the intent of the code is actually to use i in the call to regmap_update_bits(). I mean if we look at the mask AD74115_DIN_DEBOUNCE_MASK and the possible values of val...

- Nuno Sá

#2 lore raw
Quoted 27 lines · Nuno Sá

Hi Colin,

Thanks for your patch...

Quoted 22 lines · Colin Ian King

The if statement is redundant because the variable i being assigned in the statement is never read afterwards. Remove it.

Cleans up clang scan build warning:
drivers/iio/addac/ad74115.c:570:3: warning: Value stored to 'i' is never read [deadcode.DeadStores]

---
 drivers/iio/addac/ad74115.c | 3 ---

 1 file changed, 3 deletions(-)
drivers/iio/addac/ad74115.c +0 -0
index e6bc5eb3788d..d31d4adb017e 100644
@@ -566,9 +566,6 @@ static int ad74115_set_comp_debounce(struct ad74115_state *st,
 unsigned int val)
  		if (val <= ad74115_debounce_tbl[i])
  			break;

drivers/iio/addac/ad74115.c +0 -1
-	if (i == len)
Quoted 8 lines

drivers/iio/addac/ad74115.c

Quoted 2 lines
drivers/iio/addac/ad74115.c +0 -2
-		i = len - 1;
-

Hmm, this change is clearly good but I think we're actually missing the proper fix in here. I'm being lazy and not checking the datasheet and Cosmin can further comment. But I'm fairly sure that the intent of the code is actually to use i in the call to regmap_update_bits(). I mean if we look at the mask AD74115_DIN_DEBOUNCE_MASK and the possible values of val...

Good eye. I haven't looked at the datasheet either, but logically I would think you go until you hit something that is too high and then step back to the valid valies. In other words the <= should be < and the i = len - 1 should just be i--.

(The first element in the ad74115_debounce_tbl[] array is zero btw, so it's never going to break with i == 0).

regards,
dan carpenter

drivers/iio/addac/ad74115.c +4 -7
index e6bc5eb3788d..8d484cefe5ff 100644
@@ -559,19 +559,16 @@ static void ad74115_gpio_set(struct gpio_chip *gc, unsigned int offset, int valu
 
 static int ad74115_set_comp_debounce(struct ad74115_state *st, unsigned int val)
 {
-	unsigned int len = ARRAY_SIZE(ad74115_debounce_tbl);
 	unsigned int i;
 
-	for (i = 0; i < len; i++)
-		if (val <= ad74115_debounce_tbl[i])
+	for (i = 0; i < ARRAY_SIZE(ad74115_debounce_tbl); i++)
+		if (ad74115_debounce_tbl[i] > val)
 			break;
-
-	if (i == len)
-		i = len - 1;
+	i--;
 
 	return regmap_update_bits(st->regmap, AD74115_DIN_CONFIG1_REG,
 				  AD74115_DIN_DEBOUNCE_MASK,
-				  FIELD_PREP(AD74115_DIN_DEBOUNCE_MASK, val));
+				  FIELD_PREP(AD74115_DIN_DEBOUNCE_MASK, ad74115_debounce_tbl[i]));
 }
 
 static int ad74115_comp_gpio_get_direction(struct gpio_chip *chip,
#3 lore raw
Quoted 42 lines · Dan Carpenter
Quoted 32 lines · Nuno Sá

Hi Colin,

Thanks for your patch...

Quoted 22 lines · Colin Ian King

The if statement is redundant because the variable i being assigned in the statement is never read afterwards. Remove it.

Cleans up clang scan build warning:
drivers/iio/addac/ad74115.c:570:3: warning: Value stored to 'i' is never read [deadcode.DeadStores]

---
 drivers/iio/addac/ad74115.c | 3 ---

 1 file changed, 3 deletions(-)
drivers/iio/addac/ad74115.c +0 -0
index e6bc5eb3788d..d31d4adb017e 100644
@@ -566,9 +566,6 @@ static int ad74115_set_comp_debounce(struct ad74115_state
 *st,
 unsigned int val)
  		if (val <= ad74115_debounce_tbl[i])
  			break;

drivers/iio/addac/ad74115.c static void ad74115_gpio_set(struct gpio_chip *gc, unsigned int offset, int valu

Quoted 3 lines
drivers/iio/addac/ad74115.c +0 -3
-	if (i == len)
-		i = len - 1;
-

Hmm, this change is clearly good but I think we're actually missing the proper

Quoted 8 lines

fix in
here. I'm being lazy and not checking the datasheet and Cosmin can further comment.
But I'm fairly sure that the intent of the code is actually to use i in the call to
regmap_update_bits(). I mean if we look at the mask AD74115_DIN_DEBOUNCE_MASK and the
possible values of val...

Quoted 4 lines

Good eye.  I haven't looked at the datasheet either, but logically I would think you go until you hit something that is too high and then step back to the valid valies.  In other words the <= should be < and the i = len - 1 should just be i--.

I'm not sure about the i--. Apparently the intent was to keep the first index with bigger debounce than val and only step back if we go beyond the array but I'll leave that for Cosmin. The most important to me is something else. See below.

Quoted 33 lines

(The first element in the ad74115_debounce_tbl[] array is zero btw, so it's never going to break with i == 0).

regards,
dan carpenter

drivers/iio/addac/ad74115.c +0 -0
index e6bc5eb3788d..8d484cefe5ff 100644
@@ -559,19 +559,16 @@ static void ad74115_gpio_set(struct gpio_chip *gc, unsigned

int offset, int valu

 static int ad74115_set_comp_debounce(struct ad74115_state *st, unsigned int val) {

drivers/iio/addac/ad74115.c +0 -1
-	unsigned int len = ARRAY_SIZE(ad74115_debounce_tbl);

  unsigned int i;

drivers/iio/addac/ad74115.c +2 -2
-	for (i = 0; i < len; i++)
-		if (val <= ad74115_debounce_tbl[i])
+	for (i = 0; i < ARRAY_SIZE(ad74115_debounce_tbl); i++)
+		if (ad74115_debounce_tbl[i] > val)

  break;

drivers/iio/addac/ad74115.c +1 -3
-
-	if (i == len)
-		i = len - 1;
+	i--;


  return regmap_update_bits(st->regmap, AD74115_DIN_CONFIG1_REG, AD74115_DIN_DEBOUNCE_MASK,

drivers/iio/addac/ad74115.c +1 -1
-				  FIELD_PREP(AD74115_DIN_DEBOUNCE_MASK, val));
+				  FIELD_PREP(AD74115_DIN_DEBOUNCE_MASK,
Quoted 1 line

ad74115_debounce_tbl[i]));

In here, I think we want FIELD_PREP(AD74115_DIN_DEBOUNCE_MASK, i). If you look at the mask and the possible values in the array, you see we can easily go off the mask.

- Nuno Sá