Make the display of the warning "Useless use of a constant (%s)" more robust
authorRafael Garcia-Suarez <rgs@consttype.org>
Wed, 25 Jan 2012 10:41:13 +0000 (11:41 +0100)
committerRafael Garcia-Suarez <rgs@consttype.org>
Wed, 25 Jan 2012 10:48:22 +0000 (11:48 +0100)
If the constant is a string (POK), we dump it using pv_pretty, to
properly escape non-printable characters. This also improves detection
of utf-8 encoding in the constant to be printed. Also, this patch
streamlines the tests for the type and value of that constant.

Potential backwards-compatibility issues : if the constant is a string
it will now be enclosed in double quotes, like this :
    Useless use of a constant ("a") in void context
instead of
    Useless use of a constant (a) in void context

op.c
t/lib/warnings/op
t/lib/warnings/perly

diff --git a/op.c b/op.c
index 3af6ee7..e31f742 100644 (file)
--- a/op.c
+++ b/op.c
@@ -1167,14 +1167,6 @@ Perl_scalarvoid(pTHX_ OP *o)
            no_bareword_allowed(o);
        else {
            if (ckWARN(WARN_VOID)) {
-               if (SvOK(sv)) {
-                   SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
-                               "a constant (%"SVf")", sv));
-                   useless = SvPV_nolen(msv);
-                    useless_is_utf8 = SvUTF8(msv);
-               }
-               else
-                   useless = "a constant (undef)";
                /* don't warn on optimised away booleans, eg 
                 * use constant Foo, 5; Foo || print; */
                if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
@@ -1196,7 +1188,24 @@ Perl_scalarvoid(pTHX_ OP *o)
                        strnEQ(maybe_macro, "ds", 2) ||
                        strnEQ(maybe_macro, "ig", 2))
                            useless = NULL;
+                   else {
+                       SV * const dsv = newSV(0);
+                       SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
+                                   "a constant (%s)",
+                                   pv_pretty(dsv, maybe_macro, SvCUR(sv), 32, NULL, NULL,
+                                           PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_NOCLEAR | PERL_PV_ESCAPE_UNI_DETECT )));
+                       SvREFCNT_dec(dsv);
+                       useless = SvPV_nolen(msv);
+                       useless_is_utf8 = SvUTF8(msv);
+                   }
                }
+               else if (SvOK(sv)) {
+                   SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
+                               "a constant (%"SVf")", sv));
+                   useless = SvPV_nolen(msv);
+               }
+               else
+                   useless = "a constant (undef)";
            }
        }
        op_null(o);             /* don't execute or even remember it */
index 344cf12..8f57920 100644 (file)
@@ -526,6 +526,7 @@ use warnings 'void' ;
 2 + 2; # optimized to OP_CONST
 use constant U => undef;
 U;
+qq/"   \n/;
 5 || print "bad\n";    # test OPpCONST_SHORTCIRCUIT
 print "boo\n" if U;    # test OPpCONST_SHORTCIRCUIT
 no warnings 'void' ;
@@ -534,11 +535,12 @@ no warnings 'void' ;
 "x" . "y"; # optimized to OP_CONST
 2 + 2; # optimized to OP_CONST
 EXPECT
-Useless use of a constant (abc) in void context at - line 3.
+Useless use of a constant ("abc") in void context at - line 3.
 Useless use of a constant (7) in void context at - line 4.
-Useless use of a constant (xy) in void context at - line 5.
+Useless use of a constant ("xy") in void context at - line 5.
 Useless use of a constant (4) in void context at - line 6.
 Useless use of a constant (undef) in void context at - line 8.
+Useless use of a constant ("\"\t\n") in void context at - line 9.
 ########
 # op.c
 use utf8;
@@ -555,9 +557,9 @@ no warnings 'void' ;
 "àḆc"; # OP_CONST
 "Ẋ" . "ƴ"; # optimized to OP_CONST
 EXPECT
