|
| 1 | +# Copyright SUSE LLC |
| 2 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + |
| 4 | +package Perl::Critic::Policy::RedundantStrictWarning; |
| 5 | + |
| 6 | +use strict; |
| 7 | +use warnings; |
| 8 | +use version 0.77; |
| 9 | +use experimental 'signatures'; |
| 10 | + |
| 11 | +use base 'Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict'; |
| 12 | +use Perl::Critic::Utils qw{ $EMPTY }; |
| 13 | +use Perl::Critic::Utils::Constants qw{ :equivalent_modules }; |
| 14 | + |
| 15 | +our $VERSION = '0.0.1'; |
| 16 | +my $policy_title = q{Superfluoux use of strict/warning}; |
| 17 | +my $policy_explanation = q{%s is equivalent to 'use strict; use warnings;'}; |
| 18 | + |
| 19 | +sub default_themes { return qw(openqa) } |
| 20 | + |
| 21 | +sub supported_parameters { |
| 22 | + return ( |
| 23 | + { |
| 24 | + name => 'equivalent_modules', |
| 25 | + description => |
| 26 | + q<The additional modules to treat as equivalent to "strict" or "warnings".>, |
| 27 | + default_string => $EMPTY, |
| 28 | + behavior => 'string list', |
| 29 | + list_always_present_values => ['warnings', 'strict', @STRICT_EQUIVALENT_MODULES], |
| 30 | + }, |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +# check that use strict/warnings is not present when equivalent modules are. |
| 35 | +sub violates ($self, $, $doc) { |
| 36 | + # Find all equivalents of use strict/warnings. |
| 37 | + my $use_stmts = $doc->find($self->_generate_is_use_strict()); |
| 38 | + |
| 39 | + # Bail if there's none. |
| 40 | + return unless $use_stmts; |
| 41 | + |
| 42 | + # Bail out if there's only one. TestingAndDebugging::RequireUseStrict will report |
| 43 | + # that there's no use strict/warnings. |
| 44 | + return if scalar @{$use_stmts} == 1; |
| 45 | + |
| 46 | + # If the 'use strict' or 'use warnings' statement is present as well as a |
| 47 | + # module already providing that behavior, -> it violates. |
| 48 | + return map { $self->_make_violation($_) } grep { !$_->pragma() } @{$use_stmts}; |
| 49 | +} |
| 50 | + |
| 51 | +sub _make_violation ($self, $statement) { |
| 52 | + return $self->violation($policy_title, sprintf($policy_explanation, $statement), $statement); |
| 53 | +} |
| 54 | + |
| 55 | +1; |
| 56 | + |
0 commit comments