Skip to content
Browse files

Added ProhibitBitwiseOperators

  • Loading branch information...
1 parent 9dee5f9 commit e084eb79297433b507162eb4d312abd0953daffc @petdance committed May 16, 2011
View
6 Changes
@@ -5,6 +5,12 @@ Revision history for Perl-Critic-Bangs
As of 1.07_01, Perl::Critic::Bangs is released under Artistic License 2.0,
rather than dual-licensed with the GPL.
+[ENHANCEMENTS]
+Added a new policy, ProhibitBitwiseOperators. Usually when you see
+a bitwise operator like | or & in a Perl program, it's a mistake that
+should have been || or &&. It also checks for ^ and ~ as bitwise ops.
+Thanks to Mike O'Regan.
+
[FIXES]
Remove use of Perl::Critic::Utils::PPIRegexp, which has been removed
from the Perl-Critic distribution, with calls to native PPI methods. This
View
2 MANIFEST
@@ -1,4 +1,5 @@
lib/Perl/Critic/Bangs.pm
+lib/Perl/Critic/Policy/Bangs/ProhibitBitwiseOperators.pm
lib/Perl/Critic/Policy/Bangs/ProhibitCommentedOutCode.pm
lib/Perl/Critic/Policy/Bangs/ProhibitFlagComments.pm
lib/Perl/Critic/Policy/Bangs/ProhibitNoPlan.pm
@@ -25,6 +26,7 @@ t/94_includes.t
t/98_pod_syntax.t
t/99_pod_coverage.t
+t/Bangs/ProhibitBitwiseOperators.run
t/Bangs/ProhibitCommentedOutCode.run
t/Bangs/ProhibitFlagComments.run
t/Bangs/ProhibitNoPlan.run
View
1 ProhibitBitwiseOperators.pm
View
73 lib/Perl/Critic/Policy/Bangs/ProhibitBitwiseOperators.pm
@@ -0,0 +1,73 @@
+package Perl::Critic::Policy::Bangs::ProhibitBitwiseOperators;
+
+use 5.006001;
+use strict;
+use warnings;
+use Readonly;
+
+use Perl::Critic::Utils qw{ :severities :classification :data_conversion };
+use base 'Perl::Critic::Policy';
+
+our $VERSION = '1.07_01';
+
+#-----------------------------------------------------------------------------
+
+Readonly::Scalar my $DESC => q{Use of bitwise operator};
+Readonly::Scalar my $EXPL => q{Use of bitwise operator};
+
+#-----------------------------------------------------------------------------
+
+sub supported_parameters { return () }
+sub default_severity { return $SEVERITY_HIGHEST }
+sub default_themes { return qw( core bugs ) }
+sub applies_to { return 'PPI::Token::Operator' }
+
+#-----------------------------------------------------------------------------
+
+my %bitwise_operators = hashify( qw( & | ^ ~ ) );
+
+sub violates {
+ my ( $self, $elem, undef ) = @_;
+
+ if ( $bitwise_operators{$elem->content()} ) {
+ return $self->violation( $DESC, "$EXPL '" . $elem->content() . "'", $elem );
+ }
+ return; #ok!
+}
+
+1;
+
+#---------------------------------------------------------------------------
+
+=pod
+
+=head1 NAME
+
+Perl::Critic::Policy::Bangs::ProhibitBitwiseOperators - Bitwise operators are usually accidentally used instead of logical boolean operators.
+
+=head1 AFFILIATION
+
+This Policy is part of the L<Perl::Critic::Bangs> distribution.
+
+=head1 DESCRIPTION
+
+Bitwise operators are usually accidentally used instead of logical
+boolean operators. Instead of writing C<$a || $b>, people will often
+write C<$a | $b>, which is not correct.
+
+=head1 CONFIGURATION
+
+This policy cannot be configured.
+
+=head1 AUTHOR
+
+Mike O'Regan <moregan@stresscafe.com>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2006-2011 Andy Lester and Mike O'Regan
+
+This library is free software; you can redistribute it and/or modify
+it under the terms of the Artistic License 2.0.
+
+=cut
View
17 t/Bangs/ProhibitBitwiseOperators.run
@@ -0,0 +1,17 @@
+## name Double ops are OK
+## failures 0
+## cut
+
+my $a;
+my $b;
+my $c = $a || $b;
+
+#-----------------------------------------------------------------------------
+
+## name Bitwise OR failure
+## failures 1
+## cut
+
+my $a;
+my $b;
+my $c = $a | $b;

0 comments on commit e084eb7

Please sign in to comment.
Something went wrong with that request. Please try again.