This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warn-unused-(locals|params|patvars)
- Loading branch information
1 parent
c880b76
commit 322d219
Showing
3 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
input/src/main/scala/fix/fromscala2/warn-unused-locals.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
rule = Unused | ||
*/ | ||
package fix.fromscala2 | ||
package locals | ||
|
||
// scalac: -Wunused:locals -Werror | ||
|
||
class Outer { | ||
class Inner | ||
} | ||
|
||
trait Locals { | ||
def f0 = { | ||
var x = 1 // assert: Unused | ||
var y = 2 // no warn | ||
y = 3 | ||
y + y | ||
} | ||
def f1 = { | ||
val a = new Outer // no warn | ||
val b = new Outer // assert: Unused | ||
new a.Inner | ||
} | ||
def f2 = { | ||
var x = 100 // warn about it being a var | ||
x | ||
} | ||
} | ||
|
||
object Types { | ||
def l1() = { | ||
object HiObject { def f = this } // assert: Unused | ||
class Hi { // assert: Unused | ||
def f1: Hi = new Hi | ||
def f2(x: Hi) = x | ||
} | ||
class DingDongDoobie // assert: Unused | ||
class Bippy // no warn | ||
type Something = Bippy // no warn | ||
type OtherThing = String // assert: Unused | ||
(new Bippy): Something | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
input/src/main/scala/fix/fromscala2/warn-unused-params.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
rule = Unused | ||
*/ | ||
package fix.fromscala2 | ||
package params | ||
|
||
// scalac: -Ywarn-unused:params -Xfatal-warnings | ||
// | ||
|
||
trait InterFace { | ||
/** Call something. */ | ||
def call(a: Int, b: String, c: Double): Int | ||
} | ||
|
||
trait BadAPI extends InterFace { | ||
def f(a: Int, | ||
b: String, // assert: Unused | ||
c: Double): Int = { | ||
println(c) | ||
a | ||
} | ||
// TODO: don't warn deprecated metdho | ||
@deprecated ("no warn in deprecated API", since="yesterday") | ||
def g(a: Int, | ||
b: String, // assert: Unused | ||
c: Double): Int = { | ||
println(c) | ||
a | ||
} | ||
|
||
// TODO | ||
// no warn, required by superclass | ||
override def call(a: Int, | ||
b: String, // assert: Unused | ||
c: Double): Int = { | ||
println(c) | ||
a | ||
} | ||
|
||
def meth(x: Int) = x | ||
|
||
override def equals(other: Any): Boolean = true // assert: Unused | ||
|
||
def i(implicit s: String) = 42 // assert: Unused | ||
|
||
/* | ||
def future(x: Int): Int = { | ||
val y = 42 | ||
val x = y // maybe option to warn only if shadowed | ||
x | ||
} | ||
*/ | ||
} | ||
|
||
// mustn't alter warnings in super | ||
trait PoorClient extends BadAPI { | ||
override def meth(x: Int) = ??? // assert: Unused | ||
override def f(a: Int, b: String, c: Double): Int = a + b.toInt + c.toInt | ||
} | ||
|
||
// TODO: warn unused ctor param | ||
class Unusing(u: Int) { | ||
def f = ??? | ||
} | ||
|
||
class Valuing(val u: Int) // no warn | ||
|
||
class Revaluing(u: Int) { def f = u } // no warn | ||
|
||
case class CaseyKasem(k: Int) // no warn | ||
|
||
case class CaseyAtTheBat(k: Int)(s: String) // warn | ||
|
||
trait Ignorance { | ||
def f(readResolve: Int) = 42 // assert: Unused | ||
} | ||
|
||
class Reusing(u: Int) extends Unusing(u) // no warn | ||
|
||
class Main { | ||
// TODO | ||
def main(args: Array[String]): Unit = println("hello, args") // assert: Unused | ||
} | ||
|
||
trait Unimplementation { | ||
def f(u: Int): Int = ??? // assert: Unused | ||
} | ||
|
||
trait DumbStuff { | ||
def f(implicit dummy: DummyImplicit) = 42 // assert: Unused | ||
def g(dummy: DummyImplicit) = 42 // assert: Unused | ||
} | ||
trait Proofs { | ||
def f[A, B](implicit ev: A =:= B) = 42 // assert: Unused | ||
def g[A, B](implicit ev: A <:< B) = 42 // assert: Unused | ||
def f2[A, B](ev: A =:= B) = 42 // assert: Unused | ||
def g2[A, B](ev: A <:< B) = 42 // assert: Unused | ||
} | ||
|
||
trait Anonymous { | ||
def f = (i: Int) => 42 | ||
|
||
def f1 = (_: Int) => 42 // no warn underscore parameter (a fresh name) | ||
|
||
def f2: Int => Int = _ + 1 // no warn placeholder syntax (a fresh name and synthetic parameter) | ||
|
||
def g = for (i <- List(1)) yield 42 // assert: Unused | ||
} |
61 changes: 61 additions & 0 deletions
61
input/src/main/scala/fix/fromscala2/warn-unused-patvars.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
rule = Unused | ||
*/ | ||
package fix.fromscala2 | ||
package patvars | ||
|
||
// scalac: -Ywarn-unused:-patvars,_ -Xfatal-warnings | ||
// | ||
|
||
// verify no warning when -Ywarn-unused:-patvars | ||
|
||
case class C(a: Int, b: String, c: Option[String]) | ||
case class D(a: Int) | ||
|
||
trait Boundings { | ||
|
||
private val x = 42 // assert: Unused | ||
|
||
def c = C(42, "hello", Some("world")) | ||
def d = D(42) | ||
|
||
def f() = { | ||
val C(x, y, Some(z)) = c // assert: Unused | ||
17 | ||
} | ||
def g() = { | ||
val C(x @ _, y @ _, Some(z @ _)) = c // assert: Unused | ||
17 | ||
} | ||
def h() = { | ||
val C(x @ _, y @ _, z @ Some(_)) = c // assert: Unused | ||
17 | ||
} | ||
|
||
def v() = { | ||
val D(x) = d // assert: Unused | ||
17 | ||
} | ||
def w() = { | ||
val D(x @ _) = d // assert: Unused | ||
17 | ||
} | ||
|
||
} | ||
|
||
trait Forever { | ||
def f = { | ||
val t = Option((17, 42)) | ||
for { | ||
ns <- t | ||
(i, j) = ns // no warn | ||
} yield (i + j) | ||
} | ||
def g = { | ||
val t = Option((17, 42)) | ||
for { | ||
ns <- t | ||
(i, j) = ns // assert: Unused | ||
} yield 42 | ||
} | ||
} |