Skip to content
This repository has been archived by the owner on Nov 11, 2021. It is now read-only.

Commit

Permalink
Temp fix (has false negatives) for ContainsTypeMismatch (Issue #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
HairyFotr committed Aug 4, 2016
1 parent f1010c0 commit dbd3cd6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/scala/LinterPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ final class LinterPlugin(val global: Global) extends Plugin {
def seqMemberType(seenFrom: Type): Type = {
if (seenFrom.baseClasses.exists(_.tpe =:= SeqLikeClass.tpe))
SeqLikeClass.tpe.typeArgs.head.asSeenFrom(seenFrom, SeqLikeClass)
else
else
OptionClass.tpe.typeArgs.head.asSeenFrom(seenFrom, OptionClass)
}

Expand Down Expand Up @@ -788,12 +788,14 @@ final class LinterPlugin(val global: Global) extends Plugin {
/// Collection.contains on different types: List(1, 2, 3).contains("2")
case Apply(Select(col, Name("contains")), List(target))
if !(target.tpe.widen weak_<:< seqMemberType(col.tpe.widen))
&& !(target.tpe =:= AnyClass.tpe)
&& (col.tpe.baseClasses.exists(c => c.tpe =:= SeqClass.tpe || c.tpe =:= OptionClass.tpe)) =>

warn(tree, ContainsTypeMismatch(col.tpe.widen.toString, target.tpe.widen.toString))

case Apply(TypeApply(Select(col, Name("contains")), _), List(target))
if !(target.tpe.widen weak_<:< seqMemberType(col.tpe.widen))
&& !(target.tpe =:= AnyClass.tpe)
&& (col.tpe.baseClasses.exists(c => c.tpe =:= SeqClass.tpe || c.tpe =:= OptionClass.tpe)) =>

warn(tree, ContainsTypeMismatch(col.tpe.widen.toString, target.tpe.widen.toString))
Expand Down
16 changes: 15 additions & 1 deletion src/test/scala/LinterPluginTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,23 @@ final class LinterPluginTest extends JUnitMustMatchers with StandardMatchResults
should("""val x = List(4); x.contains("foo")""")
// Contains not present on 2.11 Option
if (Properties.versionString.contains("2.11")) {
should("""val x = Some(1); x.contains("foo")""") // Issue #45
// Issue #45
should("""val x = Some(1); x.contains("foo")""")
should("""val x = Some("foo"); x.contains(1)""")
noLint("""val x = Some("foo"); x.contains("ab")""")
// Issue #46
should("""val foo = Some(4); val bar = Some("bar"); foo.exists(str => bar.contains(str))""")
should("""val foo = Some(4); val bar = Some("bar"); foo.exists(bar.contains(_))""")
should("""val foo = List(4); val bar = List("bar"); foo.exists(str => bar.contains(str))""")
should("""val foo = Some("foo"); val bar = Some(4); foo.exists(str => bar.contains(str))""")
should("""val foo = Some("foo"); val bar = Some(4); foo.exists(bar.contains(_))""")
//TODO false negative
//should("""val foo = Some(4); val bar = Some("bar"); foo.exists(bar.contains)""")
//should("""val foo = Some("foo"); val bar = Some(4); foo.exists(bar.contains)""")

noLint("""val foo = Some("foo"); val bar = Some("bar"); foo.exists(str => bar.contains(str))""")
noLint("""val foo = Some("foo"); val bar = Some("bar"); foo.exists(bar.contains(_))""")
noLint("""val foo = Some("foo"); val bar = Some("bar"); foo.exists(bar.contains)""")
}

noLint("""val x = List(scala.util.Random.nextInt); x.contains(5)""")
Expand Down

0 comments on commit dbd3cd6

Please sign in to comment.