| 1 | package Perl::Critic::Policy::Variables::ProhibitUselessInitialization; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Perl::Critic::Utils ':severities'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | 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 | 5 | ▶ | sub desc { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | '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 | 5 | ▶ | sub expl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
expl: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 5 | 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 | 11 | ▶ | sub supported_parameters { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 11 | 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 | 5 | ▶ | sub default_severity { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | 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 | 0 | ▶ | sub default_themes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 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 | 11 | ▶ | sub applies_to { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 11 | 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 | 12 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 12 | my ($self, $elem, undef) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 12 | ▶ | if ($elem->type() eq 'my') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 12 | ▶ | if (violates_scalar($elem) || violates_list($elem)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
violates_scalar($elem) or violates_list($elem)
MC/DC100%2/2
violates_scalar($elem) or violates_list($elem)
violates_scalar($elem)
violates_list($elem)
Truth table100%3/3
A or B
A= violates_scalar($elem)B= violates_list($elem)
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 5 | return $self->violation(desc(), expl(), $elem); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 7 | 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 | 12 | ▶ | sub violates_scalar { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates_scalar: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 12 | my $elem = shift; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 12 | my @c = $elem->schildren; # "significant" children | |||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # e.g. (my $x = undef) | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 12 | ▶ | return unless $c[1]->isa('PPI::Token::Symbol') && $c[1]->raw_type eq q{$}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$c[1]->isa("PPI::Token::Symbol") and $c[1]->raw_type eq "\$"
MC/DC50%1/2
$c[1]->isa("PPI::Token::Symbol") and $c[1]->raw_type eq "\$"
$c[1]->isa("PPI::Token::Symbol")
$c[1]->raw_type eq "\$"
Truth table66%2/3
A and B
A= $c[1]->isa("PPI::Token::Symbol")B= $c[1]->raw_type eq "\$"
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 6 | ▶ | return unless $c[2] && $c[2]->isa('PPI::Token::Operator') && $c[2] eq q{=}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$c[2] and $c[2]->isa("PPI::Token::Operator") and $c[2] eq "="
$c[2] and $c[2]->isa("PPI::Token::Operator")
MC/DC33%1/3
$c[2] and $c[2]->isa("PPI::Token::Operator") and $c[2] eq "="
$c[2]
$c[2]->isa("PPI::Token::Operator")
$c[2] eq "="
Truth table50%2/4
A and B and C
A= $c[2]B= $c[2]->isa("PPI::Token::Operator")C= $c[2] eq "="
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 4 | ▶ | return unless $c[3]->isa('PPI::Token::Word') && $c[3] eq q{undef}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$c[3]->isa("PPI::Token::Word") and $c[3] eq "undef"
MC/DC50%1/2
$c[3]->isa("PPI::Token::Word") and $c[3] eq "undef"
$c[3]->isa("PPI::Token::Word")
$c[3] eq "undef"
Truth table66%2/3
A and B
A= $c[3]->isa("PPI::Token::Word")B= $c[3] eq "undef"
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | #return unless $c[4]->isa('PPI::Token::Structure') && $c[4] eq q{;}; | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | 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 | 10 | ▶ | sub violates_list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates_list: called CC=8 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 10 | my $elem = shift; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 10 | my @c = $elem->schildren; # "significant" children | |||||||||||||||||||||||||||||||||||||||||
| 156 | 10 | ▶ | return unless $c[1]->isa('PPI::Token::Symbol') && $c[1]->raw_type =~ /^[@%]$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$c[1]->isa("PPI::Token::Symbol") and $c[1]->raw_type =~ /^[\@%]$/
MC/DC50%1/2
$c[1]->isa("PPI::Token::Symbol") and $c[1]->raw_type =~ /^[\@%]$/
$c[1]->isa("PPI::Token::Symbol")
$c[1]->raw_type =~ /^[\@%]$/
Truth table66%2/3
A and B
A= $c[1]->isa("PPI::Token::Symbol")B= $c[1]->raw_type =~ /^[\@%]$/
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 6 | ▶ | return unless $c[2] && $c[2]->isa('PPI::Token::Operator') && $c[2] eq q{=}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$c[2] and $c[2]->isa("PPI::Token::Operator") and $c[2] eq "="
$c[2] and $c[2]->isa("PPI::Token::Operator")
MC/DC0%0/3
$c[2] and $c[2]->isa("PPI::Token::Operator") and $c[2] eq "="
$c[2]
$c[2]->isa("PPI::Token::Operator")
$c[2] eq "="
Truth table25%1/4
A and B and C
A= $c[2]B= $c[2]->isa("PPI::Token::Operator")C= $c[2] eq "="
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 6 | ▶ | return unless $c[3]->isa('PPI::Structure::List') && $c[3] eq q{()}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c[3]->isa("PPI::Structure::List") and $c[3] eq "()"
MC/DC100%2/2
$c[3]->isa("PPI::Structure::List") and $c[3] eq "()"
$c[3]->isa("PPI::Structure::List")
$c[3] eq "()"
Truth table100%3/3
A and B
A= $c[3]->isa("PPI::Structure::List")B= $c[3] eq "()"
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | #return unless $c[4]->isa('PPI::Token::Structure') && $c[4] eq q{;}; | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 3 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||