| 1 | package Perl::Critic::Policy::Community::WhileDiamondDefaultAssignment; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = 'v1.0.4'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use constant DESC => '<>/<<>>/readline/readdir/each result not explicitly assigned in while condition'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use constant EXPL => 'When used alone in a while condition, the <>/<<>> operator, readline, readdir, and each functions assign their result to $_, but do not localize it. Assign the result to an explicit lexical variable instead (my $line = <...>, my $dir = readdir ...)'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | sub supported_parameters { () } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 32 | ▶ | sub default_severity { $SEVERITY_HIGH } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub default_themes { 'community' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | ▶ | sub applies_to { 'PPI::Token::Word' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | my %bad_functions = ( | |||||||||||||||||||||||||||||||||||||||||
| 20 | each => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | readdir => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | readline => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 110 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=18 SCAR=29.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 110 | my ($self, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 110 | ▶ | return () unless $elem eq 'while' or $elem eq 'for'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$elem eq "while" or $elem eq "for"
MC/DC100%2/2
$elem eq "while" or $elem eq "for"
$elem eq "while"
$elem eq "for"
Truth table100%3/3
A or B
A= $elem eq "while"B= $elem eq "for"
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 56 | ▶ | my $next = $elem->snext_sibling || return (); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$elem->snext_sibling || (return ())
MC/DC0%0/1
$elem->snext_sibling || (return ())
$elem->snext_sibling
Truth table50%1/2
A
A= $elem->snext_sibling
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # Detect for (;<>;) | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 56 | ▶ | if ($elem eq 'for') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 8 | ▶ | return () unless $next->isa('PPI::Structure::For'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 8 | my @statements = grep { $_->isa('PPI::Statement') } $next->children; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 8 | ▶ | return () unless @statements >= 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 8 | my $middle = $statements[1]; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 8 | ▶ | return $self->violation(DESC, EXPL, $elem) if $middle->schildren | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$middle->schildren and $middle->schild(0)->isa("PPI::Token::QuoteLike::Readline")
MC/DC50%1/2
$middle->schildren and $middle->schild(0)->isa("PPI::Token::QuoteLike::Readline")
$middle->schildren
$middle->schild(0)->isa("PPI::Token::QuoteLike::Readline")
Truth table66%2/3
A and B
A= $middle->schildrenB= $middle->schild(0)->isa("PPI::Token::QuoteLike::Readline")
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | and $middle->schild(0)->isa('PPI::Token::QuoteLike::Readline'); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | } elsif ($elem eq 'while') { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # while (<>) {} or ... while <> | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 48 | ▶ | if ($next->isa('PPI::Structure::Condition')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 24 | $next = $next->schild(0); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 24 | ▶ | return () unless defined $next and $next->isa('PPI::Statement'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $next and $next->isa("PPI::Statement")
MC/DC0%0/2
defined $next and $next->isa("PPI::Statement")
defined $next
$next->isa("PPI::Statement")
Truth table33%1/3
A and B
A= defined $nextB= $next->isa("PPI::Statement")
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 24 | $next = $next->schild(0); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 24 | ▶ | return () unless defined $next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 48 | ▶ | return $self->violation(DESC, EXPL, $elem) if $next->isa('PPI::Token::QuoteLike::Readline'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 40 | ▶ | if ($next->isa('PPI::Token::Word') and exists $bad_functions{$next} and is_function_call $next) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$next->isa("PPI::Token::Word") and exists $bad_functions{$next} and is_function_call($next)
$next->isa("PPI::Token::Word") and exists $bad_functions{$next}
MC/DC66%2/3
$next->isa("PPI::Token::Word") and exists $bad_functions{$next} and is_function_call($next)
$next->isa("PPI::Token::Word")
exists $bad_functions{$next}
is_function_call($next)
Truth table75%3/4
A and B and C
A= $next->isa("PPI::Token::Word")B= exists $bad_functions{$next}C= is_function_call($next)
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 20 | return $self->violation(DESC, EXPL, $elem); | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 24 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | Perl::Critic::Policy::Community::WhileDiamondDefaultAssignment - Don't use | ||||||||||||||||||||||||||||||||||||||||||
| 62 | while with implicit assignment to $_ | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | The diamond operator C<E<lt>E<gt>> (or C<E<lt>E<lt>E<gt>E<gt>>), and functions | ||||||||||||||||||||||||||||||||||||||||||
| 67 | C<readline()>, C<readdir()>, and C<each()> are extra magical in a while | ||||||||||||||||||||||||||||||||||||||||||
| 68 | condition: if it is the only thing in the condition, it will assign its result | ||||||||||||||||||||||||||||||||||||||||||
| 69 | to C<$_>, but it does not localize C<$_> to the while loop. (Note, this also | ||||||||||||||||||||||||||||||||||||||||||
| 70 | applies to a C<for (;E<lt>E<gt>;)> construct.) This can unintentionally confuse | ||||||||||||||||||||||||||||||||||||||||||
| 71 | outer loops that are already using C<$_> to iterate. In addition, using C<$_> | ||||||||||||||||||||||||||||||||||||||||||
| 72 | at all means that your loop can get confused by other code which does not | ||||||||||||||||||||||||||||||||||||||||||
| 73 | politely localize its usage of the global variable. To avoid these | ||||||||||||||||||||||||||||||||||||||||||
| 74 | possibilities, assign the result of the diamond operator or these functions to | ||||||||||||||||||||||||||||||||||||||||||
| 75 | an explicit lexical variable. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | while (<$fh>) { ... } # not ok | ||||||||||||||||||||||||||||||||||||||||||
| 78 | while (<<>>) { ... } # not ok | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ... while <STDIN>; # not ok | ||||||||||||||||||||||||||||||||||||||||||
| 80 | for (;<>;) { ... } # not ok | ||||||||||||||||||||||||||||||||||||||||||
| 81 | while (readline $fh) { ... } # not ok | ||||||||||||||||||||||||||||||||||||||||||
| 82 | while (readdir $dh) { ... } # not ok | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | while (my $line = <$fh>) { ... } # ok | ||||||||||||||||||||||||||||||||||||||||||
| 85 | while (my $line = <<>>) { ... } # ok | ||||||||||||||||||||||||||||||||||||||||||
| 86 | ... while $line = <STDIN>; # ok | ||||||||||||||||||||||||||||||||||||||||||
| 87 | for (;my $line = <>;) { ... } # ok | ||||||||||||||||||||||||||||||||||||||||||
| 88 | while (my $line = readline $fh) { ... } # ok | ||||||||||||||||||||||||||||||||||||||||||
| 89 | while (my $dir = readdir $dh) { ... } # ok | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =head1 AFFILIATION | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | This policy is part of L<Perl::Critic::Community>. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | This policy is not configurable except for the standard options. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | Dan Book, C<dbook@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | Copyright 2015, Dan Book. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | This library is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 108 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | L<Perl::Critic> | ||||||||||||||||||||||||||||||||||||||||||