This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
POD nits on B::Lint
[perl5.git] / ext / B / B / Lint.pm
CommitLineData
a798dbf2
MB
1package B::Lint;
2
ca0b1549 3our $VERSION = '1.04';
28b605d8 4
a798dbf2
MB
5=head1 NAME
6
7B::Lint - Perl lint
8
9=head1 SYNOPSIS
10
11perl -MO=Lint[,OPTIONS] foo.pl
12
13=head1 DESCRIPTION
14
15The B::Lint module is equivalent to an extended version of the B<-w>
c00253d5 16option of B<perl>. It is named after the program F<lint> which carries
a798dbf2
MB
17out a similar process for C programs.
18
19=head1 OPTIONS AND LINT CHECKS
20
21Option words are separated by commas (not whitespace) and follow the
22usual conventions of compiler backend options. Following any options
23(indicated by a leading B<->) come lint check arguments. Each such
24argument (apart from the special B<all> and B<none> options) is a
25word representing one possible lint check (turning on that check) or
26is B<no-foo> (turning off that check). Before processing the check
27arguments, a standard list of checks is turned on. Later options
28override earlier ones. Available options are:
29
30=over 8
31
32=item B<context>
33
34Produces a warning whenever an array is used in an implicit scalar
35context. For example, both of the lines
36
37 $foo = length(@bar);
38 $foo = @bar;
c00253d5 39
a798dbf2
MB
40will elicit a warning. Using an explicit B<scalar()> silences the
41warning. For example,
42
43 $foo = scalar(@bar);
44
45=item B<implicit-read> and B<implicit-write>
46
47These options produce a warning whenever an operation implicitly
48reads or (respectively) writes to one of Perl's special variables.
49For example, B<implicit-read> will warn about these:
50
51 /foo/;
52
53and B<implicit-write> will warn about these:
54
55 s/foo/bar/;
56
57Both B<implicit-read> and B<implicit-write> warn about this:
58
59 for (@a) { ... }
60
40f1df11
RGS
61=item B<bare-subs>
62
63This option warns whenever a bareword is implicitly quoted, but is also
64the name of a subroutine in the current package. Typical mistakes that it will
65trap are:
66
c00253d5
AL
67 use constant foo => 'bar';
68 @a = ( foo => 1 );
69 $b{foo} = 2;
40f1df11
RGS
70
71Neither of these will do what a naive user would expect.
72
a798dbf2
MB
73=item B<dollar-underscore>
74
c00253d5 75This option warns whenever C<$_> is used either explicitly anywhere or
a798dbf2
MB
76as the implicit argument of a B<print> statement.
77
78=item B<private-names>
79
80This option warns on each use of any variable, subroutine or
81method name that lives in a non-current package but begins with
82an underscore ("_"). Warnings aren't issued for the special case
c00253d5 83of the single character name "_" by itself (e.g. C<$_> and C<@_>).
a798dbf2
MB
84
85=item B<undefined-subs>
86
87This option warns whenever an undefined subroutine is invoked.
88This option will only catch explicitly invoked subroutines such
89as C<foo()> and not indirect invocations such as C<&$subref()>
90or C<$obj-E<gt>meth()>. Note that some programs or modules delay
91definition of subs until runtime by means of the AUTOLOAD
92mechanism.
93
94=item B<regexp-variables>
95
c00253d5
AL
96This option warns whenever one of the regexp variables C<$`>, C<$&> or C<$'>
97is used. Any occurrence of any of these variables in your
a798dbf2
MB
98program can slow your whole program down. See L<perlre> for
99details.
100
101=item B<all>
102
103Turn all warnings on.
104
105=item B<none>
106
107Turn all warnings off.
108
109=back
110
111=head1 NON LINT-CHECK OPTIONS
112
113=over 8
114
115=item B<-u Package>
116
117Normally, Lint only checks the main code of the program together
118with all subs defined in package main. The B<-u> option lets you
119include other package names whose subs are then checked by Lint.
120
121=back
122
ca0b1549
JJ
123=head1 EXTENDING LINT
124
125Lint can be extended by registering plugins.
126
fa75652c 127The C<< B::Lint->register_plugin( MyPlugin => \@new_checks ) >> method
ca0b1549
JJ
128adds the class C<MyPlugin> to the list of plugins. It also adds the
129list of C<@new_checks> to the list of valid checks.
130
fa75652c
RGS
131You must create a C<match( \%checks )> method in your plugin class or one
132of its parents. It will be called on every op as a regular method call
133with a hash ref of checks as its parameter.
ca0b1549 134
fa75652c 135You may not alter the %checks hash reference.
ca0b1549 136
fa75652c
RGS
137The class methods C<< B::Lint->file >> and C<< B::Lint->line >> contain
138the current filename and line number.
ca0b1549 139
fa75652c
RGS
140 package Sample;
141 use B::Lint;
142 B::Lint->register_plugin( Sample => [ 'good_taste' ] );
143
144 sub match {
145 my ( $op, $checks_href ) = shift;
146 if ( $checks_href->{good_taste} ) {
147 ...
148 }
149 }
ca0b1549 150
a798dbf2
MB
151=head1 BUGS
152
153This is only a very preliminary version.
154
94011a57
JH
155This module doesn't work correctly on thread-enabled perls.
156
a798dbf2
MB
157=head1 AUTHOR
158
159Malcolm Beattie, mbeattie@sable.ox.ac.uk.
160
161=cut
162
163use strict;
b0a2b4f5 164use B qw(walkoptree_slow main_root walksymtable svref_2object parents
ca0b1549 165 class
40f1df11 166 OPf_WANT_LIST OPf_WANT OPf_STACKED G_ARRAY SVf_POK
4c1f658f 167 );
a798dbf2
MB
168
169my $file = "unknown"; # shadows current filename
170my $line = 0; # shadows current line number
171my $curstash = "main"; # shadows current stash
172
ca0b1549
JJ
173sub file { $file }
174sub line { $line }
175
a798dbf2
MB
176# Lint checks
177my %check;
178my %implies_ok_context;
179BEGIN {
180 map($implies_ok_context{$_}++,
3f872cb9
GS
181 qw(scalar av2arylen aelem aslice helem hslice
182 keys values hslice defined undef delete));
a798dbf2
MB
183}
184
185# Lint checks turned on by default
186my @default_checks = qw(context);
187
188my %valid_check;
ca0b1549 189my %plugin_valid_check;
a798dbf2
MB
190# All valid checks
191BEGIN {
192 map($valid_check{$_}++,
193 qw(context implicit_read implicit_write dollar_underscore
40f1df11 194 private_names bare_subs undefined_subs regexp_variables));
a798dbf2
MB
195}
196
197# Debugging options
198my ($debug_op);
199
200my %done_cv; # used to mark which subs have already been linted
201my @extra_packages; # Lint checks mainline code and all subs which are
202 # in main:: or in one of these packages.
203
204sub warning {
205 my $format = (@_ < 2) ? "%s" : shift;
206 warn sprintf("$format at %s line %d\n", @_, $file, $line);
207}
208
209# This gimme can't cope with context that's only determined
210# at runtime via dowantarray().
211sub gimme {
212 my $op = shift;
213 my $flags = $op->flags;
4c1f658f 214 if ($flags & OPf_WANT) {
94011a57 215 return(($flags & OPf_WANT) == OPf_WANT_LIST ? 1 : 0);
a798dbf2
MB
216 }
217 return undef;
218}
219
ca0b1549
JJ
220my @plugins;
221
222sub B::OP::lint {
223 my $op = shift;
224 my $m;
225 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
226 return;
227}
fa75652c 228
ca0b1549
JJ
229*$_ = *B::OP::lint
230 for \ ( *B::PADOP::lint,
231 *B::LOGOP::lint,
232 *B::BINOP::lint,
233 *B::LISTOP::lint );
a798dbf2
MB
234
235sub B::COP::lint {
236 my $op = shift;
3f872cb9 237 if ($op->name eq "nextstate") {
57843af0 238 $file = $op->file;
a798dbf2
MB
239 $line = $op->line;
240 $curstash = $op->stash->NAME;
241 }
ca0b1549
JJ
242
243 my $m;
244 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
245 return;
a798dbf2
MB
246}
247
248sub B::UNOP::lint {
249 my $op = shift;
3f872cb9
GS
250 my $opname = $op->name;
251 if ($check{context} && ($opname eq "rv2av" || $opname eq "rv2hv")) {
a798dbf2 252 my $parent = parents->[0];
3f872cb9 253 my $pname = $parent->name;
a798dbf2
MB
254 return if gimme($op) || $implies_ok_context{$pname};
255 # Two special cases to deal with: "foreach (@foo)" and "delete $a{$b}"
256 # null out the parent so we have to check for a parent of pp_null and
257 # a grandparent of pp_enteriter or pp_delete
3f872cb9
GS
258 if ($pname eq "null") {
259 my $gpname = parents->[1]->name;
260 return if $gpname eq "enteriter" || $gpname eq "delete";
a798dbf2
MB
261 }
262 warning("Implicit scalar context for %s in %s",
3f872cb9 263 $opname eq "rv2av" ? "array" : "hash", $parent->desc);
a798dbf2 264 }
3f872cb9 265 if ($check{private_names} && $opname eq "method") {
a798dbf2 266 my $methop = $op->first;
3f872cb9 267 if ($methop->name eq "const") {
a798dbf2
MB
268 my $method = $methop->sv->PV;
269 if ($method =~ /^_/ && !defined(&{"$curstash\::$method"})) {
270 warning("Illegal reference to private method name $method");
271 }
272 }
273 }
ca0b1549
JJ
274
275 my $m;
276 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
277 return;
a798dbf2
MB
278}
279
280sub B::PMOP::lint {
281 my $op = shift;
282 if ($check{implicit_read}) {
3f872cb9 283 if ($op->name eq "match" && !($op->flags & OPf_STACKED)) {
a798dbf2
MB
284 warning('Implicit match on $_');
285 }
286 }
287 if ($check{implicit_write}) {
3f872cb9 288 if ($op->name eq "subst" && !($op->flags & OPf_STACKED)) {
a798dbf2
MB
289 warning('Implicit substitution on $_');
290 }
291 }
ca0b1549
JJ
292
293 my $m;
294 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
295 return;
a798dbf2
MB
296}
297
298sub B::LOOP::lint {
299 my $op = shift;
300 if ($check{implicit_read} || $check{implicit_write}) {
3f872cb9 301 if ($op->name eq "enteriter") {
a798dbf2 302 my $last = $op->last;
3f872cb9 303 if ($last->name eq "gv" && $last->gv->NAME eq "_") {
a798dbf2
MB
304 warning('Implicit use of $_ in foreach');
305 }
306 }
307 }
ca0b1549
JJ
308
309 my $m;
310 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
311 return;
a798dbf2
MB
312}
313
7934575e 314sub B::SVOP::lint {
a798dbf2 315 my $op = shift;
40f1df11
RGS
316 if ( $check{bare_subs} && $op->name eq 'const'
317 && $op->private & 64 ) # OPpCONST_BARE = 64 in op.h
318 {
319 my $sv = $op->sv;
320 if( $sv->FLAGS & SVf_POK && exists &{$curstash.'::'.$sv->PV} ) {
321 warning "Bare sub name '" . $sv->PV . "' interpreted as string";
322 }
323 }
3f872cb9 324 if ($check{dollar_underscore} && $op->name eq "gvsv"
a798dbf2
MB
325 && $op->gv->NAME eq "_")
326 {
327 warning('Use of $_');
328 }
329 if ($check{private_names}) {
3f872cb9 330 my $opname = $op->name;
4f25aa18 331 if ($opname eq "gv" || $opname eq "gvsv") {
7934575e
GS
332 my $gv = $op->gv;
333 if ($gv->NAME =~ /^_./ && $gv->STASH->NAME ne $curstash) {
334 warning('Illegal reference to private name %s', $gv->NAME);
335 }
bfecbe02
RGS
336 } elsif ($opname eq "method_named") {
337 my $method = $op->gv->PV;
338 if ($method =~ /^_./) {
339 warning("Illegal reference to private method name $method");
340 }
a798dbf2
MB
341 }
342 }
343 if ($check{undefined_subs}) {
3f872cb9
GS
344 if ($op->name eq "gv"
345 && $op->next->name eq "entersub")
346 {
a798dbf2
MB
347 my $gv = $op->gv;
348 my $subname = $gv->STASH->NAME . "::" . $gv->NAME;
349 no strict 'refs';
350 if (!defined(&$subname)) {
351 $subname =~ s/^main:://;
352 warning('Undefined subroutine %s called', $subname);
353 }
354 }
355 }
3f872cb9 356 if ($check{regexp_variables} && $op->name eq "gvsv") {
a798dbf2
MB
357 my $name = $op->gv->NAME;
358 if ($name =~ /^[&'`]$/) {
359 warning('Use of regexp variable $%s', $name);
360 }
361 }
ca0b1549
JJ
362
363 my $m;
364 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
365 return;
a798dbf2
MB
366}
367
368sub B::GV::lintcv {
369 my $gv = shift;
370 my $cv = $gv->CV;
371 #warn sprintf("lintcv: %s::%s (done=%d)\n",
372 # $gv->STASH->NAME, $gv->NAME, $done_cv{$$cv});#debug
373 return if !$$cv || $done_cv{$$cv}++;
374 my $root = $cv->ROOT;
375 #warn " root = $root (0x$$root)\n";#debug
b0a2b4f5 376 walkoptree_slow($root, "lint") if $$root;
a798dbf2
MB
377}
378
379sub do_lint {
380 my %search_pack;
b0a2b4f5 381 walkoptree_slow(main_root, "lint") if ${main_root()};
a798dbf2
MB
382
383 # Now do subs in main
384 no strict qw(vars refs);
a798dbf2 385 local(*glob);
0091380b
RGS
386 for my $sym (keys %main::) {
387 next if $sym =~ /::$/;
388 *glob = $main::{$sym};
ca0b1549
JJ
389
390 # When is EGV a special value?
391 my $gv = svref_2object(\*glob)->EGV;
392 next if class( $gv ) eq 'SPECIAL';
393 $gv->lintcv;
a798dbf2
MB
394 }
395
396 # Now do subs in non-main packages given by -u options
397 map { $search_pack{$_} = 1 } @extra_packages;
398 walksymtable(\%{"main::"}, "lintcv", sub {
399 my $package = shift;
400 $package =~ s/::$//;
401 #warn "Considering $package\n";#debug
402 return exists $search_pack{$package};
403 });
404}
405
406sub compile {
407 my @options = @_;
408 my ($option, $opt, $arg);
409 # Turn on default lint checks
410 for $opt (@default_checks) {
411 $check{$opt} = 1;
412 }
413 OPTION:
414 while ($option = shift @options) {
415 if ($option =~ /^-(.)(.*)/) {
416 $opt = $1;
417 $arg = $2;
418 } else {
419 unshift @options, $option;
420 last OPTION;
421 }
422 if ($opt eq "-" && $arg eq "-") {
423 shift @options;
424 last OPTION;
425 } elsif ($opt eq "D") {
426 $arg ||= shift @options;
427 foreach $arg (split(//, $arg)) {
428 if ($arg eq "o") {
429 B->debug(1);
430 } elsif ($arg eq "O") {
431 $debug_op = 1;
432 }
433 }
434 } elsif ($opt eq "u") {
435 $arg ||= shift @options;
436 push(@extra_packages, $arg);
437 }
438 }
439 foreach $opt (@default_checks, @options) {
440 $opt =~ tr/-/_/;
441 if ($opt eq "all") {
ca0b1549 442 %check = ( %valid_check, %plugin_valid_check );
a798dbf2
MB
443 }
444 elsif ($opt eq "none") {
445 %check = ();
446 }
447 else {
7f0faf35 448 if ($opt =~ s/^no_//) {
a798dbf2
MB
449 $check{$opt} = 0;
450 }
451 else {
452 $check{$opt} = 1;
453 }
ca0b1549
JJ
454 warn "No such check: $opt\n" unless defined $valid_check{$opt}
455 or defined $plugin_valid_check{$opt};
a798dbf2
MB
456 }
457 }
458 # Remaining arguments are things to check
fa75652c 459
a798dbf2
MB
460 return \&do_lint;
461}
462
ca0b1549
JJ
463sub register_plugin {
464 my ( undef, $plugin, $new_checks ) = @_;
fa75652c 465
ca0b1549
JJ
466 # Register the plugin
467 for my $check ( @$new_checks ) {
468 defined $check
469 or warn "Undefined value in checks.";
470 not $valid_check{ $check }
471 or warn "$check is already registered as a B::Lint feature.";
472 not $plugin_valid_check{ $check }
473 or warn "$check is already registered as a $plugin_valid_check{$check} feature.";
fa75652c 474
ca0b1549
JJ
475 $plugin_valid_check{$check} = $plugin;
476 }
fa75652c 477
ca0b1549 478 push @plugins, $plugin;
fa75652c 479
ca0b1549
JJ
480 return;
481}
482
a798dbf2 4831;