map followed by flatten instead of flatMap

ID

scala.performance.map_flatten

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Inefficient Api Usage

Language

Scala

Description

map builds an intermediate collection of collections that flatten immediately throws away. flatMap produces the same result in a single traversal with one allocation.

Rationale

map builds an intermediate collection of collections that flatten immediately throws away. flatMap produces the same result in a single traversal with one allocation.

The following code illustrates the pattern detected by this rule:

case class Article(tags: List[String])
class MapFlatten {
  def allTags(articles: List[Article]): List[String] = {
    // FLAGGED: map followed by flatten instead of flatMap
    articles.map(_.tags).flatten
  }
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved