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
  }
}

Remediation

Delete the trailing return. Where a method uses return for an early exit, restructure the body around if/else or a match so that every path ends in the value it produces, which is the idiomatic form and avoids the exception-based control flow entirely.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved