NAME
TooMuchCode::ProhibitUnusedImport -- Find unused imports
DESCRIPTION
An "import" is a subroutine brought by a use
statement.
From the documentation of use, there are several forms of calling `use`. This policy scans for the following two forms:
use Module VERSION LIST
use Module LIST
... and only the one with LIST written in qw()
.
Conventionally the LIST after c<use Module> is known as arguments and conventionally when it is written with qw()
, the LIST is treated as a import list -- which is a list of symbols that becomes avaiable in the current namespace.
For example, the word baz
in the following statement is one of such:
use Foo qw( baz );
Symbols in the import list are often subroutine names or variable names. If they are not used in the following program, they do not neet to be imported.
Although an experienced perl programmer would know that the description above is only true by convention, there are many modules on CPAN that already follows such convetion. Which is a good one to follow, and I recommend you to follow.
This policy checks only import lists written in qw()
, other forms are ignored, or rather, too complicated to be correctly supported.
The syntax of Importer
module is also supported, but only the ones with qw()
at the end.
use Importer 'Foo' => qw( baz );
Modules with non-trivial form of arguments may have nothing to with symbol-importing. But it might be used with a qw()
LIST at the end. Should you wish to do so, you may let chose to let perlcritic to ignore certain modules by setting the ignored_modules
in .perlcriticrc
. For example:
[TooMuchCode::ProhibitUnusedImport]
ignored_modules = Git::Sub Regexp::Common
Alternatively, you may choose not to write the module arguments as a qw()
list.
Moose Types
Moose Types can also be imported, but their symbols may not be used as-is. Instead, some other helper functions are generated with names based on the Type. For example:
use My::Type::Library::Numeric qw( PositiveInt );
use My::Type::Library::String qw( LowerCaseStr );
my $foo = 'Bar';
my $ok = is_PositiveInt($foo);
my $lower = to_LowerCaseStr($foo);
The module My::Type::Library::Numeric
exports is_PositiveInt
as well as PositiveInt
. While PositiveInt
is not directly used in the following code, is should be considered as being used. Similar for LowerCaseStr
.
When importing from a Type library, subroutines named like is_*
and to_*
are not in the import list, but they are also imported.
By default, the following modules are treated as Type libraries
* MooseX::Types::Moose
* MooseX::Types::Common::Numeric
* MooseX::Types::Common::String
The list can be grown by including your module names to the moose_type_modules
in the .perlcriticrc
:
[TooMuchCode::ProhibitUnusedImport]
moose_type_modules = My::Type::Library::Numeric My::Type::Library::String