| 1 | package Perl::Critic::Policy::ProhibitPrintSTDERR; | ||
| 2 | $Perl::Critic::Policy::ProhibitPrintSTDERR::VERSION = '1.000'; | ||
| 3 | # ABSTRACT: Print to STDERR throws away the one thing a diagnostic is for. | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | # Not FATAL => 'all', which the house profile otherwise asks for. This is a | ||
| 8 | # library loaded into somebody else's process to critique their code, and a new | ||
| 9 | # warning category in a later perl would then kill their run rather than say | ||
| 10 | # something about it. Fatal warnings are for a program you control the exit of. | ||
| 11 | 1 | ▶ | use warnings; ## no critic (RequireFatalWarnings) |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 1 | ▶ | use re '/aa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | 0 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | |||
| 19 | use Perl::Critic::Utils qw{ :severities :classification :ppi }; | ||
| 20 | use parent qw{Perl::Critic::Policy}; | ||
| 21 | |||
| 22 | |||
| 23 | Readonly::Scalar my $STDERR_BLOCK_RX => qr/\A [{] \s* \\? \s* [*]? \s* STDERR \s* [}] \z/xms; | ||
| 24 | |||
| 25 | Readonly::Scalar my $DESC => q{Print to STDERR}; | ||
| 26 | Readonly::Scalar my $EXPL => q{Use warn, which carries the file and line and goes through $SIG{__WARN__} -- or '## no critic' if this is usage text rather than a diagnostic}; | ||
| 27 | |||
| 28 | # print, and the two other builtins that take a filehandle the same way. | ||
| 29 | Readonly::Hash my %PRINTERS => map { $_ => 1 } qw{print printf say}; | ||
| 30 | |||
| 31 | |||
| 32 | sub supported_parameters { return () } | ||
| 33 | |||
| 34 | |||
| 35 | sub default_severity { return $SEVERITY_MEDIUM } | ||
| 36 | |||
| 37 | |||
| 38 | sub default_themes { return qw(maintenance bugs) } | ||
| 39 | |||
| 40 | |||
| 41 | sub applies_to { return 'PPI::Token::Word' } | ||
| 42 | |||
| 43 | |||
| 44 | sub violates { | ||
| 45 | my ( $self, $elem, undef ) = @_; | ||
| 46 | |||
| 47 | return $self->violation( $DESC, $EXPL, $elem ) if _is_stderr_method_call($elem); | ||
| 48 | |||
| 49 | return if !$PRINTERS{ $elem->content() }; | ||
| 50 | return if !is_function_call($elem); | ||
| 51 | |||
| 52 | my $handle = $elem->snext_sibling(); | ||
| 53 | return if !$handle; | ||
| 54 | |||
| 55 | return $self->violation( $DESC, $EXPL, $elem ) if _is_stderr_handle($handle); | ||
| 56 | |||
| 57 | # print(STDERR "..."), where the parens make the handle the first thing | ||
| 58 | # inside a list rather than the next thing along. | ||
| 59 | return if !$handle->isa('PPI::Structure::List'); | ||
| 60 | |||
| 61 | my $first = $handle->schild(0); | ||
| 62 | $first = $first->schild(0) if $first && $first->isa('PPI::Statement'); | ||
| 63 | |||
| 64 | return $self->violation( $DESC, $EXPL, $elem ) if $first && _is_stderr_handle($first); | ||
| 65 | |||
| 66 | return; # ok! | ||
| 67 | } | ||
| 68 | |||
| 69 | # STDERR->print(...), which reaches print as a method rather than as a builtin | ||
| 70 | # and so never looks like a function call. | ||
| 71 | sub _is_stderr_method_call { | ||
| 72 | my ($elem) = @_; | ||
| 73 | |||
| 74 | return 0 if $elem->content() ne 'print'; | ||
| 75 | |||
| 76 | my $arrow = $elem->sprevious_sibling(); | ||
| 77 | return 0 if !$arrow || !$arrow->isa('PPI::Token::Operator') || $arrow->content() ne '->'; | ||
| 78 | |||
| 79 | my $invocant = $arrow->sprevious_sibling(); | ||
| 80 | return 0 if !$invocant; | ||
| 81 | |||
| 82 | return $invocant->isa('PPI::Token::Word') && $invocant->content() eq 'STDERR'; | ||
| 83 | } | ||
| 84 | |||
| 85 | # The filehandle slot of a print, in each of the shapes PPI hands it back: | ||
| 86 | # a bareword, a glob, or a block holding either. | ||
| 87 | sub _is_stderr_handle { | ||
| 88 | my ($elem) = @_; | ||
| 89 | |||
| 90 | return 1 if $elem->isa('PPI::Token::Word') && $elem->content() eq 'STDERR'; | ||
| 91 | return 1 if $elem->isa('PPI::Token::Symbol') && $elem->content() eq '*STDERR'; | ||
| 92 | return 1 if $elem->isa('PPI::Structure::Block') && $elem->content() =~ $STDERR_BLOCK_RX; | ||
| 93 | |||
| 94 | return 0; | ||
| 95 | } | ||
| 96 | |||
| 97 | 1; | ||
| 98 | |||
| 99 | __END__ | ||