Skip to content

Loading…

Contains check with type check #2

Merged
merged 4 commits into from

2 participants

@jrudolph

Jorge, can you check if this works for you? You don't have to really pull that one but I'd be interested if it works for you.

@jorgeortiz85 jorgeortiz85 merged commit cb966ce into jorgeortiz85:master
@OlegYch OlegYch pushed a commit to OlegYch/linter that referenced this pull request
@HairyFotr HairyFotr Added some null/Option checks. Closes #2 2e3ab33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 9 additions and 3 deletions.
  1. +8 −2 src/main/scala/LinterPlugin.scala
  2. +1 −1 src/test/scala/LinterPluginTest.scala
View
10 src/main/scala/LinterPlugin.scala
@@ -41,6 +41,9 @@ class LinterPlugin(val global: Global) extends Plugin {
val SeqLikeContains: Symbol =
SeqLikeClass.info.member(newTermName("contains"))
+ val SeqLikeA: Type =
+ SeqLikeClass.tpe.typeArgs.head
+
val AnyEquals: Symbol =
AnyClass.info.member(newTermName("$eq$eq"))
@@ -66,8 +69,11 @@ class LinterPlugin(val global: Global) extends Plugin {
case Import(t, selectors) if selectors.exists(_.name == global.nme.WILDCARD) && t.symbol == JavaConversionsModule =>
unit.warning(t.pos, "Conversions in scala.collection.JavaConversions._ are dangerous.")
- case a @ Apply(s, _) if methodImplements(s.symbol, SeqLikeContains) =>
- unit.warning(s.pos, "SeqLike.contains takes an Any instead of an element of the collection type.")
+ case a @ Apply(s@Select(seq, _), p)
+ if methodImplements(s.symbol, SeqLikeContains)
+ && !(p.head.tpe <:< SeqLikeA.asSeenFrom(seq.tpe, SeqLikeClass)) =>
+
+ unit.warning(s.pos, "SeqLike[%s].contains(%s) will probably return false." format(SeqLikeA.asSeenFrom(seq.tpe, SeqLikeClass), p.head.tpe))
case node @ Select(q, GetMethod) if q.symbol.isSubClass(OptionClass) =>
if (!node.pos.source.path.contains("src/test")) {
View
2 src/test/scala/LinterPluginTest.scala
@@ -68,7 +68,7 @@ class LinterPluginTest extends SpecsMatchers {
@Test
def testHasVersusContains(): Unit = {
- val msg = Some("SeqLike.contains takes an Any instead of an element of the collection type.")
+ val msg = Some("SeqLike[Int].contains(java.lang.String(\"foo\")) will probably return false.")
check("""val x = List(4); x.contains("foo")""", msg)
Something went wrong with that request. Please try again.