[LTP] [PATCH 3/5] API/cgroup: Lift out assignments in if statements

Richard Palethorpe rpalethorpe@suse.com
Tue Dec 14 11:36:43 CET 2021


checkpatch.pl forbids this; for good reason because it can mask
accidental use of '=' instead of '=='. Also included is a script to
automatically fix occurrences of this.

Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
---
 lib/tst_cgroup.c                           | 22 ++++++++++------
 scripts/coccinelle/fix-if-assignment.cocci | 30 ++++++++++++++++++++++
 2 files changed, 44 insertions(+), 8 deletions(-)
 create mode 100644 scripts/coccinelle/fix-if-assignment.cocci

diff --git a/lib/tst_cgroup.c b/lib/tst_cgroup.c
index d9d74faa8..8eeb98a26 100644
--- a/lib/tst_cgroup.c
+++ b/lib/tst_cgroup.c
@@ -370,7 +370,8 @@ static void cgroup_root_scan(const char *const mnt_type,
 	SAFE_FILE_READAT(mnt_dfd, "cgroup.controllers", buf, sizeof(buf));
 
 	for (tok = strtok(buf, " "); tok; tok = strtok(NULL, " ")) {
-		if ((const_ctrl = cgroup_find_ctrl(tok)))
+		const_ctrl = cgroup_find_ctrl(tok);
+		if (const_ctrl)
 			add_ctrl(&ctrl_field, const_ctrl);
 	}
 
@@ -386,7 +387,8 @@ static void cgroup_root_scan(const char *const mnt_type,
 
 v1:
 	for (tok = strtok(mnt_opts, ","); tok; tok = strtok(NULL, ",")) {
-		if ((const_ctrl = cgroup_find_ctrl(tok)))
+		const_ctrl = cgroup_find_ctrl(tok);
+		if (const_ctrl)
 			add_ctrl(&ctrl_field, const_ctrl);
 
 		no_prefix |= !strcmp("noprefix", tok);
@@ -1008,8 +1010,9 @@ int safe_cgroup_has(const char *const file, const int lineno,
 		return 0;
 
 	for_each_dir(cg, cfile->ctrl_indx, dir) {
-		if (!(alias = cgroup_file_alias(cfile, *dir)))
-		    continue;
+		alias = cgroup_file_alias(cfile, *dir);
+		if (!alias)
+			continue;
 
 		if (!faccessat((*dir)->dir_fd, alias, F_OK, 0))
 			return 1;
@@ -1077,7 +1080,8 @@ ssize_t safe_cgroup_read(const char *const file, const int lineno,
 	ssize_t read_ret = 0;
 
 	for_each_dir(cg, cfile->ctrl_indx, dir) {
-		if (!(alias = cgroup_file_alias(cfile, *dir)))
+		alias = cgroup_file_alias(cfile, *dir);
+		if (!alias)
 			continue;
 
 		if (prev_len)
@@ -1115,8 +1119,9 @@ void safe_cgroup_printf(const char *const file, const int lineno,
 	va_list va;
 
 	for_each_dir(cg, cfile->ctrl_indx, dir) {
-		if (!(alias = cgroup_file_alias(cfile, *dir)))
-		    continue;
+		alias = cgroup_file_alias(cfile, *dir);
+		if (!alias)
+			continue;
 
 		va_start(va, fmt);
 		safe_file_vprintfat(file, lineno,
@@ -1141,7 +1146,8 @@ void safe_cgroup_scanf(const char *const file, const int lineno,
 		return;
 
 	va_start(va, fmt);
-	if ((ret = vsscanf(buf, fmt, va)) < 1) {
+	ret = vsscanf(buf, fmt, va);
+	if (ret < 1) {
 		tst_brk_(file, lineno, TBROK | TERRNO,
 			 "'%s': vsscanf('%s', '%s', ...)", file_name, buf, fmt);
 	}
diff --git a/scripts/coccinelle/fix-if-assignment.cocci b/scripts/coccinelle/fix-if-assignment.cocci
new file mode 100644
index 000000000..4dad22fe0
--- /dev/null
+++ b/scripts/coccinelle/fix-if-assignment.cocci
@@ -0,0 +1,30 @@
+@@
+expression V, E;
+@@
+
++ V = E;
+  if (
+-	(V = E)
++ 	V
+  ) { ... }
+
+@@
+expression V, E;
+@@
+
++ V = E;
+  if (!
+-	(V = E)
++ 	V
+  ) { ... }
+
+@@
+expression V, E;
+binary operator B; 
+@@
+
++ V = E;
+  if (
+-	(V = E)
++ 	V
+  B ...) { ... }
-- 
2.34.0



More information about the ltp mailing list