[uast-inspections] IDEA-367844 Log inspection doesn't support {} in constant if constant and log are in different classes

- resolve references

GitOrigin-RevId: 874571723262565b0969719234506d8e65872725
This commit is contained in:
Mikhail Pyltsin
2025-04-22 13:56:18 +00:00
committed by intellij-monorepo-bot
parent 800e233ecb
commit 38eea8c4fc
3 changed files with 30 additions and 6 deletions
@@ -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<PartHolder> {
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)
@@ -752,7 +752,7 @@ class JavaLoggingPlaceholderCountMatchesArgumentCountInspectionTest : LoggingPla
public static void main(String[] args) {
log.error(<warning descr="More arguments provided (6) than placeholders specified (0)">SimpleClass.PLACEHOLDER</warning>, 1, 2, 4,5, 6, 7);
log.error(<warning descr="More arguments provided (6) than placeholders specified (2)">SimpleClass.PLACEHOLDER</warning>, 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(<warning descr="More arguments provided (6) than placeholders specified (2)">SimpleClass.PLACEHOLDER</warning>, 1, 2, 4,5, 6, 7);
}
}
""".trimIndent())
}
}
@@ -100,7 +100,7 @@ abstract class KotlinLoggingPlaceholderCountMatchesArgumentCountInspectionPlaceh
internal class X {
val LOG = LoggerFactory.getLogger()
fun m() {
LOG.info( <warning descr="Fewer arguments provided (0) than placeholders specified (4)">S + "{}" + (1 + 2) + '{' + '}' + S</warning> )
LOG.info( <warning descr="Fewer arguments provided (0) than placeholders specified (at least 2)">S + "{}" + (1 + 2) + '{' + '}' + S</warning> )
LOG.info( <warning descr="Fewer arguments provided (0) than placeholders specified (1)">message</warning> )
}