-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn when arms of if or match lub to Any
- Loading branch information
Showing
8 changed files
with
122 additions
and
88 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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,15 @@ | ||
t12829.scala:6: warning: a type was inferred to be `Any`; this may indicate a programming error. | ||
def f(i: Int, t: Boolean) = if (t) b += i else 42 // warn because if in expr position | ||
^ | ||
t12829.scala:11: warning: a type was inferred to be `Any`; this may indicate a programming error. | ||
t match { // warn expr | ||
^ | ||
t12829.scala:15: warning: a type was inferred to be `Any`; this may indicate a programming error. | ||
def z = List(42, "hello") // warn type inference as usual | ||
^ | ||
t12829.scala:23: warning: a type was inferred to be `Object`; this may indicate a programming error. | ||
xs match { // warn because interesting type of arm | ||
^ | ||
error: No warnings can be incurred under -Werror. | ||
4 warnings | ||
1 error |
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,28 @@ | ||
|
||
// scalac: -Werror -Xlint:infer-any | ||
|
||
class C { | ||
val b = collection.mutable.ListBuffer.empty[Int] | ||
def f(i: Int, t: Boolean) = if (t) b += i else 42 // warn because if in expr position | ||
def g[A](a: A): A = a | ||
def h(i: Int, t: Boolean) = g(if (t) k) // warn because expr | ||
def k: Int = 27 | ||
def n(i: Int, t: Boolean) = | ||
t match { // warn expr | ||
case true => b += i | ||
case _ => 42 | ||
} | ||
def z = List(42, "hello") // warn type inference as usual | ||
|
||
def `arms are any`(xs: List[String]) = | ||
(xs: Any) match { // nowarn because only top/bottom types | ||
case null => throw new NullPointerException // null-check first helps static analysis of instanceOf | ||
case y: Any => y | ||
} | ||
def `arms are any but`(xs: List[String]) = | ||
xs match { // warn because interesting type of arm | ||
case null => throw new NullPointerException // null-check first helps static analysis of instanceOf | ||
case _ :: Nil => new Object | ||
case List(_ @ _*) => "" | ||
} | ||
} |