1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Variables/ProhibitUnusedVariables.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Variables::ProhibitUnusedVariables;
16 use List::MoreUtils qw< any >;
17 use PPI::Token::Symbol;
19 use Perl::Critic::Utils qw< :characters :severities >;
20 use base 'Perl::Critic::Policy';
22 our $VERSION = '1.088';
24 #-----------------------------------------------------------------------------
26 Readonly::Scalar my $EXPL =>
27 q<Unused variables clutter code and make it harder to read>;
29 #-----------------------------------------------------------------------------
31 sub supported_parameters { return () }
32 sub default_severity { return $SEVERITY_MEDIUM }
33 sub default_themes { return qw< core maintenance > }
34 sub applies_to { return qw< PPI::Document > }
36 #-----------------------------------------------------------------------------
39 my ( $self, $elem, $document ) = @_;
41 my %symbol_usage = _get_symbol_usage($document);
42 return if not %symbol_usage;
44 my $declarations = $document->find('PPI::Statement::Variable');
45 return if not $declarations;
50 foreach my $declaration ( @{$declarations} ) {
51 next DECLARATION if 'my' ne $declaration->type();
53 my @children = $declaration->schildren();
54 next DECLARATION if any { $_ eq q<=> } @children;
57 foreach my $variable ( $declaration->variables() ) {
58 my $count = $symbol_usage{ $variable };
59 next VARIABLE if not $count; # BUG!
60 next VARIABLE if $count > 1;
65 qq<"$variable" is declared but not used.>,
75 sub _get_symbol_usage {
78 my $symbols = $document->find('PPI::Token::Symbol');
79 return if not $symbols;
82 foreach my $symbol ( @{$symbols} ) {
83 $symbol_usage{ $symbol->symbol() }++;
89 #-----------------------------------------------------------------------------
95 #-----------------------------------------------------------------------------
101 Perl::Critic::Policy::Variables::ProhibitUnusedVariables - Don't ask for storage you don't need.
105 This Policy is part of the core L<Perl::Critic> distribution.
110 Unused variables clutter code and require the reader to do mental
111 bookkeeping to figure out if the variable is actually used or not.
113 At present, this Policy is very limited in order to ensure that there
114 aren't any false positives. Hopefully, this will become more
117 Right now, this only looks for simply declared, uninitialized lexical
120 my $x; # not ok, assuming no other appearances.
121 my @y = (); # ok, not handled yet.
122 our $z; # ok, global.
123 local $w; # ok, global.
125 This module is very dumb: it does no scoping detection, i.e. if the
126 same variable name is used in two different locations, even if they
127 aren't the same variable, this Policy won't complain.
129 Have to start somewhere.
134 This Policy is not configurable except for the standard options.
139 Elliot Shank C<< <perl@galumph.com> >>
143 Copyright (c) 2008 Elliot Shank. All rights reserved.
145 This program is free software; you can redistribute it and/or modify
146 it under the same terms as Perl itself. The full text of this license
147 can be found in the LICENSE file included with this module.
153 # cperl-indent-level: 4
155 # indent-tabs-mode: nil
156 # c-indentation-style: bsd
158 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :