logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Perl::Critic::Policy::ValuesAndExpressions::ProhibitNullStatements - disallow empty statements (stray

Configuration

       "allow_perl4_semihash" (boolean, default false)
           If true then Perl 4 style documentation comments like the following are allowed.

               ;# Usage:
               ;#      require 'mypkg.pl';
               ;#      ...

           The  ";" must be at the start of the line.  This is fairly outdated, so it's disabled by default.  If
           you're crunching through some old code you can enable it by adding to your .perlcriticrc file

               [ValuesAndExpressions::ProhibitNullStatements]
               allow_perl4_semihash=1

Description

       This policy is part of the "Perl::Critic::Pulp" add-on.  It prohibits empty statements, ie. bare ";"
       semicolons.  This can be a typo doubling up a semi like

           use Foo;;    # bad

       Or a stray left at the end of a control structure like

           if ($foo) {
             print "foo\n";
             return;
           };           # bad

       An empty statement is harmless, so this policy is under the "cosmetic" theme (see "POLICY THEMES" in
       Perl::Critic) and medium severity.  It's surprisingly easy to leave a semi behind when chopping code
       around, especially when changing a statement to a loop or conditional.

   Allowedforms
       A C style "for (;;) { ...}" loop is ok.  Those semicolons are expression separators and empties there are
       quite usual.

           for (;;) {   # ok
             print "infinite loop\n";
           }

       A semicolon at the start of a "map" or "grep" block is allowed.  It's commonly used to ensure Perl parses
       it as a block, not an anonymous hash.  (Perl decides at the point it parses the "{".  A ";" there forces
       a block when it might otherwise guess wrongly.  See "map" in perlfunc for more on this.)

           map {; $_, 123} @some_list;      # ok

           grep {# this is a block
                 ;                          # ok
                 length $_ and $something } @some_list;

       The "map" form is much more common than the "grep", but both suffer the same ambiguity.  "grep" doesn't
       normally inspire people to quite such convoluted forms as "map" does.

   Try/CatchBlocks
       The "Try", "TryCatch" and "Syntax::Feature::Try" modules all add new "try" block statement forms.  These
       statements don't require a terminating semicolon (the same as an "if" doesn't require one).  Any
       semicolon there is reckoned as a null statement.

           use TryCatch;
           sub foo {
             try { attempt_something() }
             catch { error_recovery()  };   # bad
           }

       This doesn't apply to other try modules such as "Try::Tiny" and friends.  They're implemented as ordinary
       function calls (with prototypes), so a terminating semicolon is normal for them.

           use Try::Tiny;
           sub foo {
             try { attempt_something() }
             catch { error_recovery()  };   # ok
           }

Home Page

Name

       Perl::Critic::Policy::ValuesAndExpressions::ProhibitNullStatements - disallow empty statements (stray
       semicolons)

See Also

       Perl::Critic::Pulp, Perl::Critic, Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon

See Also