This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
stop "const in void context" warning for a const in an
authorDave Mitchell <davem@fdisolutions.com>
Wed, 25 Feb 2004 17:10:56 +0000 (17:10 +0000)
committerDave Mitchell <davem@fdisolutions.com>
Wed, 25 Feb 2004 17:10:56 +0000 (17:10 +0000)
optimised-away boolean expresssion, eg 5 || print;

p4raw-id: //depot/perl@22376

op.c
op.h
t/lib/warnings/op

diff --git a/op.c b/op.c
index 4bd252c..4273e65 100644 (file)
--- a/op.c
+++ b/op.c
@@ -677,10 +677,14 @@ Perl_scalarvoid(pTHX_ OP *o)
        else {
            if (ckWARN(WARN_VOID)) {
                useless = "a constant";
+               /* don't warn on optimised away booleans, eg 
+                * use constant F, 5; Foo || print; */
+               if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
+                   useless = 0;
                /* the constants 0 and 1 are permitted as they are
                   conventionally used as dummies in constructs like
                        1 while some_condition_with_side_effects;  */
-               if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
+               else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
                    useless = 0;
                else if (SvPOK(sv)) {
                   /* perl4's way of mixing documentation and code
@@ -3363,11 +3367,13 @@ S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
        if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
            op_free(first);
            *firstp = Nullop;
+           other->op_private |= OPpCONST_SHORTCIRCUIT;
            return other;
        }
        else {
            op_free(other);
            *otherp = Nullop;
+           first->op_private |= OPpCONST_SHORTCIRCUIT;
            return first;
        }
     }
diff --git a/op.h b/op.h
index 889b3ea..116bcd3 100644 (file)
--- a/op.h
+++ b/op.h
@@ -181,6 +181,7 @@ Deprecated.  Use C<GIMME_V> instead.
 #define OPpTARGET_MY           16      /* Target is PADMY. */
 
 /* Private for OP_CONST */
+#define        OPpCONST_SHORTCIRCUIT   4       /* eg the constant 5 in (5 || foo) */
 #define        OPpCONST_STRICT         8       /* bearword subject to strict 'subs' */
 #define OPpCONST_ENTERED       16      /* Has been entered as symbol. */
 #define OPpCONST_ARYBASE       32      /* Was a $[ translated to constant. */
index c101ffe..3aa5299 100644 (file)
@@ -525,6 +525,9 @@ Useless use of a variable in void context at - line 6.
 use warnings 'void' ;
 "abc"; # OP_CONST
 7 ; # OP_CONST
+5 || print "bad\n";    # test OPpCONST_SHORTCIRCUIT
+use constant U => undef;
+print "boo\n" if U;    # test OPpCONST_SHORTCIRCUIT
 no warnings 'void' ;
 "abc"; # OP_CONST
 7 ; # OP_CONST