Subject: | ProhibitUselessNoCritic not of user-disabled policies |
Date: | Sat, 12 Sep 2009 09:53:01 +1000 |
To: | bug-Perl-Critic [...] rt.cpan.org |
From: | Kevin Ryde <user42 [...] zip.com.au> |
When a policy is explicitly disabled in .perlcriticrc with [-Foo],
ProhibitUselessNoCritic will report as useless an annotation of that
policy,
## no critic (Foo)
It'd be good if ProhibitUselessNoCritic recognised that Foo didn't
suppress anything because I'd asked not to use it.
Whenever Foo is not run it's of course not possible to tell if that
annotation is useful or useless. More than likely it's useful to the
users using the policy, and on that basis needn't be reported as
anything bad to other people.
The couple of lines below get the effect I wanted. Is the Perl::Critic
object and/or the UserProfile communicated to the policy object? I
couldn't spot it so I put it through a global variable just to see how
it went.
Index: lib/Perl/Critic/Policy/Miscellanea/ProhibitUselessNoCritic.pm
===================================================================
--- lib/Perl/Critic/Policy/Miscellanea/ProhibitUselessNoCritic.pm (revision 3613)
+++ lib/Perl/Critic/Policy/Miscellanea/ProhibitUselessNoCritic.pm (working copy)
@@ -12,9 +12,8 @@
use warnings;
use Readonly;
+use List::MoreUtils qw< any >;
-use List::MoreUtils qw< none >;
-
use Perl::Critic::Utils qw{ :severities :classification hashify };
use base 'Perl::Critic::Policy';
@@ -40,13 +39,24 @@
# If for some reason $doc is not a P::C::Document, then all bets are off
return if not $doc->isa('Perl::Critic::Document');
+ # where can this properly be had from ?
+ my $userprofile = $Perl::Critic::UserProfile::INSTANCE;
+
my @violations = ();
my @suppressed_viols = $doc->suppressed_violations();
for my $ann ( $doc->annotations() ) {
- if ( none { _annotation_suppresses_violation($ann, $_) } @suppressed_viols ) {
- push @violations, $self->violation($DESC, $EXPL, $ann->element());
- }
+ # ok if $ann suppressed something
+ next if any {_annotation_suppresses_violation($ann,$_)}
+ @suppressed_viols;
+
+ # ok if any of $ann was explicitly disabled by the user; cannot know
+ # if an annotation is useful or useless if one or more of its policies
+ # were not run
+ next if any {$userprofile->policy_is_disabled($_)}
+ $ann->disabled_policies;
+
+ push @violations, $self->violation($DESC, $EXPL, $ann->element());
}
return @violations;
Index: lib/Perl/Critic/UserProfile.pm
===================================================================
--- lib/Perl/Critic/UserProfile.pm (revision 3613)
+++ lib/Perl/Critic/UserProfile.pm (working copy)
@@ -25,6 +25,8 @@
our $VERSION = '1.104';
+our $INSTANCE;
+
#-----------------------------------------------------------------------------
sub new {
@@ -32,6 +34,7 @@
my ( $class, %args ) = @_;
my $self = bless {}, $class;
$self->_init( %args );
+ $INSTANCE = $self;
return $self;
}