Groovy Documentation

org.codenarc.rule.unnecessary
[Groovy] Class UnnecessaryIfStatementRule

java.lang.Object
  org.codenarc.rule.AbstractRule
      org.codenarc.rule.AbstractAstVisitorRule
          org.codenarc.rule.unnecessary.UnnecessaryIfStatementRule

class UnnecessaryIfStatementRule
extends AbstractAstVisitorRule

Rule that checks for unnecessary if statements. If/else statements are considered unnecessary for the three scenarios described below.

(1) When the if and else blocks contain only an explicit return of true and false constants. These cases can be replaced by a simple return statement. Examples include:

(2) When the if statement is the last statement in a block and the if and else blocks contain only true and false expressions. This is an implicit return of true/false. For example, the if statement in the following code can be replaced by someExpression: boolean myMethod() { doSomething() if (someExpression) true; else false } (3) When either the if block or else block of an if statement that is not the last statement in a block contain only a single constant or literal expression For example, the if statement in the following code has no effect and can be removed: def myMethod() { if (someExpression) { 123 } doSomething() }
Authors:
Chris Mair
Version:
\$Revision: 482 \$ - \$Date: 2010-11-25 12:31:36 -0500 (Thu, 25 Nov 2010) \$


Field Summary
 
Fields inherited from class AbstractAstVisitorRule
DEFAULT_CONST_NAME, DEFAULT_FIELD_NAME, DEFAULT_TEST_CLASS_NAMES, DEFAULT_TEST_FILES, DEFAULT_VAR_NAME
 
Property Summary
Class astVisitorClass

String name

int priority

 
Method Summary
 
Methods inherited from class AbstractAstVisitorRule
applyTo, getAstVisitor, shouldApplyThisRuleTo
 
Methods inherited from class AbstractRule
applyTo, applyTo, createViolation, createViolation, createViolationForImport, createViolationForImport, getImportsSortedByLineNumber, getName, getPriority, isReady, packageNameForImport, setName, setPriority, sourceLineAndNumberForImport, sourceLineAndNumberForImport, toString, validate
 

Property Detail

astVisitorClass

Class astVisitorClass


name

String name


priority

int priority


 

Groovy Documentation