Scala 2.13 Scalafix: ExplicitNonNullaryApply
- Auto-application to ()
is deprecated
#537
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Scala 2.13 deprecates (with PR scala/scala#8833) the old behaviour of Scala that zero-parameter methods could be called with either one or zero pairs of parenthesis - ie if you have a method
def foo()
you could call it asfoo()
or justfoo
. With Scala 3, you have to match the number of brackets used in the method declaration when you call it - so you'd have to usefoo()
or you'd get an error like this:Java methods are exempt from this restriction - you can call either
hashCode()
(which, in Java, is how the method has to be defined, with empty brackets) or justhashCode
(which is how that method would have been declared if it was declared in Scala, in Scala methods with no side-effects should be declared without brackets: https://docs.scala-lang.org/style/method-invocation.html#arity-0).Automatically fixing this code issue
There are two possible ways of automating this code fix - in this small project, they both produce the same code changes:
Fixing if the project is already on Scala 2.13 - use
-quickfix
inscalac
You can use the
-quickfix
support added to Scala 2.13.12 with scala/scala#10482:Add either of these to the
scalacOptions
inbuild.sbt
:"-quickfix:any"
...to apply all available quick fixes"-quickfix:msg=Auto-application"
...to apply quick fixes where the message contains "Auto-application"Then run
compile
on the sbt console - the first compile will still fail, but it will subtly change the error message to say[rewritten by -quickfix]
- your files have been edited to receive the fix:...run
compile
a second time, and compiler will be much happier.Examples of other PRs using
-quickfix
to fix this code issue:Fixing while still on Scala 2.12 - use Scalafix
Fixing this everywhere in a project can be tedious, but thankfully there is a
ExplicitNonNullaryApply
Scalafix rule to fix this in the https://github.com/lightbend-labs/scala-rewrites project.The Scalafix rule needs to be run while the project is still on Scala 2.12, not Scala 2.13 (otherwise sbt will say: "Error downloading ch.epfl.scala:sbt-scalafix;sbtVersion=1.0;scalaVersion=2.13:0.13.0").
Once the Scalafix plugin is made available to sbt (by adding
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.13.0")
to eitherproject/plugins.sbt
or~/.sbt/1.0/plugins.sbt
), you can run these commands on the sbt prompt to automatically generate the changes in this PR:Examples of other PRs using Scalafix to fix this code issue:
See also: