« blib/lib/Perl/Critic/Policy/Variables/ProhibitReusedNames.pm
↑ Summary
blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitParensWithBuiltins.pm »
| 1 | package Perl::Critic::Policy::BuiltinFunctions::ProhibitShiftRef; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 40 | ▶ | use version 0.77 (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities :classification :language }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 40 | our $VERSION = '1.150'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 40 | Readonly::Scalar my $DESC => q{\shift used}; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 40 | Readonly::Scalar my $EXPL => [165]; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 40 | Readonly::Scalar my $MINIMUM_PERL_VERSION => version->new(5.008008); | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 90 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 75 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 74 | ▶ | sub default_themes { return qw( core bugs tests ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 31 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | sub prepare_to_scan_document { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 31 | ▶ | my ( $self, $document ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_to_scan_document: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # The earliest version tested was 5.8.8 | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 31 | my $version = $document->highest_explicit_perl_version(); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 31 | ▶ | return !$version || $version >= $MINIMUM_PERL_VERSION; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
!$version || $version >= $MINIMUM_PERL_VERSION
MC/DC0%0/2
!$version || $version >= $MINIMUM_PERL_VERSION
!$version
$version >= $MINIMUM_PERL_VERSION
Truth table33%1/3
not(A) || B
A= !$versionB= $version >= $MINIMUM_PERL_VERSION
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | sub violates { | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 332 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=9 SCAR=42.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 332 | ▶ | return if $elem->content() ne 'shift'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $prev = $elem->sprevious_sibling(); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( !$prev ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # If there is no previous token, we are probably nested in a block. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # Grab the statement and see if it's in a block. For simplicity, we | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # assume the block only contains a 'shift' statement, which may not be | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # reliable. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ( my $stmt = $elem->statement ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | my $block = $stmt->parent(); | |||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( $block && $block->isa('PPI::Structure::Block') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$block and $block->isa("PPI::Structure::Block")
MC/DC0%0/2
$block and $block->isa("PPI::Structure::Block")
$block
$block->isa("PPI::Structure::Block")
Truth table0%0/3
A and B
A= $blockB= $block->isa("PPI::Structure::Block")
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $prev = $block->sprevious_sibling(); | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ( $prev && $prev->isa('PPI::Token::Cast') && $prev->content() eq q{\\} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$prev and $prev->isa("PPI::Token::Cast") and $prev->content eq "\\"
$prev and $prev->isa("PPI::Token::Cast")
MC/DC0%0/3
$prev and $prev->isa("PPI::Token::Cast") and $prev->content eq "\\"
$prev
$prev->isa("PPI::Token::Cast")
$prev->content eq "\\"
Truth table0%0/4
A and B and C
A= $prevB= $prev->isa("PPI::Token::Cast")C= $prev->content eq "\\"
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 40 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | Perl::Critic::Policy::BuiltinFunctions::ProhibitShiftRef - Prohibit C<\shift> in code | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head1 AFFILIATION | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | This Policy is part of the core L<Perl::Critic|Perl::Critic> | ||||||||||||||||||||||||||||||||||||||||||
| 86 | distribution. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | Prohibit the use of C<\shift>, as it is associated with bugs in Perl and its | ||||||||||||||||||||||||||||||||||||||||||
| 92 | modules. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =head2 Background | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | Often, C<\shift> is used to create references that act much like an alias. By | ||||||||||||||||||||||||||||||||||||||||||
| 97 | creating an "alias" that is named, the code becomes more readable. For example, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | sub routine { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | my $longstring = \shift; | ||||||||||||||||||||||||||||||||||||||||||
| 101 | print $$longstring; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | is more readable than | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | sub routine { | ||||||||||||||||||||||||||||||||||||||||||
| 107 | print $_[0]; # longstring | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | Unfortunately, this added readability brings with it new and exciting issues, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | detailed in the next section. | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | =head2 Problems with C<\shift> | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | By avoiding C<\shift>, several issues in Perl can be averted, including: | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =item Memory leak since Perl 5.22 | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | Issue #126676 was introduced in Perl 5.21.4 and is triggered when C<\shift> is | ||||||||||||||||||||||||||||||||||||||||||
| 122 | used. The bug has not been resolved as of Perl 5.28. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | In short, the bug causes the ref counter for the aliased variable to be | ||||||||||||||||||||||||||||||||||||||||||
| 125 | incremented when running the subroutine, but it is not subsequently decremented | ||||||||||||||||||||||||||||||||||||||||||
| 126 | after the subroutine returns. In addition to leaking memory, this issue can | ||||||||||||||||||||||||||||||||||||||||||
| 127 | also delay the cleanup of objects until Global Destruction, which can cause | ||||||||||||||||||||||||||||||||||||||||||
| 128 | further issues. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | For more information, see L<https://rt.perl.org/Public/Bug/Display.html?id=126676>. | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =item Devel::Cover crashes | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | A separate, longstanding issue in Devel::Cover (since at least 1.21), causes | ||||||||||||||||||||||||||||||||||||||||||
| 135 | test code to segfault occasionally. This prevents the coverage data from being | ||||||||||||||||||||||||||||||||||||||||||
| 136 | written out, resulting in bad metrics. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | The bug itself isn't actually caused by C<\shift>, instead it shows up in code | ||||||||||||||||||||||||||||||||||||||||||
| 139 | like the following: | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | sub myopen { | ||||||||||||||||||||||||||||||||||||||||||
| 142 | open ${ \$_[0] }, ">test"; | ||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | However, this code would rarely be seen in production. It would more likely | ||||||||||||||||||||||||||||||||||||||||||
| 146 | manifest with C<\shift>, as it does below: | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | sub myopen { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | my $fh = \shift; | ||||||||||||||||||||||||||||||||||||||||||
| 150 | open $$fh, ">test"; | ||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | So while C<\shift> isn't the cause, it's often associated with the problem. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | For more information, see L<https://github.com/pjcj/Devel--Cover/issues/125>. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | This Policy is not configurable except for the standard options. | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | L<https://rt.perl.org/Public/Bug/Display.html?id=126676> | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | L<https://github.com/pjcj/Devel--Cover/issues/125> | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | =for stopwords Lindee | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | Chris Lindee <chris.lindee@cpanel.net> | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =for stopwords cPanel | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | Copyright (c) 2018 cPanel, L.L.C. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 187 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||||||||||||||||