From 2278b4a6fe5c69ff000ad9e9bd5382038f37c047 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 11 Oct 2021 18:19:25 +0200 Subject: [PATCH] [java] redundant cast: fix AIOOBE found by property testing GitOrigin-RevId: 220de5134c090e370b3ade0016e0879b66569202 --- .../redundantCast/lambda/IgnoreMessageFormatCall.java | 2 +- .../src/com/siyeh/ig/bugs/FormatDecode.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/java/java-tests/testData/inspection/redundantCast/lambda/IgnoreMessageFormatCall.java b/java/java-tests/testData/inspection/redundantCast/lambda/IgnoreMessageFormatCall.java index 27033d6d9d9d..69df6c870c68 100644 --- a/java/java-tests/testData/inspection/redundantCast/lambda/IgnoreMessageFormatCall.java +++ b/java/java-tests/testData/inspection/redundantCast/lambda/IgnoreMessageFormatCall.java @@ -2,6 +2,6 @@ import java.util.Map; class MyTest { public static void println(Map params) { - System.out.println(String.format("Some output: %d", (Integer)params.get("numberKey"))); + System.out.println(String.format((String)"Some output: %d", (Integer)params.get("numberKey"))); } } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/FormatDecode.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/FormatDecode.java index 45115d225dd3..5cf1f4e80d76 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/FormatDecode.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/FormatDecode.java @@ -330,6 +330,11 @@ public final class FormatDecode { int idx = IntStream.range(0, arguments.length) .filter(i -> PsiTreeUtil.isAncestor(arguments[i], cast, false)).findFirst() .orElse(-1); + + if (idx < formatArgumentIndex) { + return false; + } + Validator validator = validators[idx - formatArgumentIndex]; PsiTypeElement castType = cast.getCastType(); return validator.valid(Objects.requireNonNull(castType).getType()) &&