1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitNoWarnings.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::TestingAndDebugging::ProhibitNoWarnings;
15 use List::MoreUtils qw(all);
17 use Perl::Critic::Utils qw{ :characters :severities :data_conversion };
18 use base 'Perl::Critic::Policy';
20 our $VERSION = '1.088';
22 #-----------------------------------------------------------------------------
24 Readonly::Scalar my $DESC => q{Warnings disabled};
25 Readonly::Scalar my $EXPL => [ 431 ];
27 #-----------------------------------------------------------------------------
29 sub supported_parameters {
33 description => 'Permitted warning categories.',
34 default_string => $EMPTY,
35 parser => \&_parse_allow,
40 sub default_severity { return $SEVERITY_HIGH }
41 sub default_themes { return qw( core bugs pbp ) }
42 sub applies_to { return 'PPI::Statement::Include' }
44 #-----------------------------------------------------------------------------
47 my ($self, $parameter, $config_string) = @_;
51 if( defined $config_string ) {
52 my $allowed = lc $config_string; #String of words
53 my %allowed = hashify( $allowed =~ m/ (\w+) /gmx );
54 $self->{_allow} = \%allowed;
60 #-----------------------------------------------------------------------------
64 my ( $self, $elem, undef ) = @_;
66 return if $elem->type() ne 'no';
67 return if $elem->pragma() ne 'warnings';
69 #Arguments to 'no warnings' are usually a list of literals or a
70 #qw() list. Rather than trying to parse the various PPI elements,
71 #I just use a regex to split the statement into words. This is
72 #kinda lame, but it does the trick for now.
74 # TODO consider: a possible alternate implementation:
75 # my $re = join q{|}, keys %{$self->{allow}};
76 # return if $re && $stmnt =~ m/\b(?:$re)\b/mx;
77 # May need to detaint for that to work... Not sure.
79 my $stmnt = $elem->statement();
81 my @words = $stmnt =~ m/ ([[:lower:]]+) /gmx;
82 @words = grep { $_ ne 'qw' && $_ ne 'no' && $_ ne 'warnings' } @words;
83 return if all { exists $self->{_allow}->{$_} } @words;
85 #If we get here, then it must be a violation
86 return $self->violation( $DESC, $EXPL, $elem );
93 #-----------------------------------------------------------------------------
99 Perl::Critic::Policy::TestingAndDebugging::ProhibitNoWarnings - Prohibit various flavors of C<no warnings>.
103 This Policy is part of the core L<Perl::Critic> distribution.
108 There are good reasons for disabling certain kinds of warnings. But if you
109 were wise enough to C<use warnings> in the first place, then it doesn't make
110 sense to disable them completely. By default, any C<no warnings> statement
111 will violate this policy. However, you can configure this Policy to allow
112 certain types of warnings to be disabled (See L<Configuration>). A bare C<no
113 warnings> statement will always raise a violation.
117 The permitted warning types can be configured via the C<allow> option. The
118 value is a list of whitespace-delimited warning types that you want to be able
119 to disable. See L<perllexwarn> for a list of possible warning types. An
120 example of this customization:
122 [TestingAndDebugging::ProhibitNoWarnings]
123 allow = uninitialized once
127 L<Perl::Critic::Policy::TestingAndDebugging::RequireUseWarnings>
131 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
135 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
137 This program is free software; you can redistribute it and/or modify it under
138 the same terms as Perl itself. The full text of this license can be found in
139 the LICENSE file included with this module
143 ##############################################################################
146 # cperl-indent-level: 4
148 # indent-tabs-mode: nil
149 # c-indentation-style: bsd
151 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :