|
| 1 | +// scalac: -Wnonunit-statement -Wvalue-discard |
| 2 | +import collection.ArrayOps |
| 3 | +import collection.mutable.{ArrayBuilder, LinkedHashSet, ListBuffer} |
| 4 | +import concurrent._ |
| 5 | +import scala.reflect.ClassTag |
| 6 | + |
| 7 | +class C { |
| 8 | + import ExecutionContext.Implicits._ |
| 9 | + def c = { |
| 10 | + def improved = Future(42) |
| 11 | + def stale = Future(27) |
| 12 | + improved // error |
| 13 | + stale |
| 14 | + } |
| 15 | +} |
| 16 | +class D { |
| 17 | + def d = { |
| 18 | + class E |
| 19 | + new E().toString // error |
| 20 | + new E().toString * 2 |
| 21 | + } |
| 22 | +} |
| 23 | +class F { |
| 24 | + import ExecutionContext.Implicits._ |
| 25 | + Future(42) // error |
| 26 | +} |
| 27 | +// unused template expression uses synthetic method of class |
| 28 | +case class K(s: String) { |
| 29 | + copy() // error |
| 30 | +} |
| 31 | +// mutations returning this are ok |
| 32 | +class Mutate { |
| 33 | + val b = ListBuffer.empty[Int] |
| 34 | + b += 42 // nowarn, returns this.type |
| 35 | + val xs = List(42) |
| 36 | + 27 +: xs // error |
| 37 | + |
| 38 | + def f(x: Int): this.type = this |
| 39 | + def g(): Unit = f(42) // nowarn |
| 40 | +} |
| 41 | +// some uninteresting expressions may warn for other reasons |
| 42 | +class WhoCares { |
| 43 | + null // error for purity |
| 44 | + ??? // nowarn for impurity |
| 45 | +} |
| 46 | +// explicit Unit ascription to opt out of warning, even for funky applies |
| 47 | +class Absolution { |
| 48 | + def f(i: Int): Int = i+1 |
| 49 | + import ExecutionContext.Implicits._ |
| 50 | + // Future(42): Unit // nowarn { F(42)(ctx) }: Unit where annot is on F(42) |
| 51 | + // f(42): Unit // nowarn |
| 52 | +} |
| 53 | +// warn uni-branched unless user disables it with -Wnonunit-if:false |
| 54 | +class Boxed[A](a: A) { |
| 55 | + def isEmpty = false |
| 56 | + def foreach[U](f: A => U): Unit = |
| 57 | + if (!isEmpty) f(a) // error (if) |
| 58 | + def forall(f: A => Boolean): Unit = |
| 59 | + if (!isEmpty) { |
| 60 | + println(".") |
| 61 | + f(a) // error (if) |
| 62 | + } |
| 63 | + def take(p: A => Boolean): Option[A] = { |
| 64 | + while (isEmpty || !p(a)) () |
| 65 | + Some(a).filter(p) |
| 66 | + } |
| 67 | +} |
| 68 | +class Unibranch[A, B] { |
| 69 | + def runWith[U](action: B => U): A => Boolean = { x => |
| 70 | + val z = null.asInstanceOf[B] |
| 71 | + val fellback = false |
| 72 | + if (!fellback) action(z) // error (if) |
| 73 | + !fellback |
| 74 | + } |
| 75 | + def f(i: Int): Int = { |
| 76 | + def g = 17 |
| 77 | + if (i < 42) { |
| 78 | + g // error block statement |
| 79 | + println("uh oh") |
| 80 | + g // error (if) |
| 81 | + } |
| 82 | + while (i < 42) { |
| 83 | + g // error |
| 84 | + println("uh oh") |
| 85 | + g // error |
| 86 | + } |
| 87 | + 42 |
| 88 | + } |
| 89 | +} |
| 90 | +class Dibranch { |
| 91 | + def i: Int = ??? |
| 92 | + def j: Int = ??? |
| 93 | + def f(b: Boolean): Int = { |
| 94 | + // if-expr might have an uninteresting LUB |
| 95 | + if (b) { // error, at least one branch looks interesting |
| 96 | + println("true") |
| 97 | + i |
| 98 | + } |
| 99 | + else { |
| 100 | + println("false") |
| 101 | + j |
| 102 | + } |
| 103 | + 42 |
| 104 | + } |
| 105 | +} |
| 106 | +class Next[A] { |
| 107 | + val all = ListBuffer.empty[A] |
| 108 | + def f(it: Iterator[A], g: A => A): Unit = |
| 109 | + while (it.hasNext) |
| 110 | + all += g(it.next()) // nowarn |
| 111 | +} |
| 112 | +class Setting[A] { |
| 113 | + def set = LinkedHashSet.empty[A] |
| 114 | + def f(a: A): Unit = { |
| 115 | + set += a // error because cannot know whether the `set` was supposed to be consumed or assigned |
| 116 | + println(set) |
| 117 | + } |
| 118 | +} |
| 119 | +// neither StringBuilder warns, because either append is Java method or returns this.type |
| 120 | +// while loop looks like if branch with block1(block2, jump to label), where block2 typed as non-unit |
| 121 | +class Strung { |
| 122 | + def iterator = Iterator.empty[String] |
| 123 | + def addString(b: StringBuilder, start: String, sep: String, end: String): StringBuilder = { |
| 124 | + val jsb = b.underlying |
| 125 | + if (start.length != 0) jsb.append(start) // error (value-discard) |
| 126 | + val it = iterator |
| 127 | + if (it.hasNext) { |
| 128 | + jsb.append(it.next()) |
| 129 | + while (it.hasNext) { |
| 130 | + jsb.append(sep) // nowarn (java) |
| 131 | + jsb.append(it.next()) // error (value-discard) |
| 132 | + } |
| 133 | + } |
| 134 | + if (end.length != 0) jsb.append(end) // error (value-discard) |
| 135 | + b |
| 136 | + } |
| 137 | + def f(b: java.lang.StringBuilder, it: Iterator[String]): String = { |
| 138 | + while (it.hasNext) { |
| 139 | + b.append("\n") // nowarn (java) |
| 140 | + b.append(it.next()) // error (value-discard) |
| 141 | + } |
| 142 | + b.toString |
| 143 | + } |
| 144 | + def g(b: java.lang.StringBuilder, it: Iterator[String]): String = { |
| 145 | + while (it.hasNext) it.next() // error |
| 146 | + b.toString |
| 147 | + } |
| 148 | +} |
| 149 | +class J { |
| 150 | + import java.util.Collections |
| 151 | + def xs: java.util.List[Int] = ??? |
| 152 | + def f(): Int = { |
| 153 | + Collections.checkedList[Int](xs, classOf[Int]) |
| 154 | + 42 |
| 155 | + } |
| 156 | +} |
| 157 | +class Variant { |
| 158 | + var bs = ListBuffer.empty[Int] |
| 159 | + val xs = ListBuffer.empty[Int] |
| 160 | + private[this] val ys = ListBuffer.empty[Int] |
| 161 | + private[this] var zs = ListBuffer.empty[Int] |
| 162 | + def f(i: Int): Unit = { |
| 163 | + bs.addOne(i) |
| 164 | + xs.addOne(i) |
| 165 | + ys.addOne(i) |
| 166 | + zs.addOne(i) |
| 167 | + println("done") |
| 168 | + } |
| 169 | +} |
| 170 | +final class ArrayOops[A](private val xs: Array[A]) extends AnyVal { |
| 171 | + def other: ArrayOps[A] = ??? |
| 172 | + def transpose[B](implicit asArray: A => Array[B]): Array[Array[B]] = { |
| 173 | + val aClass = xs.getClass.getComponentType |
| 174 | + val bb = new ArrayBuilder.ofRef[Array[B]]()(ClassTag[Array[B]](aClass)) |
| 175 | + if (xs.length == 0) bb.result() |
| 176 | + else { |
| 177 | + def mkRowBuilder() = ArrayBuilder.make[B](ClassTag[B](aClass.getComponentType)) |
| 178 | + val bs = new ArrayOps(asArray(xs(0))).map((x: B) => mkRowBuilder()) |
| 179 | + for (xs <- other) { |
| 180 | + var i = 0 |
| 181 | + for (x <- new ArrayOps(asArray(xs))) { |
| 182 | + bs(i) += x |
| 183 | + i += 1 |
| 184 | + } |
| 185 | + } |
| 186 | + for (b <- new ArrayOps(bs)) bb += b.result() |
| 187 | + bb.result() |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | +class Depends { |
| 192 | + def f[A](a: A): a.type = a |
| 193 | + def g() = { |
| 194 | + val d = new Depends |
| 195 | + f(d) |
| 196 | + () |
| 197 | + } |
| 198 | +} |
0 commit comments