Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: do not warn about '(void)arg' and ...



details:   https://anonhg.NetBSD.org/src/rev/3806c2edf0df
branches:  trunk
changeset: 1023041:3806c2edf0df
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Aug 21 08:18:48 2021 +0000

description:
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.

diffstat:

 tests/usr.bin/xlint/lint1/msg_129.c   |  23 ++++++++++-------
 tests/usr.bin/xlint/lint1/msg_129.exp |   6 +---
 usr.bin/xlint/lint1/tree.c            |  45 ++++++++++++++++++++++++++++++----
 3 files changed, 55 insertions(+), 19 deletions(-)

diffs (140 lines):

diff -r 13381ec784dc -r 3806c2edf0df tests/usr.bin/xlint/lint1/msg_129.c
--- a/tests/usr.bin/xlint/lint1/msg_129.c       Sat Aug 21 07:52:07 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_129.c       Sat Aug 21 08:18:48 2021 +0000
@@ -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 @@
         * 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 @@
         * 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 @@
 
        /*
         * 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;
 }
diff -r 13381ec784dc -r 3806c2edf0df tests/usr.bin/xlint/lint1/msg_129.exp
--- a/tests/usr.bin/xlint/lint1/msg_129.exp     Sat Aug 21 07:52:07 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_129.exp     Sat Aug 21 08:18:48 2021 +0000
@@ -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]
diff -r 13381ec784dc -r 3806c2edf0df usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sat Aug 21 07:52:07 2021 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sat Aug 21 08:18:48 2021 +0000
@@ -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 @@
        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 */



Home | Main Index | Thread Index | Old Index