Skip to content

Scalastyle proposed rules (Imports)

erikvanoosten edited this page Nov 22, 2012 · 9 revisions

This page contains proposed rules for Scalastyle, category Imports.

Imports

RedundantImport

Checks for redundant import statements. An import statement is considered redundant if:

  • It is a duplicate of another import. This is, when a class is imported more than once.
  • The class imported is from the scala package, e.g. importing scala.String.
  • The class imported is from the same package.

UnusedImports

Checks for unused import statements. Scalastyle uses a simple but very reliable algorithm to report on unused import statements. An import statement is considered unused if:

  • It is not referenced in the file. The algorithm does not support wild-card imports like import scala.collection.mutable._;. Most IDE's provide very sophisticated checks for imports that handle wild-card imports.
  • It is a duplicate of another import. This is when a class is imported more than once.
  • The class imported is from the scala package. For example importing scala.String.
  • The class imported is from the same package.
  • [MJF Check this is the case for Scala] Optionally: it is referenced in Scaladoc comments. This check is off by default, as it is considered bad practice to introduce a compile time dependency for documentation purposes only. As an example, the import java.util.Date would be considered referenced with the Javadoc comment {@link Date}. The alternative to avoid introducing a compile time dependency would be to write the Javadoc comment as {@link java.util.Date}.

ImportOrder

Checks the ordering/grouping of imports. Features are:

  • groups imports: ensures that groups of imports come in a specific order (e.g., scala. comes first, then everything else)
  • adds a separation between groups : ensures that a blank line sit between each group
  • sorts imports inside each group: ensures that imports within each group are in lexicographic order
  • sorts according to case: ensures that the comparison between imports is case sensitive
  • groups static imports: ensures the relative order between regular imports and static imports (see import orders)

Syntax

Checks syntax of imports. Features are:

  • full imports: ensures that imports declare the full package (some exceptions should be possible when it is common, e.g. allow import scalaz._; import Scalaz._)