1

I'm using SBT to build a project and have in my Build.scala:

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature", "-Xfatal-warnings")

How can I configure the project to warn on name/variable shadowing? I've browsed the source here and here, but don't see what I'm looking for among the many options. A comment on this old question mentions a flag -Ywarn-shadowing which doesn't seem to exist.

Community
  • 1
  • 1
jberryman
  • 16,334
  • 5
  • 42
  • 83
  • Maybe someone knows about a scala-abide or wart remover for that. I don't see it off-hand. There are other use cases: http://stackoverflow.com/questions/27587965/scala-making-sure-that-a-field-from-the-superclass-is-used https://issues.scala-lang.org/browse/SI-9100 https://issues.scala-lang.org/browse/SI-4762 – som-snytt Jan 06 '16 at 01:43
  • This covers the sentiment: http://stackoverflow.com/questions/12385293/how-can-i-get-scalac-to-tell-me-if-i-have-unused-variables – som-snytt Jan 06 '16 at 01:44

2 Answers2

1

As indicated in a related question, you could start exploring with -X. I then tried -Xlint:help and this yields some settings for shadowing:

Information:scalac: 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.

Probably -Xlint:private-shadow is what you want?

0__
  • 66,707
  • 21
  • 171
  • 266
  • Thanks, that helps, but I was looking for shadowing of variables e.g. `val` in nested scopes, or a `val` shadowing a function or method argument. It looks like it might not be possible, which is a bit cuckoo to me. – jberryman Jan 06 '16 at 00:15
1

There are many scala linter tools. Scapegoat has an option for VariableShadowing. I don't see a shadowing option in the more popular linters, but you could implement one yourself.

dwickern
  • 3,519
  • 1
  • 14
  • 21