Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / Variables / ProhibitUnusedVariables.pm
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) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::Variables::ProhibitUnusedVariables;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13
14 use Readonly;
15
16 use List::MoreUtils qw< any >;
17 use PPI::Token::Symbol;
18
19 use Perl::Critic::Utils qw< :characters :severities >;
20 use base 'Perl::Critic::Policy';
21
22 our $VERSION = '1.088';
23
24 #-----------------------------------------------------------------------------
25
26 Readonly::Scalar my $EXPL =>
27     q<Unused variables clutter code and make it harder to read>;
28
29 #-----------------------------------------------------------------------------
30
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 >    }
35
36 #-----------------------------------------------------------------------------
37
38 sub violates {
39     my ( $self, $elem, $document ) = @_;
40
41     my %symbol_usage = _get_symbol_usage($document);
42     return if not %symbol_usage;
43
44     my $declarations = $document->find('PPI::Statement::Variable');
45     return if not $declarations;
46
47     my @violations;
48
49     DECLARATION:
50     foreach my $declaration ( @{$declarations} ) {
51         next DECLARATION if 'my' ne $declaration->type();
52
53         my @children = $declaration->schildren();
54         next DECLARATION if any { $_ eq q<=> } @children;
55
56         VARIABLE:
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;
61
62             push
63                 @violations,
64                 $self->violation(
65                     qq<"$variable" is declared but not used.>,
66                     $EXPL,
67                     $declaration,
68                 );
69         }
70     }
71
72     return @violations;
73 }
74
75 sub _get_symbol_usage {
76     my ($document) = @_;
77
78     my $symbols = $document->find('PPI::Token::Symbol');
79     return if not $symbols;
80
81     my %symbol_usage;
82     foreach my $symbol ( @{$symbols} ) {
83         $symbol_usage{ $symbol->symbol() }++;
84     }
85
86     return %symbol_usage;
87 }
88
89 #-----------------------------------------------------------------------------
90
91 1;
92
93 __END__
94
95 #-----------------------------------------------------------------------------
96
97 =pod
98
99 =head1 NAME
100
101 Perl::Critic::Policy::Variables::ProhibitUnusedVariables - Don't ask for storage you don't need.
102
103 =head1 AFFILIATION
104
105 This Policy is part of the core L<Perl::Critic> distribution.
106
107
108 =head1 DESCRIPTION
109
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.
112
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
115 sophisticated soon.
116
117 Right now, this only looks for simply declared, uninitialized lexical
118 variables.
119
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.
124
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.
128
129 Have to start somewhere.
130
131
132 =head1 CONFIGURATION
133
134 This Policy is not configurable except for the standard options.
135
136
137 =head1 AUTHOR
138
139 Elliot Shank C<< <perl@galumph.com> >>
140
141 =head1 COPYRIGHT
142
143 Copyright (c) 2008 Elliot Shank.  All rights reserved.
144
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.
148
149 =cut
150
151 # Local Variables:
152 #   mode: cperl
153 #   cperl-indent-level: 4
154 #   fill-column: 78
155 #   indent-tabs-mode: nil
156 #   c-indentation-style: bsd
157 # End:
158 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :