From 38eea8c4fc0ce4450352df321683e7807eb92ee5 Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Thu, 17 Apr 2025 17:59:22 +0200 Subject: [PATCH] [uast-inspections] IDEA-367844 Log inspection doesn't support {} in constant if constant and log are in different classes - resolve references GitOrigin-RevId: 874571723262565b0969719234506d8e65872725 --- .../logging/LoggingStringPartEvaluator.kt | 11 +++++---- ...CountMatchesArgumentCountInspectionTest.kt | 23 ++++++++++++++++++- ...entCountInspectionPlaceholderNumberTest.kt | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringPartEvaluator.kt b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringPartEvaluator.kt index 99313ac968ef..cf574dd19c30 100644 --- a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringPartEvaluator.kt +++ b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringPartEvaluator.kt @@ -59,8 +59,7 @@ internal class LoggingStringPartEvaluator { is ULiteralExpression -> getFromLiteralExpression(expression) is UPolyadicExpression -> getFromPolyadicExpression(expression, context) is UParenthesizedExpression -> recursiveCalculateValue(expression.skipParenthesizedExprDown(), context) - is UQualifiedReferenceExpression -> recursiveCalculateValue(expression.selector, context.copy(depth = context.depth - 6)) - is USimpleNameReferenceExpression -> getFromReferenceExpression(expression, context) + is UReferenceExpression -> getFromReferenceExpression(expression, context) is UCallExpression -> getFromCallExpression(expression, context) else -> listOf(PartHolder(null, false)) } @@ -122,11 +121,15 @@ internal class LoggingStringPartEvaluator { return result } - private fun getFromReferenceExpression(expression: USimpleNameReferenceExpression, + private fun getFromReferenceExpression(expression: UReferenceExpression, context: Context): List { val resolvedUElement = expression.resolveToUElement() if (resolvedUElement is UField && resolvedUElement.isFinal) { - return recursiveCalculateValue(resolvedUElement.uastInitializer, context) + var currentContext = context + if (resolvedUElement.getContainingUFile() != expression.getContainingUFile()) { + currentContext = currentContext.copy(depth = context.depth - 3) + } + return recursiveCalculateValue(resolvedUElement.uastInitializer, currentContext) } if (resolvedUElement is ULocalVariable && isNotAssignment(resolvedUElement)) { return recursiveCalculateValue(resolvedUElement.uastInitializer, context) diff --git a/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/tests/java/logging/JavaLoggingPlaceholderCountMatchesArgumentCountInspectionTest.kt b/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/tests/java/logging/JavaLoggingPlaceholderCountMatchesArgumentCountInspectionTest.kt index fec44058fd1d..ba2b7809fcb8 100644 --- a/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/tests/java/logging/JavaLoggingPlaceholderCountMatchesArgumentCountInspectionTest.kt +++ b/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/tests/java/logging/JavaLoggingPlaceholderCountMatchesArgumentCountInspectionTest.kt @@ -752,7 +752,7 @@ class JavaLoggingPlaceholderCountMatchesArgumentCountInspectionTest : LoggingPla public static void main(String[] args) { - log.error(SimpleClass.PLACEHOLDER, 1, 2, 4,5, 6, 7); + log.error(SimpleClass.PLACEHOLDER, 1, 2, 4,5, 6, 7); } } @@ -760,5 +760,26 @@ class JavaLoggingPlaceholderCountMatchesArgumentCountInspectionTest : LoggingPla public static final String PLACEHOLDER = "say {} hello {}"; }""".trimIndent()) } + fun `test field from different file`() { + myFixture.addClass(""" + class SimpleClass { + public static final String PLACEHOLDER = "say {} hello {}"; + } + """.trimIndent()) + myFixture.testHighlighting(JvmLanguage.JAVA, """ + import org.apache.logging.log4j.LogManager; + import org.apache.logging.log4j.Logger; + + class Main { + + public static final Logger log = LogManager.getLogger(); + + public static void main(String[] args) { + + log.error(SimpleClass.PLACEHOLDER, 1, 2, 4,5, 6, 7); + } + } + """.trimIndent()) + } } diff --git a/jvm/jvm-analysis-kotlin-tests-shared/testSrc/com/intellij/codeInspection/tests/kotlin/logging/KotlinLoggingPlaceholderCountMatchesArgumentCountInspectionPlaceholderNumberTest.kt b/jvm/jvm-analysis-kotlin-tests-shared/testSrc/com/intellij/codeInspection/tests/kotlin/logging/KotlinLoggingPlaceholderCountMatchesArgumentCountInspectionPlaceholderNumberTest.kt index 171b662a8662..fbf4472abc9e 100644 --- a/jvm/jvm-analysis-kotlin-tests-shared/testSrc/com/intellij/codeInspection/tests/kotlin/logging/KotlinLoggingPlaceholderCountMatchesArgumentCountInspectionPlaceholderNumberTest.kt +++ b/jvm/jvm-analysis-kotlin-tests-shared/testSrc/com/intellij/codeInspection/tests/kotlin/logging/KotlinLoggingPlaceholderCountMatchesArgumentCountInspectionPlaceholderNumberTest.kt @@ -100,7 +100,7 @@ abstract class KotlinLoggingPlaceholderCountMatchesArgumentCountInspectionPlaceh internal class X { val LOG = LoggerFactory.getLogger() fun m() { - LOG.info( S + "{}" + (1 + 2) + '{' + '}' + S ) + LOG.info( S + "{}" + (1 + 2) + '{' + '}' + S ) LOG.info( message ) }