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::CodeLayout::ProhibitIfIfSameLine - don't put if after if on same line

Description

       This policy is part of the "Perl::Critic::Pulp" add-on.  It asks you to not to write an "if" statement on
       the same line as a preceding "if".

           if ($x) {
             ...
           } if ($y) {       # bad
             ...
           }

           if ($x) {
             ...
           } elsif ($y) {    # was "elsif" intended ?
             ...
           }

       The idea is that an "if" in the layout of an "elsif" may be either a mistake or will be confusing to a
       human reader.  On that basis this policy is under the "bugs" theme and medium severity (see "POLICY
       THEMES" in Perl::Critic).

   Unless
       An "unless...if" is treated the same.  Perl allows "unless ... elsif" and so the same potential confusion
       with an "elsif" layout arises.

           unless ($x) {
             ...
           } if ($y) {       # bad
             ...
           }

           unless ($x) {
             ...
           } elsif ($y) {    # maybe meant to be "elsif" like this ?
             ...
           }

       Whether "unless ... elsif" is a good idea at all is another matter.  Sometimes it suits a combination of
       conditions.

   StatementModifiers
       This policy only applies to a statement followed by a statement.  An "if" as a statement modifier is not
       affected.  It's usual to put that on the same line as the statement it modifies.

           do {
             ...
           } if ($x);        # ok, statement modifier

   AllOneLine
       Two "if" statements written on the same line will trigger the policy.

           if(1){one;}   if(2){two;}      # bad

       Perhaps there could be an exception or option when both statements are entirely on the one line, or some
       such, for code which is trying to be compact.

   Disabling
       As always if you don't care about this then you can disable "ProhibitIfIfSameLine" from your
       .perlcriticrc (see "CONFIGURATION" in Perl::Critic),

           [-CodeLayout::ProhibitIfIfSameLine]

Home Page

Name

       Perl::Critic::Policy::CodeLayout::ProhibitIfIfSameLine - don't put if after if on same line

See Also

       Perl::Critic::Pulp, Perl::Critic

       Perl::Critic::Policy::ControlStructures::ProhibitCascadingIfElse,
       Perl::Critic::Policy::ControlStructures::ProhibitUnlessBlocks

See Also