Perl/Lint/Policy/RegularExpressions/ProhibitCaptureWithoutTest.pm

filter: Statementstmt 20.3% 21 / 103 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 1 totaltotal 10.8% 27 / 249 CC 51 SCAR 77.5
CC
51
Cov
10%
evaluate
2652.0
__ANON__
2.0
BEGIN
1.0
CRAP 2332.1
« Perl/Lint/Policy/Subroutines/ProhibitUnusedPrivateSubroutines.pm ↑ Summary Perl/Lint/Policy/ValuesAndExpressions/ProhibitCommaSeparatedStatements.pm »
1 package Perl::Lint::Policy::RegularExpressions::ProhibitCaptureWithoutTest;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use List::Util qw/none/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 DESC => 'Capture variable used outside conditional',
10 EXPL => [253],
11 2 };
12  
13 2 my %transfer_of_control_stmt_token_types = (
14 &NEXT => 1,
15 &LAST => 1,
16 &REDO => 1,
17 &GOTO => 1,
18 &RETURN => 1,
19 );
20  
21 2 my %control_stmt_token_types = (
22 &IF_STATEMENT => 1,
23 &ELSIF_STATEMENT => 1,
24 &UNLESS_STATEMENT => 1,
25 &WHILE_STATEMENT => 1,
26 );
27  
28 0 ▶sub evaluate {
Subroutine evaluate: not called CC=51 SCAR=78.8
Pod undocumented
29 0 my ($class, $file, $tokens, $src, $args) = @_;
30  
31 0 my %exceptions = (
32 die => 1,
33 croak => 1,
34 confess => 1,
35 );
36  
37 0 ▶ if (my $this_policies_arg = $args->{prohibit_capture_without_test}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_policies_arg = $args->{"prohibit_capture_without_test"}) 0 0 0
38 0 ▶ for my $exception (split(/\s+/, $this_policies_arg->{exception_source} || '')) {
Condition0%0/2
$this_policies_arg->{'exception_source'} || ''
l!l
00
MC/DC0%0/1
$this_policies_arg->{'exception_source'} || ''
$this_policies_arg->{'exception_source'}
Truth table0%0/2
A
A= $this_policies_arg->{'exception_source'}
Aresult
11
00
39 0 $exceptions{$exception} = 1;
40 };
41 }
42  
43 0 my @violations;
44 my @is_tested_by_depth;
45 0 my $is_in_context_to_assign = 0;
46 0 my $depth = 0;
47 # use Data::Dumper::Concise; warn Dumper($tokens); # TODO remove
48 0 ▶ for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 0 0 0
49 0 $token_type = $token->{type};
50 0 $token_data = $token->{data};
51  
52 0 ▶ if ($token_type == REG_QUOTE || $token_type == REG_DOUBLE_QUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 145 or $token_type == 146) 0 0 0
Condition0%0/3
$token_type == 145 or $token_type == 146
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 145 or $token_type == 146
$token_type == 145 $token_type == 146
Truth table0%0/3
A or B
A= $token_type == 145B= $token_type == 146
ABresult
1X1
011
000
53 # skip reg quotes (because it is recognized as regexp)
54 0 $i += 2;
55 0 next;
56 }
57  
58 0 ▶ if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 187 or $token_type == 156) 0 0 0
Condition0%0/3
$token_type == 187 or $token_type == 156
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 187 or $token_type == 156
$token_type == 187 $token_type == 156
Truth table0%0/3
A or B
A= $token_type == 187B= $token_type == 156
ABresult
1X1
011
000
59 # XXX
60 0 ▶ if ($depth < 0 && scalar @is_tested_by_depth < -$depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < 0 and scalar @is_tested_by_depth < -$depth) 0 0 0
Condition0%0/3
$depth < 0 and scalar @is_tested_by_depth < -$depth
!ll&&!rl&&r
000
MC/DC0%0/2
$depth < 0 and scalar @is_tested_by_depth < -$depth
$depth < 0 scalar @is_tested_by_depth < -$depth
Truth table0%0/3
A and B
A= $depth < 0B= scalar @is_tested_by_depth < -$depth
ABresult
0X0
100
111
61 0 next;
62 }
63 0 ▶ $is_tested_by_depth[$depth] = $is_in_context_to_assign ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_in_context_to_assign ? : 0 0 0
64  
65 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
66 0 $token_type = $token->{type};
67 0 ▶ if ($token_type == SEMI_COLON) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token_type == 106) { } 0 0 0
elsif ($token_type == 6) { } 0 0 0
elsif ($token_type == 53 or $token_type == 14) { } 0 0 0
68 0 goto END_OF_STATEMENT;
69 }
70 elsif (
71 $token_type == THREE_TERM_OP
72 ) {
73 0 $is_tested_by_depth[$depth] = 1;
74 0 last;
75 }
76 ▶ elsif (
Condition0%0/3
$token_type == 53 or $token_type == 14
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 53 or $token_type == 14
$token_type == 53 $token_type == 14
Truth table0%0/3
A or B
A= $token_type == 53B= $token_type == 14
ABresult
1X1
011
000
77 $token_type == OR ||
78 $token_type == ALPHABET_OR
79 ) {
80 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
81 0 $token_type = $token->{type};
82 0 $token_data = $token->{data};
83  
84 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($exceptions{$token_data} and $token_type == 122 or $token_type == 70 or $transfer_of_control_stmt_token_types{$token_type}) 0 0 0
Condition0%0/9
$exceptions{$token_data} and $token_type == 122 or $token_type == 70 or $transfer_of_control_stmt_token_types{$token_type}
l!l&&r!l&&!r
000
$exceptions{$token_data} and $token_type == 122 or $token_type == 70
l!l&&r!l&&!r
000
$exceptions{$token_data} and $token_type == 122
!ll&&!rl&&r
000
MC/DC0%0/4
$exceptions{$token_data} and $token_type == 122 or $token_type == 70 or $transfer_of_control_stmt_token_types{$token_type}
$exceptions{$token_data} $token_type == 122 $token_type == 70 $transfer_of_control_stmt_token_types{$token_type}
Truth table0%0/7
A and B or C or D
A= $exceptions{$token_data}B= $token_type == 122C= $token_type == 70D= $transfer_of_control_stmt_token_types{$token_type}
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
85 ($exceptions{$token_data} && $token_type == KEY || $token_type == BUILTIN_FUNC) ||
86 $transfer_of_control_stmt_token_types{$token_type}
87 ) {
88 0 $is_tested_by_depth[$depth] = 1;
89 0 last;
90 }
91  
92 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
93 0 ▶ if ($token->{type} == POINTER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 125) 0 0 0
94 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
95 0 $token_type = $token->{type};
96 0 $token_data = $token->{data};
97 0 ▶ if ($exceptions{$token_data} && $token_type == METHOD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exceptions{$token_data} and $token_type == 64) 0 0 0
Condition0%0/3
$exceptions{$token_data} and $token_type == 64
!ll&&!rl&&r
000
MC/DC0%0/2
$exceptions{$token_data} and $token_type == 64
$exceptions{$token_data} $token_type == 64
Truth table0%0/3
A and B
A= $exceptions{$token_data}B= $token_type == 64
ABresult
0X0
100
111
98 0 $is_tested_by_depth[$depth] = 1;
99 }
100 0 last;
101 }
102  
103 0 last;
104 }
105 }
106  
107 0 next;
108 }
109  
110 0 ▶ if ($token_type == SPECIFIC_VALUE && $token_data =~ /\A\$[1-9][0-9]*\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 137 and $token_data =~ /\A\$[1-9][0-9]*\Z/) 0 0 0
Condition0%0/3
$token_type == 137 and $token_data =~ /\A\$[1-9][0-9]*\Z/
!ll&&!rl&&r
000
MC/DC0%0/2
$token_type == 137 and $token_data =~ /\A\$[1-9][0-9]*\Z/
$token_type == 137 $token_data =~ /\A\$[1-9][0-9]*\Z/
Truth table0%0/3
A and B
A= $token_type == 137B= $token_data =~ /\A\$[1-9][0-9]*\Z/
ABresult
0X0
100
111
111 0 ▶ if (none {$_} @is_tested_by_depth) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $_; } , @is_tested_by_depth)) 0 0 0
112 push @violations, {
113 filename => $file,
114 line => $token->{line},
115 0 description => DESC,
116 explanation => EXPL,
117 policy => __PACKAGE__,
118 };
119 }
120  
121 0 next;
122 }
123  
124 0 ▶ if ($control_stmt_token_types{$token_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($control_stmt_token_types{$token_type}) 0 0 0
125 0 $token = $tokens->[++$i];
126 0 $token_type = $token->{type};
127 0 ▶ if ($token_type == LEFT_PAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 107) 0 0 0
128 0 my $lpnum = 1;
129 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
130 0 $token_type = $token->{type};
131 0 ▶ if ($token_type == LEFT_PAREN) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token_type == 107) { } 0 0 0
elsif ($token_type == 108) { } 0 0 0
elsif ($token_type == 187 or $token_type == 156) { } 0 0 0
132 0 $lpnum++;
133 }
134 elsif ($token_type == RIGHT_PAREN) {
135 0 ▶ last if --$lpnum <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$lpnum <= 0 0 0 0
136 }
137 ▶ elsif ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
Condition0%0/3
$token_type == 187 or $token_type == 156
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 187 or $token_type == 156
$token_type == 187 $token_type == 156
Truth table0%0/3
A or B
A= $token_type == 187B= $token_type == 156
ABresult
1X1
011
000
138 # XXX
139 0 ▶ if ($depth + 1 < 0 && scalar @is_tested_by_depth < -$depth + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth + 1 < 0 and scalar @is_tested_by_depth < -$depth + 1) 0 0 0
Condition0%0/3
$depth + 1 < 0 and scalar @is_tested_by_depth < -$depth + 1
!ll&&!rl&&r
000
MC/DC0%0/2
$depth + 1 < 0 and scalar @is_tested_by_depth < -$depth + 1
$depth + 1 < 0 scalar @is_tested_by_depth < -$depth + 1
Truth table0%0/3
A and B
A= $depth + 1 < 0B= scalar @is_tested_by_depth < -$depth + 1
ABresult
0X0
100
111
140 0 next;
141 }
142 0 $is_tested_by_depth[$depth + 1] = 1;
143 }
144 }
145  
146 0 next;
147 }
148  
149 # for postfix
150 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
151 0 $token_type = $token->{type};
152 0 ▶ if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 187 or $token_type == 156) { } 0 0 0
elsif ($token_type == 106) { } 0 0 0
Condition0%0/3
$token_type == 187 or $token_type == 156
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 187 or $token_type == 156
$token_type == 187 $token_type == 156
Truth table0%0/3
A or B
A= $token_type == 187B= $token_type == 156
ABresult
1X1
011
000
153 0 $is_tested_by_depth[$depth + 1] = 1;
154 }
155 elsif ($token_type == SEMI_COLON) {
156 0 last;
157 }
158 }
159 0 next;
160 }
161  
162 0 ▶ if ($token_type == LEFT_BRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 109) 0 0 0
163 0 $depth++;
164  
165 # XXX
166 0 ▶ if ($depth < 0 && scalar @is_tested_by_depth < -$depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < 0 and scalar @is_tested_by_depth < -$depth) 0 0 0
Condition0%0/3
$depth < 0 and scalar @is_tested_by_depth < -$depth
!ll&&!rl&&r
000
MC/DC0%0/2
$depth < 0 and scalar @is_tested_by_depth < -$depth
$depth < 0 scalar @is_tested_by_depth < -$depth
Truth table0%0/3
A and B
A= $depth < 0B= scalar @is_tested_by_depth < -$depth
ABresult
0X0
100
111
167 0 next;
168 }
169  
170 0 ▶ $is_tested_by_depth[$depth] ||= 0;
Condition0%0/2
$is_tested_by_depth[$depth] ||= 0
l!l
00
MC/DC0%0/1
$is_tested_by_depth[$depth] ||= 0
$is_tested_by_depth[$depth]
Truth table0%0/2
A
A= $is_tested_by_depth[$depth]
Aresult
11
00
171 0 next;
172 }
173  
174 0 ▶ if ($token_type == RIGHT_BRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 110) 0 0 0
175 0 pop @is_tested_by_depth;
176 0 $depth--;
177 0 next;
178 }
179  
180 0 ▶ if ($token_type == ASSIGN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 65) 0 0 0
181 0 $is_in_context_to_assign = 1;
182 0 next;
183 }
184  
185 END_OF_STATEMENT:
186 0 ▶ if ($token_type == SEMI_COLON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 106) 0 0 0
187 0 $is_in_context_to_assign = 0;
188 0 next;
189 }
190 }
191  
192 0 return \@violations;
193 }
194  
195 2 1;
196  
« Perl/Lint/Policy/Subroutines/ProhibitUnusedPrivateSubroutines.pm ↑ Summary Perl/Lint/Policy/ValuesAndExpressions/ProhibitCommaSeparatedStatements.pm »