mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-15 02:59:33 +07:00
[jvm] Fix false-positive UrlHashCodeInspection
Previously, this inspection highlighted the cases like `url.equals(null)` or `url == null`. This commit fixes this behavior ^IDEA-332645 fixed GitOrigin-RevId: 7448489db08e5bbf75dfc1582788bc1f78f923f2
This commit is contained in:
committed by
intellij-monorepo-bot
parent
ae2e9b0b34
commit
6233b72cb6
@@ -19,9 +19,9 @@ import org.jetbrains.uast.visitor.AbstractUastNonRecursiveVisitor
|
||||
class UrlHashCodeInspection : AbstractBaseUastLocalInspectionTool() {
|
||||
private fun UExpression.isUrlType() = getExpressionType()?.equalsToText(JAVA_NET_URL) == true
|
||||
|
||||
private fun PsiClassType.isMapType() = rawType().equalsToText(JAVA_UTIL_MAP)
|
||||
private fun PsiClassType.isMapType() = rawType().equalsToText(JAVA_UTIL_MAP)
|
||||
|
||||
private fun PsiClassType.isSetType() = rawType().equalsToText(JAVA_UTIL_SET)
|
||||
private fun PsiClassType.isSetType() = rawType().equalsToText(JAVA_UTIL_SET)
|
||||
|
||||
private val mapKeyOperationMatcher: CallMatcher = CallMatcher.instanceCall(
|
||||
JAVA_UTIL_MAP,
|
||||
@@ -34,8 +34,11 @@ class UrlHashCodeInspection : AbstractBaseUastLocalInspectionTool() {
|
||||
private val hashCodeMatcher: CallMatcher = CallMatcher.instanceCall(JAVA_NET_URL, HASH_CODE)
|
||||
.parameterCount(0)
|
||||
|
||||
private val equalsMatcher: CallMatcher = CallMatcher.instanceCall(JAVA_NET_URL, EQUALS)
|
||||
.parameterTypes(JAVA_LANG_OBJECT)
|
||||
private val equalsMatcher: CallMatcher = CallMatcher.instanceCall(JAVA_NET_URL, EQUALS).parameterTypes(
|
||||
JAVA_LANG_OBJECT).withContextFilter {
|
||||
val uCallExpression = it.toUElementOfType<UCallExpression>() ?: return@withContextFilter true
|
||||
uCallExpression.valueArguments.firstOrNull()?.isNullLiteral()?.not() ?: true
|
||||
}
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||
return UastHintedVisitorAdapter.create(
|
||||
@@ -63,6 +66,7 @@ class UrlHashCodeInspection : AbstractBaseUastLocalInspectionTool() {
|
||||
override fun visitBinaryExpression(node: UBinaryExpression): Boolean {
|
||||
if (!node.rightOperand.isUrlType() && !node.leftOperand.isUrlType()) return true
|
||||
if (node.operatorIdentifier?.name != "==") return true
|
||||
if (node.leftOperand.isNullLiteral() || node.rightOperand.isNullLiteral()) return true
|
||||
val method = node.resolveOperator() ?: return true
|
||||
if (method.name != EQUALS) return true
|
||||
val anchor = node.operatorIdentifier?.sourcePsi ?: return true
|
||||
|
||||
@@ -83,4 +83,20 @@ class JavaUrlHashCodeInspectionTest : UrlHashCodeInspectionTestBase() {
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test URL doesn't highlight when comparing with null`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import java.net.URL;
|
||||
|
||||
class Foo {
|
||||
static {
|
||||
try {
|
||||
var url = new URL("");
|
||||
if (url.equals(null)) {
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
@@ -80,4 +80,18 @@ class KotlinUrlHashCodeInspectionTest : UrlHashCodeInspectionTestBase() {
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test URL doesn't highlight when comparing with null`() {
|
||||
myFixture.testHighlighting(JvmLanguage.KOTLIN, """
|
||||
import java.net.URL
|
||||
|
||||
@Suppress("DEPRECATION", "SENSELESS_COMPARISON")
|
||||
fun main() {
|
||||
val sample = URL("")
|
||||
if (sample == null) {}
|
||||
if (null == sample) {}
|
||||
sample.equals(null)
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user