Class MoveVariableInsideIfCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class MoveVariableInsideIfCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    Checks if a variable is only used inside if statements and asks for its declaration to be moved there too.

    Rationale: Code inside if/else statements are only executed when those specific block conditions evaluate to true. Moving variables inside these blocks prevents the code from being executed when the value of the variable is not even being used. It also helps limit the scope of the variables from being too broad to confuse new readers. Suppressing variables with false violations because of the check's limitations (stated below) also help clearly state the purpose of the variable as a temporary storage for a current/future changing value.

    An example of how to configure the check is:

     <module name="MoveVariableInsideIfCheck"/>
     

    which will produce the following violation:

     String variable = input.substring(1); // violation - variable is only used inside if block
    
     if (condition) {
         return method(variable);
     }
    
     return "";
     

    The code can be written as the following to avoid a violation:

     if (condition) {
         String variable = input.substring(1);
         return method(variable);
     }
    
     return "";
     

    No violations will be produced if a variable is used in same scope as declaration, condition of block, or if used in multiple blocks.

     String variable = input.substring(1);
    
     if (condition && variable.charAt(0) == 'T') {
         return method(variable);
     }
     else {
         return method2(variable);
     }
    
     return "";
     

    Limitations: The check can not determine if the value of variable being stored is changed after the declaration. Variables like this can't be moved, or may be too complex to move, and thus should be suppressed.

    Case #1:

     final String variable = list.remove(0); // false positive - list is modified with storing value
     final String next = list.get(0); // expecting above list modification
    
     if (next.equals(input)) {
         list.add(variable);
     }
     

    Case #2:

     final String variable = field.get(0); // false positive - field is modified later, before block
    
     modifyField(); // field is modified inside this method
    
     if (condition) {
         field.add(variable);
     }
     
    Since:
    1.24.0
    Author:
    Richard Veach
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      A key is pointing to the warning message text in "messages.properties" file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Field Detail

      • MSG_KEY

        public static final String MSG_KEY
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
    • Constructor Detail

      • MoveVariableInsideIfCheck

        public MoveVariableInsideIfCheck()
    • Method Detail

      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck