mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
java MalformedFormatStringInspection: support String#formatted (IDEA-241332)
GitOrigin-RevId: 35ddcf54b78d47258ea90f025717631e05545e61
This commit is contained in:
committed by
intellij-monorepo-bot
parent
7251fdcd51
commit
a0cf47d539
+4
@@ -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...");
|
||||
|
||||
|
||||
/**
|
||||
*/
|
||||
|
||||
+20
-9
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user