1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseWarnings.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::TestingAndDebugging::RequireUseWarnings;
15 use List::Util qw(first);
18 use Perl::Critic::Utils qw{ :severities };
19 use base 'Perl::Critic::Policy';
21 our $VERSION = '1.088';
23 #-----------------------------------------------------------------------------
25 Readonly::Scalar my $DESC => q{Code before warnings are enabled};
26 Readonly::Scalar my $EXPL => [431];
28 Readonly::Scalar my $MINIMUM_VERSION => version->new(5.006);
30 #-----------------------------------------------------------------------------
32 sub supported_parameters { return () }
33 sub default_severity { return $SEVERITY_HIGH }
34 sub default_themes { return qw( core bugs pbp ) }
35 sub applies_to { return 'PPI::Document' }
37 sub default_maximum_violations_per_document { return 1; }
39 #-----------------------------------------------------------------------------
42 my ( $self, $elem, $document ) = @_;
44 my $version = $document->highest_explicit_perl_version();
45 return if $version and $version < $MINIMUM_VERSION;
47 # Find the first 'use warnings' statement
48 my $warn_stmnt = $document->find_first( \&_is_use_warnings );
49 my $warn_line = $warn_stmnt ? $warn_stmnt->location()->[0] : undef;
51 # Find all statements that aren't 'use', 'require', or 'package'
52 my $stmnts_ref = $document->find( \&_isnt_include_or_package );
53 return if !$stmnts_ref;
55 # If the 'use warnings' statement is not defined, or the other
56 # statement appears before the 'use warnings', then it violates.
59 for my $stmnt ( @{ $stmnts_ref } ) {
60 last if $stmnt->isa('PPI::Statement::End');
61 last if $stmnt->isa('PPI::Statement::Data');
63 my $stmnt_line = $stmnt->location()->[0];
64 if ( (! defined $warn_line) || ($stmnt_line < $warn_line) ) {
65 push @viols, $self->violation( $DESC, $EXPL, $stmnt );
71 sub _is_use_warnings {
72 my (undef, $elem) = @_;
74 return 0 if !$elem->isa('PPI::Statement::Include');
75 return 0 if $elem->type() ne 'use';
78 $elem->pragma() ne 'warnings'
79 and $elem->module() ne 'Moose'
80 and $elem->module() ne 'Moose::Role'
88 sub _isnt_include_or_package {
89 my (undef, $elem) = @_;
90 return 0 if ! $elem->isa('PPI::Statement');
91 return 0 if $elem->isa('PPI::Statement::Package');
92 return 0 if $elem->isa('PPI::Statement::Include');
100 #-----------------------------------------------------------------------------
106 Perl::Critic::Policy::TestingAndDebugging::RequireUseWarnings - Always C<use warnings>.
111 This Policy is part of the core L<Perl::Critic> distribution.
116 Using warnings, and paying attention to what they say, is probably the
117 single most effective way to improve the quality of your code. This
118 policy requires that the C<'use warnings'> statement must come before
119 any other statements except C<package>, C<require>, and other C<use>
120 statements. Thus, all the code in the entire package will be
123 There are special exemptions for L<Moose> and L<Moose::Role> because
124 they enforces strictness; e.g. C<'use Moose'> is treated as equivalent
125 to C<'use warnings'>.
127 This policy will not complain if the file explicitly states that it is
128 compatible with a version of perl prior to 5.6 via an include
129 statement, e.g. by having C<require 5.005> in it.
131 The maximum number of violations per document for this policy defaults
137 This Policy is not configurable except for the standard options.
142 L<Perl::Critic::Policy::TestingAndDebugging::ProhibitNoWarnings>
147 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
152 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
154 This program is free software; you can redistribute it and/or modify
155 it under the same terms as Perl itself. The full text of this license
156 can be found in the LICENSE file included with this module
160 ##############################################################################
163 # cperl-indent-level: 4
165 # indent-tabs-mode: nil
166 # c-indentation-style: bsd
168 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :