Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warnings when using -Wnonunit-statement #391

Open
yanns opened this issue Oct 12, 2022 · 0 comments
Open

warnings when using -Wnonunit-statement #391

yanns opened this issue Oct 12, 2022 · 0 comments

Comments

@yanns
Copy link
Contributor

yanns commented Oct 12, 2022

When using the scalac flag -Wnonunit-statement, most of the rules emit the following warning:

unused value of type Boolean (add `: Unit` to discard silently)

My first quick analysis is coming from:

def __advance(): Boolean = ... 
def __updateMaxCursor(): Boolean

used like:

  case class StringMatch(stringTree: Tree) extends OpTree {
    final private val autoExpandMaxStringLength = 8

    def render(wrapped: Boolean): Tree = {
      def unrollUnwrapped(s: String, ix: Int = 0): Tree =
        if (ix < s.length) q"""
          if (cursorChar == ${s charAt ix}) {
            __advance()
            ${unrollUnwrapped(s, ix + 1)}:Boolean
          } else false"""
        else q"true"

In that code, the boolean returned by __advance() is not used.

Possible solutions:

  • (1) We could add some : Unit in the code like this:
  case class StringMatch(stringTree: Tree) extends OpTree {
    final private val autoExpandMaxStringLength = 8

    def render(wrapped: Boolean): Tree = {
      def unrollUnwrapped(s: String, ix: Int = 0): Tree =
        if (ix < s.length) q"""
          if (cursorChar == ${s charAt ix}) {
            __advance(): Unit
            ${unrollUnwrapped(s, ix + 1)}:Boolean
          } else false"""
        else q"true"
  • (2) we could change methods like __advance() to return Unit. We would then have to return true in some branches.

I'm wondering if there is a speed difference between having a method returning a value that we don't use?
The fact that we return true allows also some code like q"_root_.java.lang.Character.toLowerCase(cursorChar) == $charTree && __advance()" that shortcuts the __advance() if the first predicate does not match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant