line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::Variables::ProhibitUselessInitialization; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
512349
|
use strict; |
|
3
|
|
|
|
|
39
|
|
|
3
|
|
|
|
|
107
|
|
4
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
88
|
|
5
|
3
|
|
|
3
|
|
14
|
use base 'Perl::Critic::Policy'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
3085
|
|
6
|
3
|
|
|
3
|
|
320108
|
use Perl::Critic::Utils ':severities'; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
227
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Perl::Critic::Policy::Variables::ProhibitUselessInitialization - prohibit superfluous initializations |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 DESCRIPTION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Don't clutter your code with unnecessary variable initialization: |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $scalar = undef; # don't do this |
19
|
|
|
|
|
|
|
my @array = (); # or this |
20
|
|
|
|
|
|
|
my %hash = (); # or this |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Instead, do this: |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $scalar; # equivalent |
25
|
|
|
|
|
|
|
my @array; # ditto |
26
|
|
|
|
|
|
|
my %hash; # isn't that better? |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 AUTHOR |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
John Trammell <johntrammell -at- gmail -dot- com> |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 COPYRIGHT |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Copyright (c) John Joseph Trammell. All rights reserved. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
37
|
|
|
|
|
|
|
it under the same terms as Perl itself. The full text of this license |
38
|
|
|
|
|
|
|
can be found in the LICENSE file included with this module. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head2 desc() |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Returns a string containing a sort description of this policy. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub desc { |
49
|
5
|
|
|
5
|
1
|
22
|
'Useless variable initialization'; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head2 expl() |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Returns a string containing an explanation of this policy. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub expl { |
60
|
5
|
|
|
5
|
1
|
45
|
q{Don't clutter your code with unnecessary variable initializations}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 supported_parameters |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Define parameters supported by this policy. There are none. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub supported_parameters { |
70
|
11
|
|
|
11
|
1
|
71488
|
return (); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 default_severity |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Returns a numeric constant defining the severity of violating this policy. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub default_severity { |
80
|
5
|
|
|
5
|
1
|
69
|
return $SEVERITY_LOW; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head2 default_themes |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Returns a list of strings defining the themes for this policy. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub default_themes { |
90
|
0
|
|
|
0
|
1
|
0
|
return qw(petpeeves JTRAMMELL); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 applies_to |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Returns a string describing the elements to which this policy applies. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub applies_to { |
100
|
11
|
|
|
11
|
1
|
63454
|
return 'PPI::Statement::Variable'; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 violates |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Method to determine if the element currently under scrutiny violates this |
106
|
|
|
|
|
|
|
policy. If it does, return a properly constructed C<Perl::Critic::Violation> |
107
|
|
|
|
|
|
|
object. Otherwise, return C<undef>. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub violates { |
112
|
12
|
|
|
12
|
1
|
226
|
my ($self, $elem, undef) = @_; |
113
|
12
|
50
|
|
|
|
67
|
if ($elem->type() eq 'my') { |
114
|
12
|
100
|
100
|
|
|
542
|
if (violates_scalar($elem) || violates_list($elem)) { |
115
|
5
|
|
|
|
|
20
|
return $self->violation(desc(), expl(), $elem); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
7
|
|
|
|
|
327
|
return; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head2 violates_scalar($elem) |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Returns true if C<$elem> contains an assignment of the form |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
my $foo = undef; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
See L<http://search.cpan.org/dist/PPI/lib/PPI/Statement/Variable.pm> for |
128
|
|
|
|
|
|
|
details on how this function works. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub violates_scalar { |
133
|
12
|
|
|
12
|
1
|
21
|
my $elem = shift; |
134
|
12
|
|
|
|
|
51
|
my @c = $elem->schildren; # "significant" children |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# e.g. (my $x = undef) |
137
|
12
|
100
|
66
|
|
|
341
|
return unless $c[1]->isa('PPI::Token::Symbol') && $c[1]->raw_type eq q{$}; |
138
|
6
|
50
|
66
|
|
|
102
|
return unless $c[2] && $c[2]->isa('PPI::Token::Operator') && $c[2] eq q{=}; |
|
|
|
66
|
|
|
|
|
139
|
4
|
100
|
66
|
|
|
90
|
return unless $c[3]->isa('PPI::Token::Word') && $c[3] eq q{undef}; |
140
|
|
|
|
|
|
|
#return unless $c[4]->isa('PPI::Token::Structure') && $c[4] eq q{;}; |
141
|
2
|
|
|
|
|
31
|
return 1; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 violates_list($elem) |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Returns true if C<$elem> contains an assignment of the forms: |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my @foo = (); # useless array init |
149
|
|
|
|
|
|
|
my %bar = (); # useless hash init |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub violates_list { |
154
|
10
|
|
|
10
|
1
|
168
|
my $elem = shift; |
155
|
10
|
|
|
|
|
34
|
my @c = $elem->schildren; # "significant" children |
156
|
10
|
100
|
66
|
|
|
169
|
return unless $c[1]->isa('PPI::Token::Symbol') && $c[1]->raw_type =~ /^[@%]$/; |
157
|
6
|
50
|
33
|
|
|
128
|
return unless $c[2] && $c[2]->isa('PPI::Token::Operator') && $c[2] eq q{=}; |
|
|
|
33
|
|
|
|
|
158
|
6
|
100
|
100
|
|
|
142
|
return unless $c[3]->isa('PPI::Structure::List') && $c[3] eq q{()}; |
159
|
|
|
|
|
|
|
#return unless $c[4]->isa('PPI::Token::Structure') && $c[4] eq q{;}; |
160
|
3
|
|
|
|
|
132
|
return 1; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |