[java] redundant cast: fix AIOOBE

found by property testing

GitOrigin-RevId: 220de5134c090e370b3ade0016e0879b66569202
This commit is contained in:
Anna Kozlova
2021-10-11 19:14:56 +00:00
committed by intellij-monorepo-bot
parent 9e24cc369f
commit 2278b4a6fe
2 changed files with 6 additions and 1 deletions
@@ -2,6 +2,6 @@ import java.util.Map;
class MyTest {
public static void println(Map<String, Object> params) {
System.out.println(String.format("Some output: %d", (Integer)params.get("numberKey")));
System.out.println(String.format((<warning descr="Casting '\"Some output: %d\"' to 'String' is redundant">String</warning>)"Some output: %d", (Integer)params.get("numberKey")));
}
}
@@ -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()) &&