Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/idea173.x-pavelfatin' in…
Browse files Browse the repository at this point in the history
…to idea173.x
  • Loading branch information
niktrop committed Dec 12, 2017
2 parents b628348 + 5983cfc commit 3b67586
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.plugins.scala.codeInspection.collections

import org.jetbrains.plugins.scala.codeInspection.InspectionBundle
import org.jetbrains.plugins.scala.codeInspection.collections.ComparingLengthInspection._
import org.jetbrains.plugins.scala.lang.psi.api.base.ScIntLiteral
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScExpression

/**
Expand All @@ -22,10 +23,14 @@ private object ComparingLengthInspection {
case q `.sizeOrLength` () `!=` n => (q, "!=", n)
case q `.sizeOrLength` () `<` n => (q, "<", n)
case q `.sizeOrLength` () `<=` n => (q, "<=", n)
} filter { case (q, op, n) =>
isSeq(q) && !isIndexedSeq(q)
} filter { case (q, _, n) =>
isNonIndexedSeq(q) && !intLiteralValue(n).contains(0)
} map { case (q, op, n) =>
replace(e).withText(s"${invocationText(q, "lengthCompare", n)} $op 0").highlightFrom(q)
}
}

private def intLiteralValue(e: ScExpression) = Some(e).collect {
case ScIntLiteral(n) => n
}
}
Expand Up @@ -394,6 +394,8 @@ package object collections {

def isIndexedSeq(expr: ScExpression): Boolean = isExpressionOfType("scala.collection.IndexedSeqLike", expr)

def isNonIndexedSeq(expr: ScExpression): Boolean = isSeq(expr) && !isIndexedSeq(expr)

def isMap(expr: ScExpression): Boolean = isExpressionOfType("scala.collection.GenMapLike", expr)

def isSortedSet(expr: ScExpression): Boolean = isExpressionOfType("scala.collection.SortedSetLike", expr)
Expand Down
Expand Up @@ -64,6 +64,33 @@ class ComparingLengthTest extends OperationsOnCollectionInspectionTest {
)
}

def testLengthIndexedSeq(): Unit = {
checkTextHasNoErrors(
"Vector(1, 2, 3).length == 2"
)
}

def testLengthZero(): Unit = {
checkTextHasNoErrors(
"Seq(1, 2, 3).length == 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).length != 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).length > 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).length => 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).length < 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).length <= 0"
)
}

def testSizeEqual(): Unit = {
doTest(
s"Seq(1, 2, 3).${START}size == 2$END",
Expand Down Expand Up @@ -112,7 +139,28 @@ class ComparingLengthTest extends OperationsOnCollectionInspectionTest {
)
}

def testIndexedSeq(): Unit = {
def testSizeZero(): Unit = {
checkTextHasNoErrors(
"Seq(1, 2, 3).size == 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).size != 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).size > 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).size => 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).size < 0"
)
checkTextHasNoErrors(
"Seq(1, 2, 3).size <= 0"
)
}

def testSizeIndexedSeq(): Unit = {
checkTextHasNoErrors(
"Vector(1, 2, 3).size == 2"
)
Expand Down

0 comments on commit 3b67586

Please sign in to comment.