Skip to contentSkip to conversation

iio/light/gp2ap020a00f: Use common error handling code in gp2ap020a00f_adjust_lux_mode()

PATCH In review SF Markus Elfring 2 participants 3 messages Oct 26, 2017 – Oct 27, 2017

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

PATCH 1/1 iio/light/gp2ap020a00f: Use common error handling code in gp2ap020a00f_adjust_lux_mode()

From: Markus Elfring <[email protected]>
Date: Thu, 26 Oct 2017 09:50:21 +0200

* Add jump targets so that two error messages are stored only once at the end of this function implementation.

* Adjust condition checks.

This issue was detected by using the Coccinelle software.

---
 drivers/iio/light/gp2ap020a00f.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
drivers/iio/light/gp2ap020a00f.c +17 -20
index 44b13fbcd093..ae29bce660ac 100644
@@ -741,19 +741,13 @@ static bool gp2ap020a00f_adjust_lux_mode(struct gp2ap020a00f_data *data,
 		 */
 		err =  gp2ap020a00f_write_event_threshold(data,
 					GP2AP020A00F_THRESH_TH, false);
-		if (err < 0) {
-			dev_err(&data->client->dev,
-				"Clearing als threshold register failed.\n");
-			return false;
-		}
+		if (err)
+			goto report_clearing_failure;
 
 		err =  gp2ap020a00f_write_event_threshold(data,
 					GP2AP020A00F_THRESH_TL, false);
-		if (err < 0) {
-			dev_err(&data->client->dev,
-				"Clearing als threshold register failed.\n");
-			return false;
-		}
+		if (err)
+			goto report_clearing_failure;
 
 		/* Change lux mode */
 		err = regmap_update_bits(data->regmap,
@@ -793,27 +787,30 @@ static bool gp2ap020a00f_adjust_lux_mode(struct gp2ap020a00f_data *data,
 		if (test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV, &data->flags)) {
 			err =  gp2ap020a00f_write_event_threshold(data,
 					GP2AP020A00F_THRESH_TH, true);
-			if (err < 0) {
-				dev_err(&data->client->dev,
-				"Adjusting als threshold value failed.\n");
-				return false;
-			}
+			if (err)
+				goto report_adjustment_failure;
 		}
 
 		if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV, &data->flags)) {
 			err =  gp2ap020a00f_write_event_threshold(data,
 					GP2AP020A00F_THRESH_TL, true);
-			if (err < 0) {
-				dev_err(&data->client->dev,
-				"Adjusting als threshold value failed.\n");
-				return false;
-			}
+			if (err)
+				goto report_adjustment_failure;
 		}
 
 		return true;
 	}
 
 	return false;
+
+report_clearing_failure:
+	dev_err(&data->client->dev,
+		"Clearing als threshold register failed.\n");
+	return false;
+
+report_adjustment_failure:
+	dev_err(&data->client->dev, "Adjusting als threshold value failed.\n");
+	return false;
 }
 
 static void gp2ap020a00f_output_to_lux(struct gp2ap020a00f_data *data,
Signature · 1 line
2.14.3
maintainer
#1 lore raw
Quoted 11 lines · On Thu, 26 Oct 2017 10:00:21 +0200 SF Markus Elfring…

From: Markus Elfring <[email protected]>
Date: Thu, 26 Oct 2017 09:50:21 +0200

* Add jump targets so that two error messages are stored only once at the end of this function implementation.

* Adjust condition checks.

This issue was detected by using the Coccinelle software.

Same issue - readability is worse than without the patch so it is always going to be rejected.

Hence I am not going to take this or anything similar, and I doubt anyone else in the kernel will either...

Jonathan

Quoted 73 lines
---
 drivers/iio/light/gp2ap020a00f.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
drivers/iio/light/gp2ap020a00f.c +17 -20
index 44b13fbcd093..ae29bce660ac 100644
@@ -741,19 +741,13 @@ static bool gp2ap020a00f_adjust_lux_mode(struct gp2ap020a00f_data *data,
 		 */
 		err =  gp2ap020a00f_write_event_threshold(data,
 					GP2AP020A00F_THRESH_TH, false);
-		if (err < 0) {
-			dev_err(&data->client->dev,
-				"Clearing als threshold register failed.\n");
-			return false;
-		}
+		if (err)
+			goto report_clearing_failure;
 
 		err =  gp2ap020a00f_write_event_threshold(data,
 					GP2AP020A00F_THRESH_TL, false);
-		if (err < 0) {
-			dev_err(&data->client->dev,
-				"Clearing als threshold register failed.\n");
-			return false;
-		}
+		if (err)
+			goto report_clearing_failure;
 
 		/* Change lux mode */
 		err = regmap_update_bits(data->regmap,
@@ -793,27 +787,30 @@ static bool gp2ap020a00f_adjust_lux_mode(struct gp2ap020a00f_data *data,
 		if (test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV, &data->flags)) {
 			err =  gp2ap020a00f_write_event_threshold(data,
 					GP2AP020A00F_THRESH_TH, true);
-			if (err < 0) {
-				dev_err(&data->client->dev,
-				"Adjusting als threshold value failed.\n");
-				return false;
-			}
+			if (err)
+				goto report_adjustment_failure;
 		}
 
 		if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV, &data->flags)) {
 			err =  gp2ap020a00f_write_event_threshold(data,
 					GP2AP020A00F_THRESH_TL, true);
-			if (err < 0) {
-				dev_err(&data->client->dev,
-				"Adjusting als threshold value failed.\n");
-				return false;
-			}
+			if (err)
+				goto report_adjustment_failure;
 		}
 
 		return true;
 	}
 
 	return false;
+
+report_clearing_failure:
+	dev_err(&data->client->dev,
+		"Clearing als threshold register failed.\n");
+	return false;
+
+report_adjustment_failure:
+	dev_err(&data->client->dev, "Adjusting als threshold value failed.\n");
+	return false;
 }
 
 static void gp2ap020a00f_output_to_lux(struct gp2ap020a00f_data *data,
#2 lore raw
Quoted 1 line

Same issue - readability is worse

An alternative opinion triggered the development also of this update suggestion.

Quoted 1 line

than without the patch so it is always going to be rejected.

Corresponding clarification might be needed if the change (in a direction I propose here) will become desirable.
Can it make sense to concentrate a bit of error reporting at the end of such a function?

Regards,
Markus