java MalformedFormatStringInspection: support String#formatted (IDEA-241332)

GitOrigin-RevId: 35ddcf54b78d47258ea90f025717631e05545e61
This commit is contained in:
Anna Kozlova
2020-05-20 06:38:48 +00:00
committed by intellij-monorepo-bot
parent 7251fdcd51
commit a0cf47d539
2 changed files with 24 additions and 9 deletions
@@ -17,6 +17,7 @@ package com.siyeh.ig.psiutils;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.ig.callMatcher.CallMatcher;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
@@ -26,6 +27,9 @@ import java.util.List;
import java.util.Set;
public class FormatUtils {
public static final CallMatcher STRING_FORMATTED = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "formatted")
.parameterTypes("java.lang.Object...");
/**
*/
@@ -145,17 +145,28 @@ public class MalformedFormatStringInspection extends BaseInspection {
@Override
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
if (!FormatUtils.isFormatCall(expression, methodNames, classNames)) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
PsiExpression[] arguments = argumentList.getExpressions();
int formatArgumentIndex = findFirstStringArgumentIndex(arguments);
if (formatArgumentIndex < 0) {
return;
final PsiExpression formatArgument;
int formatArgumentIndex;
if (FormatUtils.STRING_FORMATTED.matches(expression)) {
formatArgument = expression.getMethodExpression().getQualifierExpression();
formatArgumentIndex = 0;
}
else {
if (!FormatUtils.isFormatCall(expression, methodNames, classNames)) {
return;
}
formatArgumentIndex = findFirstStringArgumentIndex(arguments);
if (formatArgumentIndex < 0) {
return;
}
formatArgument = arguments[formatArgumentIndex];
formatArgumentIndex++;
}
final PsiExpression formatArgument = arguments[formatArgumentIndex];
formatArgumentIndex++;
if (!ExpressionUtils.hasStringType(formatArgument) || !PsiUtil.isConstantExpression(formatArgument)) {
return;
}
@@ -167,7 +178,7 @@ public class MalformedFormatStringInspection extends BaseInspection {
if (value == null) {
return;
}
int argumentCount = arguments.length - (formatArgumentIndex);
int argumentCount = arguments.length - formatArgumentIndex;
final FormatDecode.Validator[] validators;
try {
validators = FormatDecode.decode(value, argumentCount);