| 1 | package Perl::Critic::Policy::RequireFatalWarnings; | ||
| 2 | $Perl::Critic::Policy::RequireFatalWarnings::VERSION = '1.000'; | ||
| 3 | # ABSTRACT: A warning nobody reads is a bug you ship. | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use re '/aa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 0 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use Perl::Critic::Utils qw{ :severities :classification :ppi }; | ||
| 15 | use parent qw{Perl::Critic::Policy}; | ||
| 16 | |||
| 17 | |||
| 18 | Readonly::Scalar my $DESC => q{Warnings are enabled but not fatal}; | ||
| 19 | Readonly::Scalar my $EXPL => q{Use 'use warnings FATAL => "all";' so a warning stops the run where the bug is}; | ||
| 20 | |||
| 21 | |||
| 22 | sub supported_parameters { | ||
| 23 | return ({ | ||
| 24 | name => 'equivalent_modules', | ||
| 25 | description => 'Modules that switch fatal warnings on for you.', | ||
| 26 | default_string => 'Moose Moo Mouse strictures Test2::V0', | ||
| 27 | behavior => 'string list', | ||
| 28 | }); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub default_severity { return $SEVERITY_MEDIUM } | ||
| 32 | sub default_themes { return qw(bugs maintenance) } | ||
| 33 | sub applies_to { return 'PPI::Document' } | ||
| 34 | |||
| 35 | |||
| 36 | sub violates { | ||
| 37 | my ( $self, $elem, $document ) = @_; | ||
| 38 | |||
| 39 | my $includes = $document->find('PPI::Statement::Include') || []; | ||
| 40 | |||
| 41 | my $enabling; | ||
| 42 | foreach my $include (@$includes) { | ||
| 43 | next if $include->type() ne 'use'; | ||
| 44 | |||
| 45 | my $module = $include->module(); | ||
| 46 | next if !defined $module; | ||
| 47 | |||
| 48 | return if $self->{_equivalent_modules}{$module}; | ||
| 49 | next if $module ne 'warnings'; | ||
| 50 | |||
| 51 | return if _is_fatal($include); # somebody already said so | ||
| 52 | $enabling //= $include; | ||
| 53 | } | ||
| 54 | |||
| 55 | # Point at the `use warnings` that should have been fatal, or at the top of | ||
| 56 | # the file when there is nothing to point at. | ||
| 57 | my $anchor = $enabling // $document->schild(0) // $document; | ||
| 58 | return $self->violation( $DESC, $EXPL, $anchor ); | ||
| 59 | } | ||
| 60 | |||
| 61 | # `use warnings FATAL => 'all'`, in any of its spellings. | ||
| 62 | sub _is_fatal { | ||
| 63 | my ($include) = @_; | ||
| 64 | |||
| 65 | my @words; | ||
| 66 | foreach my $token ( $include->schildren() ) { | ||
| 67 | if ( $token->isa('PPI::Token::QuoteLike::Words') ) { | ||
| 68 | push @words, $token->literal(); | ||
| 69 | } | ||
| 70 | elsif ( $token->isa('PPI::Token::Quote') ) { | ||
| 71 | push @words, $token->string(); | ||
| 72 | } | ||
| 73 | elsif ( $token->isa('PPI::Token::Word') ) { | ||
| 74 | push @words, $token->content(); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | my $fatal; | ||
| 79 | foreach my $word (@words) { | ||
| 80 | next if !defined $word; | ||
| 81 | $fatal = 1 if $word eq 'FATAL'; | ||
| 82 | return 1 if $fatal && $word eq 'all'; | ||
| 83 | } | ||
| 84 | |||
| 85 | return 0; | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | __END__ | ||