Skip to content

Commit 5eba85b

Browse files
committed
Fix #23: use synthetic val instead for unused vars
Synthetic vals just work for ignoring any warnings, including with wartremover and would still trigger side effects if needed
1 parent fc79937 commit 5eba85b

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

better-monadic-for/src/main/scala/com/olegpy/bm4/NoTupleBinding.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ trait NoTupleBinding extends TreeUtils {
1818
// Synthetic vals are generated by destructuring. We don't want to touch them
1919
case v @ ValDef(mods, _, _, _) if mods.hasFlag(Flag.SYNTHETIC) => v
2020

21-
// we wrap RHS in locally { } because we cannot check if it's pure or not without typer
21+
// we wrap RHS in synthetic val ourselves because we cannot check if it's pure or not without typer
2222
// (pure exprs will be generated by destructuring) but we don't want warnings to be issued on
2323
// expressions like (_, x) = tmp because a temp variable underscore is written to is unused
24-
case ValDef(_, TermName(s), _, rhs) if !usedVal(s) =>
25-
replaceTree(rhs, q"scala.Predef.locally($rhs)")
24+
case ValDef(_, TermName(s), tp, rhs) if !usedVal(s) =>
25+
val name = freshTermName("$suppressedUnused$")(currentFreshNameCreator)
26+
val defn = ValDef(Modifiers() | Flag.SYNTHETIC, name, tp, rhs)
27+
replaceTree(rhs, defn)
2628

2729
case a => a
2830
}

build.sbt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,13 @@ lazy val scalazTests = (project in file("scalaz-tests"))
9898
)
9999
)
100100
.settings(testSettings)
101+
102+
lazy val wartRemoverTests = (project in file("wartremover-tests"))
103+
.dependsOn(pluginTests % "compile->compile;test->test")
104+
.settings(
105+
name := "wartremover-tests",
106+
crossScalaVersions := List(scala212),
107+
addCompilerPlugin("org.wartremover" %% "wartremover" % "2.4.2"),
108+
scalacOptions += "-P:wartremover:traverser:org.wartremover.warts.NonUnitStatements"
109+
)
110+
.settings(testSettings)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.olegpy.bm4
2+
3+
import org.scalatest.freespec.AnyFreeSpec
4+
5+
6+
class WartremoverWarnings extends AnyFreeSpec {
7+
"Wartremover should not complain" in {
8+
def right[A, B](b: B): Either[A, B] = Right(b)
9+
case class Foo(a: Long)
10+
for {
11+
Foo(a) <- right[String, Foo](Foo(1))
12+
b <- right(Foo(2))
13+
Foo(c) = b
14+
} yield a + c
15+
}
16+
}

0 commit comments

Comments
 (0)