[java-inspections] Disallow PatternVariableCanBeUsed inspection on upcasting

GitOrigin-RevId: 89f15c8cbcc84d339973bb5ad1978c7409805fa3
This commit is contained in:
Tagir Valeev
2021-01-26 05:55:44 +00:00
committed by intellij-monorepo-bot
parent 6fe2638869
commit b9161cc09c
2 changed files with 14 additions and 1 deletions
@@ -28,10 +28,14 @@ public class PatternVariableCanBeUsedInspection extends AbstractBaseJavaLocalIns
if (identifier == null) return;
PsiTypeCastExpression cast = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(variable.getInitializer()),
PsiTypeCastExpression.class);
if (cast == null || cast.getOperand() == null || cast.getCastType() == null) return;
if (cast == null || cast.getCastType() == null) return;
PsiExpression operand = cast.getOperand();
if (operand == null) return;
PsiType castType = cast.getCastType().getType();
if (castType instanceof PsiPrimitiveType) return;
if (!variable.getType().equals(castType)) return;
PsiType operandType = operand.getType();
if (operandType == null || castType.isAssignableFrom(operandType)) return;
PsiElement scope = PsiUtil.getVariableCodeBlock(variable, null);
if (scope == null) return;
PsiDeclarationStatement declaration = ObjectUtils.tryCast(variable.getParent(), PsiDeclarationStatement.class);
@@ -0,0 +1,9 @@
// "Replace 'obj' with pattern variable" "false"
class X {
void test(String s) {
if (s instanceof Object) {
Object <caret>obj = (Object) s;
System.out.println(obj);
}
}
}