Module Name:    src
Committed By:   rillig
Date:           Sat Aug 21 08:18:48 UTC 2021

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_129.c msg_129.exp
        src/usr.bin/xlint/lint1: tree.c

Log Message:
lint: do not warn about '(void)arg' and similar expressions

In the current NetBSD build, 5260 of the 46264 total lint warnings are
about expressions that have a null effect.  Most of these occurrences
follow well-established patterns, which makes the warnings bogus.
Remove these warnings.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/xlint/lint1/msg_129.c \
    src/tests/usr.bin/xlint/lint1/msg_129.exp
cvs rdiff -u -r1.340 -r1.341 src/usr.bin/xlint/lint1/tree.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/lint1/msg_129.c
diff -u src/tests/usr.bin/xlint/lint1/msg_129.c:1.4 src/tests/usr.bin/xlint/lint1/msg_129.c:1.5
--- src/tests/usr.bin/xlint/lint1/msg_129.c:1.4	Sat Aug 21 07:52:07 2021
+++ src/tests/usr.bin/xlint/lint1/msg_129.c	Sat Aug 21 08:18:48 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_129.c,v 1.4 2021/08/21 07:52:07 rillig Exp $	*/
+/*	$NetBSD: msg_129.c,v 1.5 2021/08/21 08:18:48 rillig Exp $	*/
 # 3 "msg_129.c"
 
 // Test for message: expression has null effect [129]
@@ -51,8 +51,6 @@ legitimate_use_cases(int arg)
 	 * This expression is commonly used to mark the argument as
 	 * deliberately unused.
 	 */
-	/* TODO: remove this bogus warning. */
-	/* expect+1: warning: expression has null effect [129] */
 	(void)arg;
 
 	/*
@@ -62,16 +60,20 @@ legitimate_use_cases(int arg)
 	 * variants of the code, such as in debugging mode, and writing down
 	 * the exact conditions would complicate the code unnecessarily.
 	 */
-	/* TODO: remove this bogus warning. */
-	/* expect+1: warning: expression has null effect [129] */
 	(void)local;
 
 	/* This is a short-hand notation for a do-nothing command. */
-	/* TODO: remove this bogus warning. */
-	/* expect+1: warning: expression has null effect [129] */
 	(void)0;
 
 	/*
+	 * At the point where lint checks for expressions having a null
+	 * effect, constants have been folded, therefore the following
+	 * expression is considered safe as well.  It does not appear in
+	 * practice though.
+	 */
+	(void)(3 - 3);
+
+	/*
 	 * This variant of the do-nothing command is commonly used in
 	 * preprocessor macros since it works nicely with if-else and if-then
 	 * statements.  It is longer than the above variant though.
@@ -81,9 +83,12 @@ legitimate_use_cases(int arg)
 
 	/*
 	 * Only the expression '(void)0' is common, other expressions are
-	 * unusual enough that they warrant a warning.
+	 * unusual enough to warrant a warning.
 	 */
-	/* TODO: remove this bogus warning. */
 	/* expect+1: warning: expression has null effect [129] */
 	(void)13;
+
+	/* Double casts are unusual enough to warrant a warning. */
+	/* expect+1: warning: expression has null effect [129] */
+	(void)(void)0;
 }
Index: src/tests/usr.bin/xlint/lint1/msg_129.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_129.exp:1.4 src/tests/usr.bin/xlint/lint1/msg_129.exp:1.5
--- src/tests/usr.bin/xlint/lint1/msg_129.exp:1.4	Sat Aug 21 07:52:07 2021
+++ src/tests/usr.bin/xlint/lint1/msg_129.exp	Sat Aug 21 08:18:48 2021
@@ -1,6 +1,4 @@
 msg_129.c(37): warning: expression has null effect [129]
 msg_129.c(41): warning: expression has null effect [129]
-msg_129.c(56): warning: expression has null effect [129]
-msg_129.c(67): warning: expression has null effect [129]
-msg_129.c(72): warning: expression has null effect [129]
-msg_129.c(88): warning: expression has null effect [129]
+msg_129.c(89): warning: expression has null effect [129]
+msg_129.c(93): warning: expression has null effect [129]

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.340 src/usr.bin/xlint/lint1/tree.c:1.341
--- src/usr.bin/xlint/lint1/tree.c:1.340	Thu Aug 19 21:13:58 2021
+++ src/usr.bin/xlint/lint1/tree.c	Sat Aug 21 08:18:48 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.340 2021/08/19 21:13:58 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.341 2021/08/21 08:18:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: tree.c,v 1.340 2021/08/19 21:13:58 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.341 2021/08/21 08:18:48 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -3801,14 +3801,47 @@ has_side_effect(const tnode_t *tn) // NO
 	return false;
 }
 
+static bool
+is_void_cast(const tnode_t *tn)
+{
+
+	return tn->tn_op == CVT && tn->tn_cast &&
+	       tn->tn_type->t_tspec == VOID;
+}
+
+static bool
+is_local_symbol(const tnode_t *tn)
+{
+
+	return tn->tn_op == LOAD &&
+	       tn->tn_left->tn_op == NAME &&
+	       tn->tn_left->tn_sym->s_scl == AUTO;
+}
+
+static bool
+is_int_constant_zero(const tnode_t *tn)
+{
+
+	return tn->tn_op == CON &&
+	       tn->tn_type->t_tspec == INT &&
+	       tn->tn_val->v_quad == 0;
+}
+
 static void
 check_null_effect(const tnode_t *tn)
 {
 
-	if (hflag && !has_side_effect(tn)) {
-		/* expression has null effect */
-		warning(129);
-	}
+	if (!hflag)
+		return;
+	if ( has_side_effect(tn))
+		return;
+	if (is_void_cast(tn) && is_local_symbol(tn->tn_left))
+		return;
+	if (is_void_cast(tn) && is_int_constant_zero(tn->tn_left))
+		return;
+
+	/* expression has null effect */
+	warning(129);
 }
 
 /* ARGSUSED */

Reply via email to