develooper Front page | perl.perl5.porters | Postings from February 2006

Patch lint for grep { /.../ } and grep /.../,

Thread Next
From:
Joshua ben Jore
Date:
February 28, 2006 23:11
Subject:
Patch lint for grep { /.../ } and grep /.../,
Message ID:
dc5c751d0602282311p4f7481d4s5dbed3c7456f3979@mail.gmail.com
Without this patch the code C<grep /.../> and C<grep { /.../ }>
triggers the warning "Implict match on $_". I don't think the lint
warning is correct so here's the patch to remove it from that case.

Can someone tell my why lint tells me that statement modifier foreach
is also a problem? C<... for ...;> is really common. It triggers the
warning "Implicit use of $_ in foreach". I can understand C<for ( ...
) {... }> throwing the error but dont' see why C<... for ...;> does.

Josh

diff -ur bleadperl/ext/B/B/Lint.pm myblead/ext/B/B/Lint.pm
--- bleadperl/ext/B/B/Lint.pm   2005-12-20 08:54:26.000000000 -0600
+++ myblead/ext/B/B/Lint.pm     2006-03-01 01:03:27.000000000 -0600
@@ -1,6 +1,6 @@
 package B::Lint;

-our $VERSION = '1.04';
+our $VERSION = '1.05';

 =head1 NAME

@@ -280,7 +280,12 @@
 sub B::PMOP::lint {
     my $op = shift;
     if ($check{implicit_read}) {
-       if ($op->name eq "match" && !($op->flags & OPf_STACKED)) {
+       if ($op->name eq "match"
+            and not ( $op->flags & OPf_STACKED
+                      or join( " ",
+                               map $_->name,
+                               @{B::parents()} )
+                         =~ /^(?:leave )?(?:null )*grep/ ) ) {
            warning('Implicit match on $_');
        }
     }
diff -ur bleadperl/ext/B/t/lint.t myblead/ext/B/t/lint.t
--- bleadperl/ext/B/t/lint.t    2005-12-27 08:14:46.000000000 -0600
+++ myblead/ext/B/t/lint.t      2006-03-01 00:14:29.000000000 -0600
@@ -16,7 +16,7 @@
     require 'test.pl';
 }

-plan tests => 16; # adjust also number of skipped tests !
+plan tests => 17; # adjust also number of skipped tests !

 # Runs a separate perl interpreter with the appropriate lint options
 # turned on
@@ -43,6 +43,8 @@
 Implicit match on $_ at -e line 1
 RESULT

+runlint 'implicit-read', 'grep /foo/, ()', '';
+
 runlint 'implicit-write', 's/foo/bar/', <<'RESULT';
 Implicit substitution on $_ at -e line 1
 RESULT

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