Perl/Lint/Policy/ControlStructures/ProhibitUnreachableCode.pm

filter: Statementstmt 23.7% 19 / 80 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 12.4% 24 / 193 CC 36 SCAR 70.6
CC
36
Cov
12%
evaluate
1332.0
BEGIN
1.0
BEGIN
1.0
CRAP 1169.7
« Perl/Lint/Policy/Subroutines/RequireArgUnpacking.pm ↑ Summary Perl/Lint/Policy/ValuesAndExpressions/RequireConstantVersion.pm »
1 package Perl::Lint::Policy::ControlStructures::ProhibitUnreachableCode;
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 Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 DESC => 'Unreachable code',
9 EXPL => 'Consider removing it',
10 2 };
11  
12 2 my %control_statement_token_types_to_exit = (
13 &RETURN => 1,
14 &NEXT => 1,
15 &LAST => 1,
16 &REDO => 1,
17 );
18  
19 2 my %control_statement_to_exit = (
20 die => 1,
21 exit => 1,
22 croak => 1,
23 confess => 1,
24 );
25  
26 2 my %conditional_token_types = (
27 &AND => 1,
28 &OR => 1,
29 &ALPHABET_AND => 1,
30 &ALPHABET_OR => 1,
31 );
32  
33 0 ▶sub evaluate {
Subroutine evaluate: not called CC=36 SCAR=71.9
Pod undocumented
34 0 my ($class, $file, $tokens, $src, $args) = @_;
35  
36 0 my $depth = 0;
37 0 my %is_exited_by_depth;
38 my %unreachable_token_by_depth;
39 0 my %is_in_ignore_context_by_depth;
40  
41 0 my @violations;
42 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
43 0 $token_type = $token->{type};
44 0 $token_data = $token->{data};
45  
46 0 ▶ if ($token_type == LEFT_BRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 109) 0 0 0
47 0 $depth++;
48 0 next;
49 }
50  
51 0 ▶ if ($token_type == RIGHT_BRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 110) 0 0 0
52 0 ▶ if (my $unreachable_token = $unreachable_token_by_depth{$depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $unreachable_token = $unreachable_token_by_depth{$depth}) 0 0 0
53 push @violations, {
54 filename => $file,
55 line => $unreachable_token->{line},
56 0 description => DESC,
57 explanation => EXPL,
58 policy => __PACKAGE__,
59 };
60  
61 0 undef $unreachable_token_by_depth{$depth};
62 }
63  
64 0 $is_exited_by_depth{$depth} = 0;
65 0 $is_in_ignore_context_by_depth{$depth} = 0;
66 0 $depth--;
67 0 next;
68 }
69  
70 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 122 || $token_type == 70 and $control_statement_to_exit{$token_data} or $control_statement_token_types_to_exit{$token_type}) 0 0 0
Condition0%0/9
$token_type == 122 || $token_type == 70 and $control_statement_to_exit{$token_data} or $control_statement_token_types_to_exit{$token_type}
l!l&&r!l&&!r
000
$token_type == 122 || $token_type == 70 and $control_statement_to_exit{$token_data}
!ll&&!rl&&r
000
$token_type == 122 or $token_type == 70
l!l&&r!l&&!r
000
MC/DC0%0/4
$token_type == 122 || $token_type == 70 and $control_statement_to_exit{$token_data} or $control_statement_token_types_to_exit{$token_type}
$token_type == 122 $token_type == 70 $control_statement_to_exit{$token_data} $control_statement_token_types_to_exit{$token_type}
Truth table0%0/8
A or B and C or D
A= $token_type == 122B= $token_type == 70C= $control_statement_to_exit{$token_data}D= $control_statement_token_types_to_exit{$token_type}
ABCDresult
1X1X1
011X1
00X11
1X011
01011
00X00
1X000
01000
71 (($token_type == KEY || $token_type == BUILTIN_FUNC) && $control_statement_to_exit{$token_data}) ||
72 $control_statement_token_types_to_exit{$token_type}
73 ) {
74 0 ▶ if ($is_exited_by_depth{$depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_exited_by_depth{$depth}) 0 0 0
75 0 ▶ $unreachable_token_by_depth{$depth} //= $token;
Condition0%0/3
$unreachable_token_by_depth{$depth} //= $token
l!l&&r!l&&!r
000
MC/DC0%0/2
$unreachable_token_by_depth{$depth} //= $token
$unreachable_token_by_depth{$depth} $token
Truth table0%0/3
A //= B
A= $unreachable_token_by_depth{$depth}B= $token
ABresult
1X1
011
000
76 0 next;
77 }
78  
79 0 my $before_token = $tokens->[$i-1];
80 0 ▶ if ($conditional_token_types{$before_token->{type}} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($conditional_token_types{$before_token->{"type"}} or $before_token->{"type"} == 60 and $before_token->{"data"} eq "//") 0 0 0
Condition0%0/6
$conditional_token_types{$before_token->{"type"}} or $before_token->{"type"} == 60 and $before_token->{"data"} eq "//"
l!l&&r!l&&!r
000
$before_token->{"type"} == 60 and $before_token->{"data"} eq "//"
!ll&&!rl&&r
000
MC/DC0%0/3
$conditional_token_types{$before_token->{"type"}} or $before_token->{"type"} == 60 and $before_token->{"data"} eq "//"
$conditional_token_types{$before_token->{"type"}} $before_token->{"type"} == 60 $before_token->{"data"} eq "//"
Truth table0%0/4
A or B and C
A= $conditional_token_types{$before_token->{"type"}}B= $before_token->{"type"} == 60C= $before_token->{"data"} eq "//"
ABCresult
1XX1
0111
00X0
0100
81 ($before_token->{type} == DEFAULT_OP && $before_token->{data} eq '//')
82 ) {
83 # if before token is conditional operator, ignore
84 0 next;
85 }
86  
87 0 $is_exited_by_depth{$depth} = 1;
88  
89 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
90 0 $token_type = $token->{type};
91 0 ▶ if ($token_type == SEMI_COLON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 106) 0 0 0
92 0 last;
93 }
94  
95 0 ▶ if ($token_type == IF_STATEMENT || $token_type == UNLESS_STATEMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 96 or $token_type == 99) 0 0 0
Condition0%0/3
$token_type == 96 or $token_type == 99
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 96 or $token_type == 99
$token_type == 96 $token_type == 99
Truth table0%0/3
A or B
A= $token_type == 96B= $token_type == 99
ABresult
1X1
011
000
96 # if postfix conditional statement exists, ignore
97 0 $is_exited_by_depth{$depth} = 0;
98 0 last;
99 }
100 }
101  
102 0 next;
103 }
104  
105 0 ▶ if ($token_type == KEY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 122) 0 0 0
106 0 $token = $tokens->[++$i];
107 0 ▶ if ($token->{type} == COLON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 105) 0 0 0
108 # Label (e.g. FOO:)
109 0 $is_in_ignore_context_by_depth{$depth} = 1;
110 }
111  
112 0 next;
113 }
114  
115 0 ▶ if ($token_type == PACKAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 128) 0 0 0
116 # in other package
117 0 $is_in_ignore_context_by_depth{$depth} = 1;
118 0 next;
119 }
120  
121 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 93 or $token_type == 91 or $token_type == 70 and $token_data eq "no") 0 0 0
Condition0%0/9
$token_type == 93 or $token_type == 91 or $token_type == 70 and $token_data eq "no"
l!l&&r!l&&!r
000
$token_type == 93 or $token_type == 91
l!l&&r!l&&!r
000
$token_type == 70 and $token_data eq "no"
!ll&&!rl&&r
000
MC/DC0%0/4
$token_type == 93 or $token_type == 91 or $token_type == 70 and $token_data eq "no"
$token_type == 93 $token_type == 91 $token_type == 70 $token_data eq "no"
Truth table0%0/5
A or B or C and D
A= $token_type == 93B= $token_type == 91C= $token_type == 70D= $token_data eq "no"
ABCDresult
1XXX1
01XX1
00111
000X0
00100
122 $token_type == USE_DECL || $token_type == OUR_DECL ||
123 ($token_type == BUILTIN_FUNC && $token_data eq 'no')
124 ) {
125 # for compiler phase. Ignore them.
126 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
127 0 $token_type = $token->{type};
128 0 ▶ if ($token_type == SEMI_COLON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 106) 0 0 0
129 0 last;
130 }
131 }
132 0 next;
133 }
134  
135 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 63 or $token_type == 75 and $token_data eq "BEGIN") 0 0 0
Condition0%0/6
$token_type == 63 or $token_type == 75 and $token_data eq "BEGIN"
l!l&&r!l&&!r
000
$token_type == 75 and $token_data eq "BEGIN"
!ll&&!rl&&r
000
MC/DC0%0/3
$token_type == 63 or $token_type == 75 and $token_data eq "BEGIN"
$token_type == 63 $token_type == 75 $token_data eq "BEGIN"
Truth table0%0/4
A or B and C
A= $token_type == 63B= $token_type == 75C= $token_data eq "BEGIN"
ABCresult
1XX1
0111
00X0
0100
136 $token_type == FUNCTION_DECL ||
137 ($token_type == MOD_WORD && $token_data eq 'BEGIN')
138 ) {
139 # for function declare and BEGIN block
140 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
141 0 $token_type = $token->{type};
142 0 ▶ if ($token_type == LEFT_BRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 109) 0 0 0
143 0 last;
144 }
145 }
146  
147 0 $i--; # rewind
148  
149 0 next;
150 }
151  
152 0 ▶ if ($is_exited_by_depth{$depth} && !$is_in_ignore_context_by_depth{$depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_exited_by_depth{$depth} and not $is_in_ignore_context_by_depth{$depth}) 0 0 0
Condition0%0/3
$is_exited_by_depth{$depth} and not $is_in_ignore_context_by_depth{$depth}
!ll&&!rl&&r
000
MC/DC0%0/2
$is_exited_by_depth{$depth} and not $is_in_ignore_context_by_depth{$depth}
$is_exited_by_depth{$depth} not $is_in_ignore_context_by_depth{$depth}
Truth table0%0/3
A and not(B)
A= $is_exited_by_depth{$depth}B= not $is_in_ignore_context_by_depth{$depth}
ABresult
0X0
100
111
153 0 ▶ $unreachable_token_by_depth{$depth} //= $token;
Condition0%0/3
$unreachable_token_by_depth{$depth} //= $token
l!l&&r!l&&!r
000
MC/DC0%0/2
$unreachable_token_by_depth{$depth} //= $token
$unreachable_token_by_depth{$depth} $token
Truth table0%0/3
A //= B
A= $unreachable_token_by_depth{$depth}B= $token
ABresult
1X1
011
000
154 }
155 }
156  
157 # for depth of top, finally
158 0 ▶ if (my $unreachable_token = $unreachable_token_by_depth{$depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $unreachable_token = $unreachable_token_by_depth{$depth}) 0 0 0
159 push @violations, {
160 filename => $file,
161 line => $unreachable_token->{line},
162 0 description => DESC,
163 explanation => EXPL,
164 policy => __PACKAGE__,
165 };
166 }
167  
168 0 return \@violations;
169 }
170  
171 2 1;
172  
« Perl/Lint/Policy/Subroutines/RequireArgUnpacking.pm ↑ Summary Perl/Lint/Policy/ValuesAndExpressions/RequireConstantVersion.pm »