This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: B::Lint changes
[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
JH
18}
19
9b494a7e 20plan tests => 28;
94011a57
JH
21
22# Runs a separate perl interpreter with the appropriate lint options
23# turned on
24sub runlint ($$$;$) {
9b494a7e 25 my ( $opts, $prog, $result, $testname ) = @_;
94011a57 26 my $res = runperl(
9b494a7e
JJ
27 switches => ["-MO=Lint,$opts"],
28 prog => $prog,
29 stderr => 1,
94011a57
JH
30 );
31 $res =~ s/-e syntax OK\n$//;
32 is( $res, $result, $testname || $opts );
33}
34
9b494a7e
JJ
35runlint 'magic-diamond', 'while(<>){}', <<'RESULT';
36Use of <> at -e line 1
37RESULT
38
39runlint 'magic-diamond', 'while(<ARGV>){}', <<'RESULT';
40Use of <> at -e line 1
41RESULT
42
43runlint 'magic-diamond', 'while(<FOO>){}', <<'RESULT';
44RESULT
45
94011a57
JH
46runlint 'context', '$foo = @bar', <<'RESULT';
47Implicit scalar context for array in scalar assignment at -e line 1
48RESULT
49
50runlint 'context', '$foo = length @bar', <<'RESULT';
51Implicit scalar context for array in length at -e line 1
52RESULT
53
2e9e4ed7
JJ
54runlint 'context', 'our @bar', '';
55
56runlint 'context', 'exists $BAR{BAZ}', '';
57
94011a57
JH
58runlint 'implicit-read', '/foo/', <<'RESULT';
59Implicit match on $_ at -e line 1
60RESULT
61
3ee1325f
JJ
62runlint 'implicit-read', 'grep /foo/, ()', '';
63
64runlint 'implicit-read', 'grep { /foo/ } ()', '';
65
94011a57
JH
66runlint 'implicit-write', 's/foo/bar/', <<'RESULT';
67Implicit substitution on $_ at -e line 1
68RESULT
69
ca0b1549
JJ
70{
71 my $res = runperl(
9b494a7e
JJ
72 switches => ["-MB::Lint"],
73 prog =>
74 'BEGIN{B::Lint->register_plugin(X=>[q[x]])};use O(qw[Lint x]);sub X::match{warn qq[X ok.\n]};dummy()',
75 stderr => 1,
ca0b1549
JJ
76 );
77 like( $res, qr/X ok\./, 'Lint plugin' );
78}
79
9b494a7e
JJ
80runlint 'implicit-read', 'for ( @ARGV ) { 1 }',
81 <<'RESULT', 'implicit-read in foreach';
94011a57
JH
82Implicit use of $_ in foreach at -e line 1
83RESULT
84
9b494a7e 85runlint 'implicit-read', '1 for @ARGV', '', 'implicit-read in foreach';
2e9e4ed7 86
9b494a7e 87runlint 'dollar-underscore', '$_ = 1', <<'RESULT';
94011a57
JH
88Use of $_ at -e line 1
89RESULT
90
9b494a7e
JJ
91runlint 'dollar-underscore', 'foo( $_ ) for @A', '';
92runlint 'dollar-underscore', 'map { foo( $_ ) } @A', '';
93runlint 'dollar-underscore', 'grep { foo( $_ ) } @A', '';
2e9e4ed7 94
9b494a7e
JJ
95runlint 'dollar-underscore', 'print',
96 <<'RESULT', 'dollar-underscore in print';
94011a57
JH
97Use of $_ at -e line 1
98RESULT
99
9b494a7e
JJ
100runlint 'private-names', 'sub A::_f{};A::_f()', <<'RESULT';
101Illegal reference to private name '_f' at -e line 1
94011a57
JH
102RESULT
103
9b494a7e
JJ
104runlint 'private-names', '$A::_x', <<'RESULT';
105Illegal reference to private name '_x' at -e line 1
94011a57
JH
106RESULT
107
9b494a7e
JJ
108runlint 'private-names', 'sub A::_f{};A->_f()', <<'RESULT',
109Illegal reference to private method name '_f' at -e line 1
94011a57 110RESULT
bfecbe02 111 'private-names (method)';
94011a57 112
9b494a7e
JJ
113runlint 'undefined-subs', 'foo()', <<'RESULT';
114Nonexistant subroutine 'foo' called at -e line 1
115RESULT
116
117runlint 'undefined-subs', 'foo();sub foo;', <<'RESULT';
118Undefined subroutine 'foo' called at -e line 1
94011a57
JH
119RESULT
120
9b494a7e 121runlint 'regexp-variables', 'print $&', <<'RESULT';
94011a57
JH
122Use of regexp variable $& at -e line 1
123RESULT
124
9b494a7e 125runlint 'regexp-variables', 's/./$&/', <<'RESULT';
94011a57
JH
126Use of regexp variable $& at -e line 1
127RESULT
94011a57 128
9b494a7e 129runlint 'bare-subs', 'sub bare(){1};$x=bare', '';
40f1df11 130
9b494a7e 131runlint 'bare-subs', 'sub bare(){1}; $x=[bare=>0]; $x=$y{bare}', <<'RESULT';
40f1df11
RGS
132Bare sub name 'bare' interpreted as string at -e line 1
133Bare sub name 'bare' interpreted as string at -e line 1
134RESULT