Redundant trailing return in a Unit method
ID |
scala.maintainability.omit_redundant_control_flow |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundant Code |
Language |
Scala |
Description
Reports a bare return written as the last statement of a Unit-returning method, where control already leaves the method at that point.
Rationale
The statement changes nothing, so it is pure noise on the reader’s path. It also carries a Java habit into Scala, where return is not a no-cost keyword: it is implemented by throwing a NonLocalReturnControl, it does not do what it looks like inside a closure, and its presence invites someone to add a return expr later in a language where the last expression is already the result.
The following code illustrates the pattern detected by this rule:
class OmitRedundantControlFlow {
// FLAGGED: Redundant trailing return in a Unit method
def publish(event: String): Unit = {
println(event)
return
}
}