« blib/lib/Perl/Lint/Policy/InputOutput/RequireBriefOpen.pm
↑ Summary
blib/lib/Perl/Lint/Policy/InputOutput/RequireCheckedOpen.pm »
| 1 | package Perl::Lint::Policy::InputOutput::RequireCheckedClose; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Perl::Lint::Constants::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use Perl::Lint::Constants::Kind; | ||
| 6 | use parent "Perl::Lint::Policy"; | ||
| 7 | |||
| 8 | use constant { | ||
| 9 | DESC => 'Return value of "close" ignored', | ||
| 10 | EXPL => 'Check the return value of "close" for success', | ||
| 11 | }; | ||
| 12 | |||
| 13 | sub evaluate { | ||
| 14 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 15 | |||
| 16 | my $is_in_assign_context = 0; | ||
| 17 | my $is_in_statement_context = 0; | ||
| 18 | my $is_called_close_in_void = 0; | ||
| 19 | my $is_enabled_autodie = 0; | ||
| 20 | my @violations; | ||
| 21 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 22 | my $token_type = $token->{type}; | ||
| 23 | my $token_kind = $token->{kind}; | ||
| 24 | my $token_data = $token->{data}; | ||
| 25 | |||
| 26 | if ($token_type == ASSIGN) { | ||
| 27 | $is_in_assign_context = 1; | ||
| 28 | next; | ||
| 29 | } | ||
| 30 | |||
| 31 | if ($token_type == USED_NAME) { | ||
| 32 | if ($token_data eq 'Fatal') { | ||
| 33 | my $next_token = $tokens->[$i+1]; | ||
| 34 | my $next_token_type = $next_token->{type}; | ||
| 35 | if ($next_token_type == REG_LIST) { | ||
| 36 | for ($i += 3; my $token = $tokens->[$i]; $i++) { | ||
| 37 | my $token_type = $token->{type}; | ||
| 38 | if ($token_type == REG_EXP && $token->{data} eq 'close') { | ||
| 39 | return []; | ||
| 40 | } | ||
| 41 | elsif ($token_type == REG_DELIM) { | ||
| 42 | last; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | elsif ($next_token_type == LEFT_PAREN) { | ||
| 47 | my $left_paren_num = 1; | ||
| 48 | for ($i += 2; my $token = $tokens->[$i]; $i++) { | ||
| 49 | my $token_type = $token->{type}; | ||
| 50 | if ($token_type == LEFT_PAREN) { | ||
| 51 | $left_paren_num++; | ||
| 52 | } | ||
| 53 | elsif (($token_type == STRING || $token_type == RAW_STRING) && $token->{data} eq 'close') { | ||
| 54 | return []; | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | last if --$left_paren_num <= 0; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | elsif (($next_token_type == STRING || $next_token_type == RAW_STRING) && $next_token->{data} eq 'close') { | ||
| 62 | last; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | elsif ($token_data eq 'autodie') { | ||
| 66 | if ($tokens->[$i+1]->{type} == REG_LIST) { | ||
| 67 | for ($i += 3; my $token = $tokens->[$i]; $i++) { | ||
| 68 | my $token_type = $token->{type}; | ||
| 69 | if ($token_type == REG_EXP && $token->{data} =~ /\A\s*:io\s*\Z/) { | ||
| 70 | $is_enabled_autodie = 1; | ||
| 71 | } | ||
| 72 | elsif ($token_type == REG_DELIM) { | ||
| 73 | last; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | $is_enabled_autodie = 1; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | next; | ||
| 83 | } | ||
| 84 | |||
| 85 | if ($token_type == NAMESPACE && $token_data eq 'Fatal') { | ||
| 86 | my $skipped_token = $tokens->[$i+2]; | ||
| 87 | if ($skipped_token && $skipped_token->{type} == NAMESPACE && $skipped_token->{data} eq 'Exception') { | ||
| 88 | for ($i += 3; my $token = $tokens->[$i]; $i++) { | ||
| 89 | my $token_type = $token->{type}; | ||
| 90 | if ($token_type == REG_LIST) { | ||
| 91 | for ($i += 2; my $token = $tokens->[$i]; $i++) { | ||
| 92 | my $token_type = $token->{type}; | ||
| 93 | if ($token_type == REG_EXP && $token->{data} eq 'close') { | ||
| 94 | return []; | ||
| 95 | } | ||
| 96 | elsif ($token_type == REG_DELIM) { | ||
| 97 | last; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | elsif ($token_type == LEFT_PAREN) { | ||
| 102 | my $left_paren_num = 1; | ||
| 103 | for ($i++; my $token = $tokens->[$i]; $i++) { | ||
| 104 | my $token_type = $token->{type}; | ||
| 105 | if ($token_type == LEFT_PAREN) { | ||
| 106 | $left_paren_num++; | ||
| 107 | } | ||
| 108 | elsif (($token_type == STRING || $token_type == RAW_STRING) && $token->{data} eq 'close') { | ||
| 109 | return []; | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | last if --$left_paren_num <= 0; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | elsif (($token_type == STRING || $token_type == RAW_STRING) && $token->{data} eq 'close') { | ||
| 117 | last; | ||
| 118 | } | ||
| 119 | elsif ($token->{kind} == KIND_STMT_END) { | ||
| 120 | last; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | next; | ||
| 126 | } | ||
| 127 | |||
| 128 | if ($token_kind == KIND_STMT) { | ||
| 129 | $is_in_statement_context = 1; | ||
| 130 | |||
| 131 | if ($tokens->[$i+1]->{type} == LEFT_PAREN) { | ||
| 132 | $i++; | ||
| 133 | my $left_paren_num = 1; | ||
| 134 | for ($i++; my $token = $tokens->[$i]; $i++) { | ||
| 135 | my $token_type = $token->{type}; | ||
| 136 | if ($token_type == LEFT_PAREN) { | ||
| 137 | $left_paren_num++; | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | last if --$left_paren_num <= 0; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | next; | ||
| 145 | } | ||
| 146 | |||
| 147 | if ($token_type == BUILTIN_FUNC) { | ||
| 148 | if ($token_data eq 'close') { | ||
| 149 | if (!$is_in_assign_context && !$is_in_statement_context) { | ||
| 150 | $is_called_close_in_void = 1; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | elsif ($token_data eq 'no') { | ||
| 154 | my $next_token = $tokens->[++$i]; | ||
| 155 | if ($next_token->{type} == KEY && $next_token->{data} eq 'autodie') { | ||
| 156 | $is_enabled_autodie = 0; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | next; | ||
| 160 | } | ||
| 161 | |||
| 162 | if ($token_kind == KIND_OP) { | ||
| 163 | $is_called_close_in_void = 0; | ||
| 164 | next; | ||
| 165 | } | ||
| 166 | |||
| 167 | if ($token_kind == KIND_STMT_END) { | ||
| 168 | next if $is_enabled_autodie; | ||
| 169 | |||
| 170 | if ($is_called_close_in_void) { | ||
| 171 | push @violations, { | ||
| 172 | filename => $file, | ||
| 173 | line => $token->{line}, | ||
| 174 | description => DESC, | ||
| 175 | explanation => EXPL, | ||
| 176 | policy => __PACKAGE__, | ||
| 177 | }; | ||
| 178 | } | ||
| 179 | |||
| 180 | $is_in_assign_context = 0; | ||
| 181 | $is_in_statement_context = 0; | ||
| 182 | $is_called_close_in_void = 0; | ||
| 183 | next; | ||
| 184 | } | ||
| 185 | |||
| 186 | } | ||
| 187 | |||
| 188 | return \@violations; | ||
| 189 | } | ||
| 190 | |||
| 191 | 1; | ||
| 192 | |||