diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/FormatUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/FormatUtils.java index 5e4271a31961..9fb483dd2ce3 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/FormatUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/FormatUtils.java @@ -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..."); + /** */ diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/MalformedFormatStringInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/MalformedFormatStringInspection.java index 2e3799504f3f..6e910fd35d92 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/MalformedFormatStringInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/MalformedFormatStringInspection.java @@ -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);