8000 Merge pull request #5662 from teldosas/SI-9675 · scala/scala@e62e9e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit e62e9e2

Browse files
authored
Merge pull request #5662 from teldosas/SI-9675
SI-9675 warn about non-sensible equals in anonymous functions
2 parents aa9a627 + 6864d32 commit e62e9e2

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/compiler/scala/tools/nsc/typechecker/RefChecks.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ abstract class RefChecks extends Transform {
11301130
}
11311131
/** Sensibility check examines flavors of equals. */
11321132
def checkSensible(pos: Position, fn: Tree, args: List[Tree]) = fn match {
1133-
case Select(qual, name @ (nme.EQ | nme.NE | nme.eq | nme.ne)) if args.length == 1 && isObjectOrAnyComparisonMethod(fn.symbol) && !currentOwner.isSynthetic =>
1133+
case Select(qual, name @ (nme.EQ | nme.NE | nme.eq | nme.ne)) if args.length == 1 && isObjectOrAnyComparisonMethod(fn.symbol) && (!currentOwner.isSynthetic || currentOwner.isAnonymousFunction) =>
11341134
checkSensibleEquals(pos, qual, name, fn.symbol, args.head)
11351135
case _ =>
11361136
}

test/files/neg/t9675.check

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
t9675.scala:4: warning: comparing values of types Test.A and String using `!=' will always yield true
2+
val func1 = (x: A) => { x != "x" }
3+
^
4+
t9675.scala:6: warning: comparing values of types Test.A and String using `!=' will always yield true
5+
val func2 = (x: A) => { x != "x" }: Boolean
6+
^
7+
t9675.scala:8: warning: comparing values of types Test.A and String using `!=' will always yield true
8+
val func3: Function1[A, Boolean] = (x) => { x != "x" }
9+
^
10+
t9675.scala:11: warning: comparing values of types Test.A and String using `!=' will always yield true
11+
def apply(x: A): Boolean = { x != "x" }
12+
^
13+
t9675.scala:14: warning: comparing values of types Test.A and String using `!=' will always yield true
14+
def method(x: A): Boolean = { x != "x" }
15+
^
16+
t9675.scala:18: warning: comparing values of types Test.A and String using `!=' will always yield true
17+
A("x") != "x"
18+
^
19+
t9675.scala:20: warning: comparing values of types Test.A and String using `!=' will always yield true
20+
val func5: Function1[A, Boolean] = (x) => { x != "x" }
21+
^
22+
t9675.scala:22: warning: comparing values of types Test.A and String using `!=' will always yield true
23+
List(A("x")).foreach((item: A) => item != "x")
24+
^
25+
error: No warnings can be incurred under -Xfatal-warnings.
26+
8 warnings found
27+
one error found

test/files/neg/t9675.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xfatal-warnings

test/files/neg/t9675.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
object Test {
2+
case class A(x: String)
3+
4+
val func1 = (x: A) => { x != "x" }
5+
6+
val func2 = (x: A) => { x != "x" }: Boolean
7+
8+
val func3: Function1[A, Boolean] = (x) => { x != "x" }
9+
10+
val func4 = new Function1[A, Boolean] {
11+
def apply(x: A): Boolean = { x != "x" }
12+
}
13+
14+
def method(x: A): Boolean = { x != "x" }
15+
case class PersonInfo(rankPayEtc: Unit)
16+
17+
def main(args: Array[String]) {
18+
A("x") != "x"
19+
20+
val func5: Function1[A, Boolean] = (x) => { x != "x" }
21+
22+
List(A("x")).foreach((item: A) => item != "x")
23+
}
24+
}

0 commit comments

Comments
 (0)
0