line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::TooMuchCode::ProhibitUnusedConstant; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
3983
|
use strict; |
|
5
|
|
|
|
|
32
|
|
|
5
|
|
|
|
|
147
|
|
4
|
5
|
|
|
5
|
|
37
|
use warnings; |
|
5
|
|
|
|
|
22
|
|
|
5
|
|
|
|
|
126
|
|
5
|
5
|
|
|
5
|
|
44
|
use Perl::Critic::Utils; |
|
5
|
|
|
|
|
24
|
|
|
5
|
|
|
|
|
88
|
|
6
|
5
|
|
|
5
|
|
7380
|
use PPIx::Utils::Traversal qw(get_constant_name_elements_from_declaring_statement); |
|
5
|
|
|
|
|
61023
|
|
|
5
|
|
|
|
|
325
|
|
7
|
5
|
|
|
5
|
|
52
|
use Scalar::Util qw(refaddr); |
|
5
|
|
|
|
|
18
|
|
|
5
|
|
|
|
|
203
|
|
8
|
5
|
|
|
5
|
|
29
|
use parent 'Perl::Critic::Policy'; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
30
|
|
9
|
|
|
|
|
|
|
|
10
|
5
|
|
|
5
|
|
419
|
use Perl::Critic::TooMuchCode; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
2092
|
|
11
|
|
|
|
|
|
|
|
12
|
0
|
|
|
0
|
1
|
0
|
sub default_themes { return qw( maintenance ) } |
13
|
10
|
|
|
10
|
1
|
114139
|
sub applies_to { return 'PPI::Document' } |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#--------------------------------------------------------------------------- |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub violates { |
18
|
10
|
|
|
10
|
1
|
103
|
my ( $self, $elem, $doc ) = @_; |
19
|
|
|
|
|
|
|
|
20
|
10
|
|
|
|
|
22
|
my %defined_constants; |
21
|
|
|
|
|
|
|
my %used; |
22
|
|
|
|
|
|
|
|
23
|
10
|
|
50
|
330
|
|
52
|
my $include_statements = $elem->find(sub { $_[1]->isa('PPI::Statement::Include') }) || []; |
|
330
|
|
|
|
|
3544
|
|
24
|
10
|
|
|
|
|
167
|
for my $st (@$include_statements) { |
25
|
13
|
50
|
33
|
|
|
52
|
next unless $st->schild(0) eq 'use' && $st->module eq 'constant'; |
26
|
13
|
|
|
|
|
696
|
my @constants = get_constant_name_elements_from_declaring_statement( $st ); |
27
|
13
|
|
|
|
|
1760
|
for my $tok (@constants) { |
28
|
17
|
|
|
|
|
48
|
push @{ $defined_constants{"$tok"} }, $st; |
|
17
|
|
|
|
|
39
|
|
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
10
|
50
|
|
330
|
|
78
|
for my $el_word (@{ $elem->find( sub { $_[1]->isa('PPI::Token::Word') }) ||[]}) { |
|
10
|
|
|
|
|
52
|
|
|
330
|
|
|
|
|
3570
|
|
33
|
58
|
|
|
|
|
539
|
my $st = $el_word->statement; |
34
|
58
|
100
|
|
|
|
712
|
if ($defined_constants{"$el_word"}) { |
35
|
21
|
|
|
|
|
108
|
for my $st (@{ $defined_constants{"$el_word"} }) { |
|
21
|
|
|
|
|
43
|
|
36
|
21
|
100
|
|
|
|
112
|
unless ($el_word->descendant_of($st)) { |
37
|
4
|
|
|
|
|
52
|
$used{"$el_word"}++; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
10
|
|
|
|
|
120
|
Perl::Critic::TooMuchCode::__get_symbol_usage(\%used, $doc); |
44
|
|
|
|
|
|
|
|
45
|
10
|
|
|
|
|
19
|
my @violations; |
46
|
10
|
|
|
|
|
47
|
my @to_report = grep { !$used{$_} } (sort keys %defined_constants); |
|
17
|
|
|
|
|
53
|
|
47
|
10
|
|
|
|
|
28
|
for my $tok (@to_report) { |
48
|
10
|
|
|
|
|
1077
|
for my $el (@{ $defined_constants{$tok} }) { |
|
10
|
|
|
|
|
29
|
|
49
|
10
|
|
|
|
|
46
|
push @violations, $self->violation( 'Unused constant', "A constant <$tok> is defined but not used.", $el ); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
10
|
|
|
|
|
1502
|
return @violations; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
1; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=encoding utf-8 |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 NAME |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
TooMuchCode::ProhibitUnusedConstant -- Find unused constants. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 DESCRIPTION |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
This policy finds constant declarations by "constant" pragma, and further looks to see if they exist in the rest of the code. |
67
|
|
|
|
|
|
|
(The scope of searching is within the same file.) |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
It identifies constants defined in two simple forms, such as: |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
use constant PI => 3.14; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
... and |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
use constant { PI => 3.14, TAU => 6.28 }; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |