[java-inspections] IDEA-360135 Non-constant string concatenation in logging call could be triggered with exception argument

GitOrigin-RevId: b9a08671660f1a3cb27e566e1a9ed6a05cd211d4
This commit is contained in:
Mikhail Pyltsin
2024-10-07 12:37:20 +02:00
committed by intellij-monorepo-bot
parent 0a0b47c9d4
commit 77ec6df673
6 changed files with 71 additions and 7 deletions

View File

@@ -395,7 +395,7 @@ public final class StringConcatenationArgumentToLogCallInspection extends BaseIn
@Override
public void fix(@NotNull PsiMethodCallExpression callExpression) {
PsiExpression[] expressions = callExpression.getArgumentList().getExpressions();
if (expressions.length != 1) {
if (expressions.length < 1) {
return;
}
@@ -407,8 +407,8 @@ public final class StringConcatenationArgumentToLogCallInspection extends BaseIn
StringBuilder builder = new StringBuilder();
CommentTracker tracker = new CommentTracker();
for (PsiElement child : callExpression.getChildren()) {
if (child instanceof PsiExpressionList) {
builder.append(createNewArgumentsFromCall(formatCallExpression, tracker));
if (child instanceof PsiExpressionList expressionList) {
builder.append(createNewArgumentsFromCall(formatCallExpression, tracker, expressionList.getExpressions()));
}
else {
builder.append(tracker.text(child));
@@ -428,7 +428,8 @@ public final class StringConcatenationArgumentToLogCallInspection extends BaseIn
}
private @NotNull String createNewArgumentsFromCall(@NotNull PsiMethodCallExpression formatCallExpression,
@NotNull CommentTracker tracker) {
@NotNull CommentTracker tracker,
PsiExpression @NotNull [] allArguments) {
List<String> arguments = new ArrayList<>();
List<Map.Entry<TextRange, Integer>> placeholders =
myTextMapping.entrySet()
@@ -445,6 +446,11 @@ public final class StringConcatenationArgumentToLogCallInspection extends BaseIn
}
arguments.add(formatWithPlaceholders);
Collections.reverse(arguments);
if (allArguments.length > 1) {
for (int i = 1; i < allArguments.length; i++) {
arguments.add(tracker.text(allArguments[i]));
}
}
return "(" + String.join(", ", arguments) + ")";
}
}
@@ -675,7 +681,11 @@ public final class StringConcatenationArgumentToLogCallInspection extends BaseIn
ProblemType problemType = null;
if (arguments.length == 1 && argument instanceof PsiMethodCallExpression callExpression) {
if (argument instanceof PsiMethodCallExpression callExpression && (
arguments.length == 1 ||
(arguments.length == 2 && arguments[1] != null &&
InheritanceUtil.isInheritor(arguments[1].getType(), CommonClassNames.JAVA_LANG_THROWABLE))
)) {
FormatDecode.FormatArgument formatArgument =
FormatDecode.FormatArgument.extract(callExpression, List.of("format"), List.of("String"), true);
if (formatArgument != null) {

View File

@@ -0,0 +1,12 @@
import org.slf4j.*;
import java.text.MessageFormat;
class SimpleMessageFormat {
Logger LOG = LoggerFactory.getLogger(SimpleMessageFormat.class);
void f() {
LOG.in<caret>fo("{}, 2 {} {}", 3.0, 2, "1", new Exception());
}
}

View File

@@ -0,0 +1,12 @@
import org.slf4j.*;
import java.text.MessageFormat;
class SimpleMessageFormat {
Logger LOG = LoggerFactory.getLogger(SimpleMessageFormat.class);
void f() {
LOG.in<caret>fo(MessageFormat.format("{2}, 2 {1} {0}", "1", 2, 3.0), new Exception());
}
}

View File

@@ -0,0 +1,12 @@
import org.slf4j.*;
import java.text.MessageFormat;
class SimpleStringFormat {
Logger LOG = LoggerFactory.getLogger(SimpleStringFormat.class);
void f() {
LOG.in<caret>fo("{} something {} {}", "text", true, 1, new RuntimeException());
}
}

View File

@@ -0,0 +1,12 @@
import org.slf4j.*;
import java.text.MessageFormat;
class SimpleStringFormat {
Logger LOG = LoggerFactory.getLogger(SimpleStringFormat.class);
void f() {
LOG.in<caret>fo(String.format("%s something %b %d", "text", true, 1), new RuntimeException());
}
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.fixes.logging;
import com.intellij.codeInspection.InspectionsBundle;
@@ -12,7 +12,11 @@ public class StringConcatenationArgumentToLogCallFixTest extends IGQuickFixesTes
public void setUp() throws Exception {
super.setUp();
myDefaultHint = InspectionGadgetsBundle.message("string.concatenation.argument.to.log.call.quickfix");
myFixture.addClass("package org.slf4j; public interface Logger { void info(String format); }");
myFixture.addClass("""
package org.slf4j; public interface Logger {
void info(String format);
void info(String format, Exception e);
}""");
myFixture.addClass("package org.slf4j; public final class LoggerFactory { public static Logger getLogger(Class clazz) { return null; }}");
myFixture.addClass("package org.apache.logging.log4j; public interface LogBuilder { void log(String format); LogBuilder withLocation(); }");
myFixture.addClass("package org.apache.logging.log4j; public interface Logger { LogBuilder atInfo(); void info(String format, Object... arguments); LogBuilder withLocation(); }");
@@ -56,6 +60,7 @@ public class StringConcatenationArgumentToLogCallFixTest extends IGQuickFixesTes
public void testMessageFormatFormatter() { assertQuickfixNotAvailable(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.message.format.call.quickfix")); }
public void testSimpleConcatenationInsideMethod() { doTest(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.call.quickfix")); }
public void testConcatenationMessageFormat() { doTest(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.message.format.call.quickfix")); }
public void testSimpleMessageFormatWithException() { doTest(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.message.format.call.quickfix")); }
public void testSimpleStringFormat() { doTest(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.string.format.call.quickfix")); }
public void testStringFormatWithWidth() { assertQuickfixNotAvailable(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.string.format.call.quickfix")); }
@@ -65,6 +70,7 @@ public class StringConcatenationArgumentToLogCallFixTest extends IGQuickFixesTes
public void testMoreArgumentsStringFormat() { assertQuickfixNotAvailable(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.string.format.call.quickfix")); }
public void testPreviousArgumentStringFormat() { assertQuickfixNotAvailable(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.string.format.call.quickfix")); }
public void testConcatenationStringFormat() { doTest(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.string.format.call.quickfix")); }
public void testSimpleStringFormatWithException() { doTest(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.string.format.call.quickfix")); }
@Override