Compiler options warnings vs linter - scala

In Scala 2.12 is there any difference between any of these options:
-Xlint:adapted-args vs -Ywarn-adapted-args
-Xlint:nullary-unit vs -Ywarn-nullary-unit
-Xlint:inaccessible vs -Ywarn-inaccessible
-Xlint:nullary-override vs -Ywarn-nullary-override
-Xlint:infer-any vs -Ywarn-infer-any
If not, which one makes more sense to use? I also compile with fatal-warnings, and almost all -Ywarn and -Xlint options.

If you run scalac -Xlint:help you'll see
❯ 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 viewbounds.
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.
so there's no difference between selecting Xlint options or using the corresponding flags directly. I would just go with -Xlint (no options, so all enabled by default) and remove the ones I don't need, for example
-Xlint:-unused,_
to enable everything but the unused flag.

Related

How to configure `sbt` to not display warnings "Warning private default argument in object * is never used"

I have a scala compiler 2.12.11 and compiler prints some warnings, like:
private default argument in class SomeClass is never used
while as it is used.
I have seen a Scala 2.12.2 emits a ton of useless "Warning: parameter value ... in method ... is never used" warnings. How to get rid of them? however it doesn't help me, as it is not possible to negate params.
Can you help me to surpress this warning, however saving another unused warnings? Currently, I have -Xlint option.
Instead of -Xlint which is a batch, you can turn on specific options manually. For instant in 2.13 I can print available options like this:
scalac -Xlint:help
Enable recommended warnings
adapted-args An argument list was modified to match the receiver.
nullary-unit `def f: Unit` looks like an accessor; add parens to look side-effecting.
inaccessible Warn about inaccessible types in method signatures.
infer-any A type argument was inferred as Any.
missing-interpolator A string literal appears to be missing an interpolator id.
doc-detached When running scaladoc, warn if a doc comment is discarded.
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 an implicit view.
delayedinit-select Selecting member of DelayedInit.
package-object-classes Class or object defined in package object.
stars-align In a pattern, a sequence wildcard `_*` should match all of a repeated parameter.
strict-unsealed-patmat Pattern match on an unsealed class without a catch-all.
constant Evaluation of a constant arithmetic expression resulted in an error.
unused Enable -Wunused:imports,privates,locals,implicits,nowarn.
nonlocal-return A return statement used an exception for flow control.
implicit-not-found Check #implicitNotFound and #implicitAmbiguous messages.
serial #SerialVersionUID on traits and non-serializable classes.
valpattern Enable pattern checks in val definitions.
eta-zero Usage `f` of parameterless `def f()` resulted in eta-expansion, not empty application `f()`.
eta-sam The Java-defined target interface for eta-expansion was not annotated #FunctionalInterface.
deprecation Enable -deprecation and also check #deprecated annotations.
byname-implicit Block adapted by implicit with by-name parameter.
recurse-with-default Recursive call used default argument.
unit-special Warn for specialization of Unit in parameter position.
multiarg-infix Infix operator was defined or used with multiarg operand.
implicit-recursion Implicit resolves to an enclosing definition.
Default: All choices are enabled by default.
so I could e.g. enable -Xlint:inaccessible -Xlint:adapted-args -Wunused:privates,locales or whatever I want instead of everything. For 2.12 this list would be different. (You can also check scalac -X, scalac -Y and scalac -W).
An alternative is to enable warnings and suppress them when you "breaking" something consciously. For unused you have #scala.annotation.unused, for other warnings in 2.12 and before there is silencer plugin and since 2.13 there is #scala.annotation.nowarn annotation.

Scala 2.12.2 emits a ton of useless "Warning: parameter value ... in method ... is never used" warnings. How to get rid of them?

This is a question so I don't have to traverse the entire Internet to find the answer, as scalac options are currently not published.
How do I disable these warnings starting in Scala 2.12.2, when I have a global "-Xlint"?
$ scalac -Ywarn-unused:help
Enable or disable specific `unused' warnings
imports Warn if an import selector is not referenced.
patvars Warn if a variable bound in a pattern is unused.
privates Warn if a private member is unused.
locals Warn if a local definition is unused.
params Warn if a value parameter is unused.
implicits Warn if an implicit parameter is unused.
Default: All choices are enabled by default.
So
-Ywarn-unused:-params,_
But:
$ 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.
So
-Xlint:unused
Or also more surgically:
-Xlint:-unused,_ -Ywarn-unused:imports
There's a PR to improve ergonomics, so you can set/unset in arbitrary combinations, but this is the incantation for 2.12.2.

How do you debug typelevel code?

Most of the time, all you get is an implicit not found error. You don't know where in the chain of implicit construction it failed. Apparently you can't use runtime debug or print statement. So how do you debug type-level program other than staring at your code really hard?
I wish I had a better answer, but here it goes: Start passing the parameters explicitly, one at a time until it gives you a more useful error. (adding-prinlns-equivalent for implicits params)
You can use ??? for undefined code parts (because it have bottom type Nothing) and _ for unknown types. Also see -Ytyper-debug compiler option (more options here: https://docs.scala-lang.org/overviews/compiler-options/index.html). According to problem with implicit parameters and wrappers, "Idea" has nice feature "Show Implicit Hints" and "Implicit Arguments" (Main menu > Help > Find Action... and type e.g. implicit). Also, you can use Null bottom type instance null for AnyRef like null: YourTypeHere.

Finding applicable implicit conversions in Scala

In the Coq proof assistant - which also has implicit conversions - it is possible to search for an implicit conversion using the SearchAbout T command, which returns all the things which have T in their type (which would include conversions to or from T).
Is there a way of finding all conversions to or from a type for Scala programmers? Note that the conversions might be defined outside the project that defines either the source or destination type.
To just quickly see if a conversion exists in the current scope between two reference types S and T, just type
((null:S):T)
and see if it compiles. With Eclipse Scala IDE >= 2.1M2 you can see which conversion is called, if implicit highlighting is enabled in the preferences.
Of course this requires you to guess both types (but you will probably already have a clear idea of what you want to convert to and from), and it requires the conversions to already be in scope.

What Scala annotations modify the compiler's messages?

I know about two:
#deprecated("use blabla instead") is used to add an explanation to the warning output by the compiler when the annotated definition is used in client code.
#implicitNotFound(msg = "more meaningful explanation") is used to output an additional error message whenever an implicit of the type of the annotated definition cannot be found. Looking at CanBuildFrom, msg can contain placeholders of the type ${A} if A is the name of a type parameter of the annotated type, which is filled in by the compiler with the actual expected type, e.g.:
#implicitNotFound(msg = "Cannot construct a collection of type ${To} with elements of type ${Elem} based on a collection of type ${To}.")
trait CanBuildFrom[-From, -Elem, +To] { ... }
Are there any other such annotations?
There is #migration, which is used with -Xmigration to indicate semantic changes in methods from one version to another, in helping port code between versions.
#migration(2, 8, "As of 2.8, keys returns Iterable[A] rather than Iterator[A].")
There is #tailrec, which makes the compiler output an error if tail call optimization cannot be applied to the annotated method.
As of Scala 2.9, there's also #deprecatedName: “An annotation that designates the name of the parameter to which it is applied as deprecated. Using that name in a named argument generates a deprecation warning.”