Grinnz / Perl-Critic-Community

Perl::Critic::Community - Community-inspired Perl::Critic policies
https://metacpan.org/pod/Perl::Critic::Community
Other
8 stars 10 forks source link

DollarAB false positive in dereference #28

Closed exercism-1 closed 6 years ago

exercism-1 commented 7 years ago
use strict;
use warnings;

my @xs = (
    ['foo'],
    ['bar', 'baz'],
);

@xs = sort {
    my ($k1) = @{$a};
    my ($k2) = @{$b};
    $k1 cmp $k2
} @xs;
$ perlcritic bug.pl
Using $a or $b outside sort() at line 10, column 18.  $a and $b are special package variables for use in sort() and related functions. Declaring them as lexicals like "my $a" may break sort(). Use different variable names.  (Severity: 4)
Using $a or $b outside sort() at line 11, column 18.  $a and $b are special package variables for use in sort() and related functions. Declaring them as lexicals like "my $a" may break sort(). Use different variable names.  (Severity: 4)

:-(

Grinnz commented 7 years ago

Oddly, PPI parses @{$a} as a block cast to @. The policy needs to be able to handle inner blocks anyway, though. This should be fixed in the next release. Thanks!

Grinnz commented 7 years ago

New false positive case:

sort { scalar(@{$a->{ids}}) <=> scalar(@{$b->{ids}}) } @foo;
Grinnz commented 6 years ago

This case should also be fixed by https://github.com/Grinnz/Perl-Critic-Freenode/commit/c35ced23c705abba3391de4f57fce954991f1c1a