CLOVER🍀

That was when it all began.

自分がよく指定するScalaのコンパイルオプション

よく忘れるので、自分のためにメモ。

だいたい、このあたりを指定しています。

scalacOptions ++= Seq(
  "-deprecation",
  "-feature",
  "-unchecked",
  "-Xlint",
  "-Ywarn-dead-code",
  "-Ywarn-nullary-unit",
  "-Ywarn-numeric-widen",
  "-Ywarn-unused",
  "-Ywarn-unused-import"
)

確認しているScalacのバージョンは、こちら。

$ scalac -version
Scala compiler version 2.12.4 -- Copyright 2002-2017, LAMP/EPFL and Lightbend, Inc.
  • -deprecation … 非推奨警告
  • -feature … SIP-18 - Modularizing Language Features - Scala Documentationのimportが必要な箇所を警告
  • -unchecked … ある条件を満たす場合に、追加の警告を有効にする(?)

このあたりは、「scalac -help」で見ればOK。

$ scalac -help

よりadvancedな警告を有効にするのは「-Xlint」。どういうものが有効にされるのかは、「scalac -Xlint:help」で見れます。

$ scalac -Xlint:help
Enable or disable specific warnings
  adapted-args               Warn if an argument list is modified to match the receiver.
  nullary-unit               Warn when nullary methods return Unit.
  inaccessible               Warn about inaccessible types in method signatures.
  nullary-override           Warn when non-nullary `def f()' overrides nullary `def f'.
  infer-any                  Warn when a type argument is inferred to be `Any`.
  missing-interpolator       A string literal appears to be missing an interpolator id.
  doc-detached               A Scaladoc comment appears to be detached from its element.
  private-shadow             A private field (or class parameter) shadows a superclass field.
  type-parameter-shadow      A local type parameter shadows a type already in scope.
  poly-implicit-overload     Parameterized overloaded implicit methods are not visible as view bounds.
  option-implicit            Option.apply used implicit view.
  delayedinit-select         Selecting member of DelayedInit.
  by-name-right-associative  By-name parameter of right associative operator.
  package-object-classes     Class or object defined in package object.
  unsound-match              Pattern match may not be typesafe.
  stars-align                Pattern sequence wildcard must align with sequence component.
  constant                   Evaluation of a constant arithmetic expression results in an error.
  unused                     Enable -Ywarn-unused:imports,privates,locals,implicits.
Default: All choices are enabled by default.

なお、「-X」なオプションにどのようなものがあるかは、「scalac -X」を使えば見ることができます。

$ scalac -X

最後、「-Y」はprivateなオプション。「-Ywarn-〜」なオプションをいくつか使います。「-Y」なオプションにどのようなものがあるかは、「scalac -Y」で
見ることができます。

$ scalac -Y

うち、「-Ywarn-〜」なものは、こちら。

  -Ywarn-adapted-args                      Warn if an argument list is modified to match the receiver.
  -Ywarn-dead-code                         Warn when dead code is identified.
  -Ywarn-extra-implicit                    Warn when more than one implicit parameter section is defined.
  -Ywarn-inaccessible                      Warn about inaccessible types in method signatures.
  -Ywarn-infer-any                         Warn when a type argument is inferred to be `Any`.
  -Ywarn-macros:<mode>                     Enable lint warnings on macro expansions. Default: `before', `help' to list choices.
  -Ywarn-nullary-override                  Warn when non-nullary `def f()' overrides nullary `def f'.
  -Ywarn-nullary-unit                      Warn when nullary methods return Unit.
  -Ywarn-numeric-widen                     Warn when numerics are widened.
  -Ywarn-unused:<_,warning,-warning>       Enable or disable specific `unused' warnings: `_' for all, `-Ywarn-unused:help' to list choices.
  -Ywarn-unused-import                     Warn when imports are unused.
  -Ywarn-value-discard                     Warn when non-Unit expression results are unused.

Mavenで指定する場合は、こんな感じで。

            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.3.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <scalaVersion>2.12.4</scalaVersion>
                    <args>
                        <arg>-deprecation</arg>
                        <arg>-feature</arg>
                        <arg>-unchecked</arg>
                        <arg>-Xlint</arg>
                        <arg>-Ywarn-dead-code</arg>
                        <arg>-Ywarn-nullary-unit</arg>
                        <arg>-Ywarn-numeric-widen</arg>
                        <arg>-Ywarn-unused</arg>
                        <arg>-Ywarn-unused-import</arg>
                    </args>
                    <recompileMode>incremental</recompileMode>
                </configuration>
            </plugin>

参考)
scalac にもっと警告してもらう

Scala2.11の警告機能がとても親切で素晴らしい件 - xuwei-k's blog