[java-inspections] EA-1160494 - IOE: BasicJavaParserUtil.parseFragment

GitOrigin-RevId: 5a13ceb97f8105766928d67336fe9a87df62053a
This commit is contained in:
Tagir Valeev
2024-06-11 10:30:06 +02:00
committed by intellij-monorepo-bot
parent ab6fad9379
commit 49ed49dc2d
3 changed files with 20 additions and 5 deletions

View File

@@ -73,9 +73,8 @@ public final class IntegerMultiplicationImplicitCastToLongInspection extends Bas
@SuppressWarnings("PublicField")
public boolean ignoreNonOverflowingCompileTimeConstants = true;
@Nullable
@Override
protected LocalQuickFix buildFix(Object... infos) {
protected @NotNull LocalQuickFix buildFix(Object... infos) {
return new IntegerMultiplicationImplicitCastToLongInspectionFix();
}
@@ -180,8 +179,8 @@ public final class IntegerMultiplicationImplicitCastToLongInspection extends Bas
else {
exprToCast = Arrays.stream(operands)
.map(operand -> PsiUtil.deparenthesizeExpression(operand))
.filter(operand -> operand instanceof PsiLiteralExpression ||
operand instanceof PsiPrefixExpression && ((PsiPrefixExpression)operand).getOperand() instanceof PsiLiteral)
.filter(operand -> isIntegerLiteral(operand) ||
operand instanceof PsiPrefixExpression prefixExpr && isIntegerLiteral(prefixExpr.getOperand()))
.findFirst()
.orElse(operands[0]);
}
@@ -189,6 +188,10 @@ public final class IntegerMultiplicationImplicitCastToLongInspection extends Bas
addCast(exprToCast);
}
private static boolean isIntegerLiteral(@Nullable PsiExpression operand) {
return operand instanceof PsiLiteralExpression literal && literal.getValue() instanceof Integer;
}
private static void addCast(@NotNull PsiExpression expression) {
if (expression instanceof PsiPrefixExpression) {
final PsiExpression operand = ((PsiPrefixExpression)expression).getOperand();
@@ -196,7 +199,7 @@ public final class IntegerMultiplicationImplicitCastToLongInspection extends Bas
}
final String replacementText;
if (expression instanceof PsiLiteralExpression) {
if (isIntegerLiteral(expression)) {
replacementText = expression.getText() + "L";
}
else {

View File

@@ -0,0 +1,6 @@
// "Fix all 'Integer multiplication or shift implicitly cast to 'long'' problems in file" "true"
class X {
void test(int x) {
long y = (long) x * 'a';
}
}

View File

@@ -0,0 +1,6 @@
// "Fix all 'Integer multiplication or shift implicitly cast to 'long'' problems in file" "true"
class X {
void test(int x) {
long y = x *<caret> 'a';
}
}