OK, I wrote a bunch of regression tests (attached) that fail because of
hash key barewords. I marked them as TODO tests, so they pass when you do
Build test
but you can see the failures when you do
Build test test_files=t/02_policies.t verbose=1
There, you see four failing tests under:
BuiltinFunctions::ProhibitStringyEval
BuiltinFunctions::RequireBlockGrep
BuiltinFunctions::RequireBlockMap
ControlStructures::ProhibitPostfixControls
In all four cases, if you can distinguish hash keys from function calls
(either via a new PPI construct or by looking at the element's parent
node), then the problem goes away.
I couldn't create any failure with these policies (which also use
PPI::Token::Word nodes) because thy look at siblings. I couldn't think
of a way to give a hashkey a sibling, so I couldn't make a failure.
CodeLayout::ProhibitParensWithBuiltins
ControlStructures::ProhibitCascadingIfElse
Subroutines::ProhibitExplicitReturnUndef
-- Chris
diff -Nur Perl-Critic-0.12-orig/t/02_policies.t Perl-Critic-0.12-hashtests/t/02_policies.t
--- Perl-Critic-0.12-orig/t/02_policies.t 2005-10-11 03:16:50.000000000 -0500
+++ Perl-Critic-0.12-hashtests/t/02_policies.t 2005-10-12 21:38:34.000000000 -0500
@@ -1,7 +1,7 @@
use blib;
use strict;
use warnings;
-use Test::More tests => 96;
+use Test::More tests => 100;
use Perl::Critic;
my $code = undef;
@@ -28,6 +28,20 @@
#----------------------------------------------------------------
+TODO: {
+local $TODO = "keywords as hash keys are not recognized";
+
+$code = <<'END_PERL';
+my %hash;
+$hash{eval} = 1;
+END_PERL
+
+$policy = 'BuiltinFunctions::ProhibitStringyEval';
+is( critique($policy, \$code), 0, $policy);
+}
+
+#----------------------------------------------------------------
+
$code = <<'END_PERL';
grep $_ eq 'foo', @list;
@matches = grep $_ eq 'foo', @list;
@@ -48,6 +62,20 @@
#----------------------------------------------------------------
+TODO: {
+local $TODO = "keywords as hash keys are not recognized";
+
+$code = <<'END_PERL';
+my %hash;
+$hash{grep} = 1;
+END_PERL
+
+$policy = 'BuiltinFunctions::RequireBlockGrep';
+is( critique($policy, \$code), 0, $policy);
+}
+
+#----------------------------------------------------------------
+
$code = <<'END_PERL';
map $_++, @list;
@foo = map $_++, @list;
@@ -68,6 +96,20 @@
#-----------------------------------------------------------------------------
+TODO: {
+local $TODO = "keywords as hash keys are not recognized";
+
+$code = <<'END_PERL';
+my %hash;
+$hash{map} = 1;
+END_PERL
+
+$policy = 'BuiltinFunctions::RequireBlockMap';
+is( critique($policy, \$code), 0, $policy);
+}
+
+#----------------------------------------------------------------
+
$code = <<'END_PERL';
@files = <*.pl>;
END_PERL
@@ -311,6 +353,24 @@
#----------------------------------------------------------------
+TODO: {
+local $TODO = "keywords as hash keys are not recognized";
+
+$code = <<'END_PERL';
+my %hash;
+$hash{if} = 1;
+$hash{unless} = 1;
+$hash{while} = 1;
+$hash{until} = 1;
+$hash{for} = 1;
+END_PERL
+
+$policy = 'ControlStructures::ProhibitPostfixControls';
+is( critique($policy, \$code), 0, $policy);
+}
+
+#----------------------------------------------------------------
+
$code = <<'END_PERL';
if ($condition1){
$foo;