develooper Front page | perl.perl5.porters | Postings from August 2014

[perl #122567] [PATCH] Warn when a conditional my() is used with an assignment

Thread Next
From:
Lubomir Rintel
Date:
August 20, 2014 16:21
Subject:
[perl #122567] [PATCH] Warn when a conditional my() is used with an assignment
Message ID:
rt-4.0.18-23036-1408481579-787.122567-75-0@perl.org
# New Ticket Created by  Lubomir Rintel 
# Please include the string:  [perl #122567]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=122567 >


The "Deprecated use of my() in false conditional" failed to trigger in case
there was an assignment (my $x = 123 if 0), silently creating "static"
variables without a warning. This aims to correct thit and adds a test case.

The only occassion in perl tree that triggers the warning is in Math::BigFloat,
fix submitted in [rt.cpan.org #98185].
---
 op.c              | 21 +++++++++++++++++----
 t/lib/warnings/op | 14 ++++++++++++++
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/op.c b/op.c
index f785c55..75abb50 100644
--- a/op.c
+++ b/op.c
@@ -6172,14 +6172,27 @@ S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
 	    return other;
 	}
 	else {
-	    /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
+	    /* Check for abuse of my() in false conditional to create a state variable */
 	    const OP *o2 = other;
-	    if ( ! (o2->op_type == OP_LIST
+
+	    /* C<my ($x,$y) if 0> */
+	    if ( ! (((o2->op_type == OP_LIST ) ||
+		     /* C<my @a = (1,2,3) if 0>, C<my ($x,$y) = (1,2) if 0> */
+		     (o2->op_type == OP_AASSIGN && (( o2 = cBINOPx(o2)->op_last)))
+		    )
 		    && (( o2 = cUNOPx(o2)->op_first))
 		    && o2->op_type == OP_PUSHMARK
 		    && (( o2 = OP_SIBLING(o2))) )
-	    )
-		o2 = other;
+	    ) {
+		/* C<my $x = 123 if 0> */
+		if (! (o2->op_type == OP_SASSIGN
+		    && (( o2 = cBINOPx(o2)->op_last )) )
+		) {
+		    /* None of the above. Maybe just C<my $x if 0> */
+		    o2 = other;
+		}
+	    }
+
 	    if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
 			|| o2->op_type == OP_PADHV)
 		&& o2->op_private & OPpLVAL_INTRO
diff --git a/t/lib/warnings/op b/t/lib/warnings/op
index 33ee585..689ebff 100644
--- a/t/lib/warnings/op
+++ b/t/lib/warnings/op
@@ -1640,6 +1640,13 @@ my ($x4) if 0;
 my ($x5,@x6, %x7) if 0;
 0 && my $z1;
 0 && my (%z2);
+my $x1 = length("moo") if 0;
+my @x2 = (1, 2, 3) if 0;
+my %x3 = () if 0;
+my ($x4) = (666) if 0;
+my ($x5,@x6, %x7) = (0,1,length("moo")) if 0;
+0 && my $z1;
+0 && my (%z2);
 # these shouldn't warn
 our $x if 0;
 our $x unless 0;
@@ -1655,6 +1662,13 @@ Deprecated use of my() in false conditional at - line 5.
 Deprecated use of my() in false conditional at - line 6.
 Deprecated use of my() in false conditional at - line 7.
 Deprecated use of my() in false conditional at - line 8.
+Deprecated use of my() in false conditional at - line 9.
+Deprecated use of my() in false conditional at - line 10.
+Deprecated use of my() in false conditional at - line 11.
+Deprecated use of my() in false conditional at - line 12.
+Deprecated use of my() in false conditional at - line 13.
+Deprecated use of my() in false conditional at - line 14.
+Deprecated use of my() in false conditional at - line 15.
 ########
 # op.c
 $[ = 1;
-- 
2.0.4


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About