How to to warn the statement if it is an expression and the type of it is not Unit?


As the screenshot shows above, the line 165 is an expression which has a type Tf.Tensor and this line is not the end line, the compiler does not give a warning for this line even though i have configured the scalacOption -Wvalue-discard.
Here is my sbt config:

import org.scalajs.linker.interface.ModuleSplitStyle

lazy val root = project
  .in(file("."))
  .enablePlugins(ScalaJSPlugin)
  .settings(
    name := "bm3d-scalajs",
    scalaVersion := "2.13.11",
    scalacOptions ++= Seq(
      "-encoding", "UTF-8",
      "-unchecked",
      "-deprecation",
      "-Ywarn-dead-code",
      "-Ywarn-numeric-widen",
      "-Wvalue-discard",
      "-Ywarn-unused"
    ),

    scalaJSUseMainModuleInitializer := false,
    scalaJSLinkerConfig ~= {
      _.withModuleKind(ModuleKind.CommonJSModule)
        .withModuleSplitStyle(ModuleSplitStyle.SmallModulesFor(List("bm3d")))
    },

    libraryDependencies ++= Seq(
      "org.scala-js" %%% "scalajs-dom" % "2.4.0",
      "io.github.metarank" %% "cfor" % "0.3"
    )
  )

Is there a way to make the type checker give a warning for non-Unit type of expression?

I fixed this issue by wartremover, but i still want to know that is there any way to fix this by scalac itself?

That is not a value discard but an unused statement, which is a different flag.

Personally, I just use sbt-tpolecat which manages all those flags for me.

1 Like

Thanks! @BalmungSan

The flag is -Wnonunit-statement.

Under scalac -W:

  -Wnonunit-statement            Warn when block statements are non-Unit expressions. [false]

It has useful exclusions, such as for side-effecting methods that return this.type.

2 Likes