Skip to content

Commit 2c6777f

Browse files
committed
Merge pull request scala#1565 from retronym/ticket/6567
SI-6567 Warning for Option(implicitView(foo))
2 parents 98ccb9f + 0bcb9e9 commit 2c6777f

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,12 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
10511051
def apply(tp: Type) = mapOver(tp).normalize
10521052
}
10531053

1054+
def checkImplicitViewOptionApply(pos: Position, fn: Tree, args: List[Tree]): Unit = if (settings.lint.value) (fn, args) match {
1055+
case (tap@TypeApply(fun, targs), List(view: ApplyImplicitView)) if fun.symbol == Option_apply =>
1056+
unit.warning(pos, s"Suspicious application of an implicit view (${view.fun}) in the argument to Option.apply.") // SI-6567
1057+
case _ =>
1058+
}
1059+
10541060
def checkSensible(pos: Position, fn: Tree, args: List[Tree]) = fn match {
10551061
case Select(qual, name @ (nme.EQ | nme.NE | nme.eq | nme.ne)) if args.length == 1 =>
10561062
def isReferenceOp = name == nme.eq || name == nme.ne
@@ -1535,7 +1541,10 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
15351541

15361542
case Apply(fn, args) =>
15371543
// sensicality should be subsumed by the unreachability/exhaustivity/irrefutability analyses in the pattern matcher
1538-
if (!inPattern) checkSensible(tree.pos, fn, args)
1544+
if (!inPattern) {
1545+
checkImplicitViewOptionApply(tree.pos, fn, args)
1546+
checkSensible(tree.pos, fn, args)
1547+
}
15391548
currentApplication = tree
15401549
tree
15411550
}

src/reflect/scala/reflect/internal/Definitions.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,12 @@ trait Definitions extends api.StandardDefinitions {
540540
lazy val ScalaLongSignatureAnnotation = requiredClass[scala.reflect.ScalaLongSignature]
541541

542542
// Option classes
543-
lazy val OptionClass: ClassSymbol = requiredClass[Option[_]]
544-
lazy val SomeClass: ClassSymbol = requiredClass[Some[_]]
545-
lazy val NoneModule: ModuleSymbol = requiredModule[scala.None.type]
546-
lazy val SomeModule: ModuleSymbol = requiredModule[scala.Some.type]
543+
lazy val OptionClass: ClassSymbol = requiredClass[Option[_]]
544+
lazy val OptionModule: ModuleSymbol = requiredModule[scala.Option.type]
545+
lazy val Option_apply = getMemberMethod(OptionModule, nme.apply)
546+
lazy val SomeClass: ClassSymbol = requiredClass[Some[_]]
547+
lazy val NoneModule: ModuleSymbol = requiredModule[scala.None.type]
548+
lazy val SomeModule: ModuleSymbol = requiredModule[scala.Some.type]
547549

548550
def compilerTypeFromTag(tt: ApiUniverse # WeakTypeTag[_]): Type = tt.in(rootMirror).tpe
549551
def compilerSymbolFromTag(tt: ApiUniverse # WeakTypeTag[_]): Symbol = tt.in(rootMirror).tpe.typeSymbol

test/files/neg/t6567.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
t6567.scala:8: warning: Suspicious application of an implicit view (Test.this.a2b) in the argument to Option.apply.
2+
Option[B](a)
3+
^
4+
t6567.scala:10: warning: Suspicious application of an implicit view (Test.this.a2b) in the argument to Option.apply.
5+
val b: Option[B] = Option(a)
6+
^
7+
error: No warnings can be incurred under -Xfatal-warnings.
8+
two warnings found
9+
one error found

test/files/neg/t6567.flags

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

test/files/neg/t6567.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class A
2+
class B
3+
4+
object Test {
5+
val a: A = null
6+
implicit def a2b(a: A) = new B
7+
8+
Option[B](a)
9+
10+
val b: Option[B] = Option(a)
11+
}

0 commit comments

Comments
 (0)