« Perl/Lint/Policy/BuiltinFunctions/ProhibitSleepViaSelect.pm
↑ Summary
Perl/Lint/Policy/Subroutines/ProhibitExcessComplexity.pm »
| 1 | package Perl::Lint::Policy::Variables::ProhibitUnusedVariables; | ||||||||||||||||||||||||||||
| 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::Flatten (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use List::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Perl::Lint::Constants::Type; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use parent "Perl::Lint::Policy"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | DESC => '"%s" is declared but not used.', | |||||||||||||||||||||||||||
| 11 | EXPL => 'Unused variables clutter code and make it harder to read', | ||||||||||||||||||||||||||||
| 12 | 2 | }; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | my %var_token_types = ( | |||||||||||||||||||||||||||
| 15 | &LOCAL_VAR => 1, | ||||||||||||||||||||||||||||
| 16 | &LOCAL_ARRAY_VAR => 1, | ||||||||||||||||||||||||||||
| 17 | &LOCAL_HASH_VAR => 1, | ||||||||||||||||||||||||||||
| 18 | &GLOBAL_VAR => 1, | ||||||||||||||||||||||||||||
| 19 | &GLOBAL_ARRAY_VAR => 1, | ||||||||||||||||||||||||||||
| 20 | &GLOBAL_HASH_VAR => 1, | ||||||||||||||||||||||||||||
| 21 | &VAR => 1, | ||||||||||||||||||||||||||||
| 22 | &ARRAY_VAR => 1, | ||||||||||||||||||||||||||||
| 23 | &HASH_VAR => 1, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub evaluate { | ||||||||||||||||||||||||||
|
Subroutine
evaluate: not called CC=28 SCAR=67.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 0 | my ($class, $file, $tokens, $src, $args) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | my @violations; | |||||||||||||||||||||||||||
| 30 | my $depth; | ||||||||||||||||||||||||||||
| 31 | 0 | my %vars_by_depth; | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | $token_type = $token->{type}; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ($token_type == LEFT_BRACE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $depth++; | |||||||||||||||||||||||||||
| 37 | 0 | next; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($token_type == RIGHT_BRACE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | for my $variable (keys %vars_by_depth) { | |||||||||||||||||||||||||||
| 42 | push @violations, { | ||||||||||||||||||||||||||||
| 43 | filename => $file, | ||||||||||||||||||||||||||||
| 44 | 0 | line => $vars_by_depth{$variable}, | |||||||||||||||||||||||||||
| 45 | description => sprintf(DESC, $variable), | ||||||||||||||||||||||||||||
| 46 | explanation => EXPL, | ||||||||||||||||||||||||||||
| 47 | policy => __PACKAGE__, | ||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | %vars_by_depth = (); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | $depth--; | |||||||||||||||||||||||||||
| 53 | 0 | next; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($token_type == VAR_DECL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | $token = $tokens->[++$i]; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ($token->{type} == LEFT_PAREN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | my $lpnum = 1; | |||||||||||||||||||||||||||
| 60 | 0 | my %vars_in_paren; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | for ($i++; $token = $tokens->[$i]; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $token_type = $token->{type}; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ($token_type == LEFT_PAREN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | $lpnum++; | |||||||||||||||||||||||||||
| 66 | 0 | next; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ($token_type == RIGHT_PAREN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | last if --$lpnum <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | next; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($var_token_types{$token_type}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $vars_in_paren{$token->{data}} = $token->{line}; | |||||||||||||||||||||||||||
| 76 | 0 | next; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | $token = $tokens->[$i+1]; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($token->{type} != ASSIGN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | %vars_by_depth = (%vars_by_depth, %vars_in_paren); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | next; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my $next_token = $tokens->[$i+1]; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($next_token->{type} != ASSIGN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $vars_by_depth{$token->{data}} = $token->{line}; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 0 | next; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($token_type == OUR_DECL || $token_type == LOCAL_DECL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$token_type == 91 or $token_type == 90
MC/DC0%0/2
$token_type == 91 or $token_type == 90
$token_type == 91
$token_type == 90
Truth table0%0/3
A or B
A= $token_type == 91B= $token_type == 90
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | for ($i++; $token = $tokens->[$i]; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($token->{type} == SEMI_COLON) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | last; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | next; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($var_token_types{$token_type}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | my $variable = $token->{data}; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | $token = $tokens->[$i+1]; | |||||||||||||||||||||||||||
| 108 | 0 | $token_type = $token->{type}; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($token_type == LEFT_BRACKET) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 110 | 0 | $variable = '@' . substr $variable, 1; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | elsif ($token_type == LEFT_BRACE) { | ||||||||||||||||||||||||||||
| 113 | 0 | $variable = '%' . substr $variable, 1; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | delete $vars_by_depth{$variable}; | |||||||||||||||||||||||||||
| 117 | 0 | next; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$token_type == 157 or $token_type == 187
MC/DC0%0/2
$token_type == 157 or $token_type == 187
$token_type == 157
$token_type == 187
Truth table0%0/3
A or B
A= $token_type == 157B= $token_type == 187
| |||||||||||||||||||||||||||||
| 121 | $token_type == REG_REPLACE_TO || | ||||||||||||||||||||||||||||
| 122 | $token_type == REG_EXP | ||||||||||||||||||||||||||||
| 123 | ) { | ||||||||||||||||||||||||||||
| 124 | 0 | my $regex = $token->{data}; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | while ($regex =~ /(\\*)([\$\@]\w+[\{\[]?)/gc) { # XXX | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if (length($1) % 2 == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | # not escaped | ||||||||||||||||||||||||||||
| 129 | 0 | my $variable = $2; | |||||||||||||||||||||||||||
| 130 | 0 | my $last_char = substr $variable, -1, 1; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($last_char eq '{') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 132 | 0 | $variable = '%' . substr $variable, 1, -1; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | elsif ($last_char eq '[') { | ||||||||||||||||||||||||||||
| 135 | 0 | $variable = '@' . substr $variable, 1, -1; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | delete $vars_by_depth{$variable}; | |||||||||||||||||||||||||||
| 139 | 0 | next; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||
| 142 | # escaped | ||||||||||||||||||||||||||||
| 143 | 0 | next; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | next; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | for my $variable (keys %vars_by_depth) { | |||||||||||||||||||||||||||
| 152 | push @violations, { | ||||||||||||||||||||||||||||
| 153 | filename => $file, | ||||||||||||||||||||||||||||
| 154 | 0 | line => $vars_by_depth{$variable}, | |||||||||||||||||||||||||||
| 155 | description => sprintf(DESC, $variable), | ||||||||||||||||||||||||||||
| 156 | explanation => EXPL, | ||||||||||||||||||||||||||||
| 157 | policy => __PACKAGE__, | ||||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | return \@violations; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 2 | 1; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||