-Useless use of a constant (àḆc) in void context at - line 5.
-Useless use of a constant (Ẋƴ) in void context at - line 6.
-Useless use of a constant (FOO) in void context at - line 7.
+Useless use of a constant ("\340\x{1e06}c") in void context at - line 5.
+Useless use of a constant ("\x{1e8a}\x{1b4}") in void context at - line 6.
+Useless use of a constant ("\x{ff26}\x{ff2f}\x{ff2f}") in void context at - line 7.
 Useless use of a constant (undef) in void context at - line 9.
 ########
 # op.c
index 02e29fd..d2b9560 100644 (file)
@@ -55,9 +55,9 @@ EXPECT
 Use of qw(...) as parentheses is deprecated at - line 2.
 Use of qw(...) as parentheses is deprecated at - line 3.
 Use of qw(...) as parentheses is deprecated at - line 4.
-Useless use of a constant (z) in void context at - line 4.
+Useless use of a constant ("z") in void context at - line 4.
 Use of qw(...) as parentheses is deprecated at - line 5.
-Useless use of a constant (z) in void context at - line 5.
+Useless use of a constant ("z") in void context at - line 5.
 x0
 x2
 ########
@@ -76,9 +76,9 @@ EXPECT
 Use of qw(...) as parentheses is deprecated at - line 2.
 Use of qw(...) as parentheses is deprecated at - line 3.
 Use of qw(...) as parentheses is deprecated at - line 4.
-Useless use of a constant (z) in void context at - line 4.
+Useless use of a constant ("z") in void context at - line 4.
 Use of qw(...) as parentheses is deprecated at - line 5.
-Useless use of a constant (z) in void context at - line 5.
+Useless use of a constant ("z") in void context at - line 5.
 x1
 x3
 ########
@@ -97,9 +97,9 @@ EXPECT
 Use of qw(...) as parentheses is deprecated at - line 2.
 Use of qw(...) as parentheses is deprecated at - line 3.
 Use of qw(...) as parentheses is deprecated at - line 4.
-Useless use of a constant (z) in void context at - line 4.
+Useless use of a constant ("z") in void context at - line 4.
 Use of qw(...) as parentheses is deprecated at - line 5.
-Useless use of a constant (z) in void context at - line 5.
+Useless use of a constant ("z") in void context at - line 5.
 x0
 x2
 ########
@@ -115,7 +115,7 @@ given qw(z a) { print "x1 $_\n"; }
 EXPECT
 Use of qw(...) as parentheses is deprecated at - line 2.
 Use of qw(...) as parentheses is deprecated at - line 3.
-Useless use of a constant (z) in void context at - line 3.
+Useless use of a constant ("z") in void context at - line 3.
 x0 a
 x1 a
 ########
@@ -135,9 +135,9 @@ EXPECT
 Use of qw(...) as parentheses is deprecated at - line 2.
 Use of qw(...) as parentheses is deprecated at - line 3.
 Use of qw(...) as parentheses is deprecated at - line 4.
-Useless use of a constant (z) in void context at - line 4.
+Useless use of a constant ("z") in void context at - line 4.
 Use of qw(...) as parentheses is deprecated at - line 5.
-Useless use of a constant (z) in void context at - line 5.
+Useless use of a constant ("z") in void context at - line 5.
 x0
 x2
 ########
@@ -158,9 +158,9 @@ EXPECT
 Use of qw(...) as parentheses is deprecated at - line 2.
 Use of qw(...) as parentheses is deprecated at - line 3.
 Use of qw(...) as parentheses is deprecated at - line 4.
-Useless use of a constant (z) in void context at - line 4.
+Useless use of a constant ("z") in void context at - line 4.
 Use of qw(...) as parentheses is deprecated at - line 5.
-Useless use of a constant (z) in void context at - line 5.
+Useless use of a constant ("z") in void context at - line 5.
 x0
 x2
 ########
@@ -178,9 +178,9 @@ EXPECT
 Use of qw(...) as parentheses is deprecated at - line 2.
 Use of qw(...) as parentheses is deprecated at - line 3.
 Use of qw(...) as parentheses is deprecated at - line 4.
-Useless use of a constant (z) in void context at - line 4.
+Useless use of a constant ("z") in void context at - line 4.
 Use of qw(...) as parentheses is deprecated at - line 5.
-Useless use of a constant (z) in void context at - line 5.
+Useless use of a constant ("z") in void context at - line 5.
 x1
 x3
 ########