This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bring the joy of strict to ext/B/t/lint.t.
[perl5.git] / ext / B / t / lint.t
CommitLineData
94011a57
JH
1#!./perl -w
2
3BEGIN {
9b494a7e
JJ
4 if ( $ENV{PERL_CORE} ) {
5 chdir('t') if -d 't';
6 @INC = ( '.', '../lib' );
7 }
8 else {
9 unshift @INC, 't';
10 push @INC, "../../t";
5638aaac 11 }
9cd8f857 12 require Config;
9b494a7e 13 if ( ( $Config::Config{'extensions'} !~ /\bB\b/ ) ) {
9cd8f857
NC
14 print "1..0 # Skip -- Perl configured without B module\n";
15 exit 0;
16 }
5638aaac 17 require 'test.pl';
94011a57 18}
354c4f67
NC
19use strict;
20use warnings;
94011a57 21
2adc4a42 22plan tests => 29;
94011a57
JH
23
24# Runs a separate perl interpreter with the appropriate lint options
25# turned on
26sub runlint ($$$;$) {
9b494a7e 27 my ( $opts, $prog, $result, $testname ) = @_;
94011a57 28 my $res = runperl(
9b494a7e
JJ
29 switches => ["-MO=Lint,$opts"],
30 prog => $prog,
31 stderr => 1,
94011a57
JH
32 );
33 $res =~ s/-e syntax OK\n$//;
354c4f67 34 local $::Level = $::Level + 1;
94011a57
JH
35 is( $res, $result, $testname || $opts );
36}
37
9b494a7e
JJ
38runlint 'magic-diamond', 'while(<>){}', <<'RESULT';
39Use of <> at -e line 1
40RESULT
41
42runlint 'magic-diamond', 'while(<ARGV>){}', <<'RESULT';
43Use of <> at -e line 1
44RESULT
45
46runlint 'magic-diamond', 'while(<FOO>){}', <<'RESULT';
47RESULT
48
94011a57
JH
49runlint 'context', '$foo = @bar', <<'RESULT';
50Implicit scalar context for array in scalar assignment at -e line 1
51RESULT
52
53runlint 'context', '$foo = length @bar', <<'RESULT';
54Implicit scalar context for array in length at -e line 1
55RESULT
56
2e9e4ed7
JJ
57runlint 'context', 'our @bar', '';
58
59runlint 'context', 'exists $BAR{BAZ}', '';
60
94011a57
JH
61runlint 'implicit-read', '/foo/', <<'RESULT';
62Implicit match on $_ at -e line 1
63RESULT
64
3ee1325f
JJ
65runlint 'implicit-read', 'grep /foo/, ()', '';
66
67runlint 'implicit-read', 'grep { /foo/ } ()', '';
68
94011a57
JH
69runlint 'implicit-write', 's/foo/bar/', <<'RESULT';
70Implicit substitution on $_ at -e line 1
71RESULT
72
9b494a7e
JJ
73runlint 'implicit-read', 'for ( @ARGV ) { 1 }',
74 <<'RESULT', 'implicit-read in foreach';
94011a57
JH
75Implicit use of $_ in foreach at -e line 1
76RESULT
77
9b494a7e 78runlint 'implicit-read', '1 for @ARGV', '', 'implicit-read in foreach';
2e9e4ed7 79
9b494a7e 80runlint 'dollar-underscore', '$_ = 1', <<'RESULT';
94011a57
JH
81Use of $_ at -e line 1
82RESULT
83
2adc4a42
JJ
84runlint 'dollar-underscore', 'sub foo {}; foo( $_ ) for @A', '';
85runlint 'dollar-underscore', 'sub foo {}; map { foo( $_ ) } @A', '';
86runlint 'dollar-underscore', 'sub foo {}; grep { foo( $_ ) } @A', '';
2e9e4ed7 87
9b494a7e
JJ
88runlint 'dollar-underscore', 'print',
89 <<'RESULT', 'dollar-underscore in print';
94011a57
JH
90Use of $_ at -e line 1
91RESULT
92
9b494a7e
JJ
93runlint 'private-names', 'sub A::_f{};A::_f()', <<'RESULT';
94Illegal reference to private name '_f' at -e line 1
94011a57
JH
95RESULT
96
9b494a7e
JJ
97runlint 'private-names', '$A::_x', <<'RESULT';
98Illegal reference to private name '_x' at -e line 1
94011a57
JH
99RESULT
100
9b494a7e
JJ
101runlint 'private-names', 'sub A::_f{};A->_f()', <<'RESULT',
102Illegal reference to private method name '_f' at -e line 1
94011a57 103RESULT
bfecbe02 104 'private-names (method)';
94011a57 105
9b494a7e
JJ
106runlint 'undefined-subs', 'foo()', <<'RESULT';
107Nonexistant subroutine 'foo' called at -e line 1
108RESULT
109
110runlint 'undefined-subs', 'foo();sub foo;', <<'RESULT';
111Undefined subroutine 'foo' called at -e line 1
94011a57
JH
112RESULT
113
9b494a7e 114runlint 'regexp-variables', 'print $&', <<'RESULT';
94011a57
JH
115Use of regexp variable $& at -e line 1
116RESULT
117
9b494a7e 118runlint 'regexp-variables', 's/./$&/', <<'RESULT';
94011a57
JH
119Use of regexp variable $& at -e line 1
120RESULT
94011a57 121
9b494a7e 122runlint 'bare-subs', 'sub bare(){1};$x=bare', '';
40f1df11 123
9b494a7e 124runlint 'bare-subs', 'sub bare(){1}; $x=[bare=>0]; $x=$y{bare}', <<'RESULT';
40f1df11
RGS
125Bare sub name 'bare' interpreted as string at -e line 1
126Bare sub name 'bare' interpreted as string at -e line 1
127RESULT
2adc4a42
JJ
128
129{
130
131 # Check for backwards-compatible plugin support. This was where
132 # preloaded mdoules would register themselves with B::Lint.
133 my $res = runperl(
134 switches => ["-MB::Lint"],
135 prog =>
136 'BEGIN{B::Lint->register_plugin(X=>[q[x]])};use O(qw[Lint x]);sub X::match{warn qq[X ok.\n]};dummy()',
137 stderr => 1,
138 );
139 like( $res, qr/X ok\./, 'Lint legacy plugin' );
140}
141
142{
143
144 # Check for Module::Plugin support
145 my $res = runperl(
146 switches => [ '-I../ext/B/t/pluglib', '-MO=Lint,none' ],
147 prog => 1,
148 stderr => 1,
149 );
150 like( $res, qr/Module::Pluggable ok\./, 'Lint uses Module::Pluggable' );
151}