|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | PROPERTY | CONSTR | METHOD | DETAIL: FIELD | PROPERTY | CONSTR | METHOD |
java.lang.Objectorg.codenarc.rule.AbstractAstVisitorRule
groovy.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 oftrue
and false
constants. These cases can be replaced by a simple return statement. Examples include:
if (someExpression) return true else return false
- can be replaced by return someExpression
if (someExpression) { return true } else { return false }
- can be replaced by return someExpression
if (someExpression) { return Boolean.TRUE } else { return Boolean.FALSE }
- can be replaced by return someExpression
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()
}
Property Summary | |
---|---|
Class |
astVisitorClass
|
String |
name
|
int |
priority
|
Property Detail |
---|
Class astVisitorClass
String name
int priority
Groovy Documentation