java redundant cast: fixed multiple casts in synchronized stmt

GitOrigin-RevId: 49a7d8dbcd26a3aa905c57db97d386bcbd285464
This commit is contained in:
Anna Kozlova
2020-05-07 10:10:36 +02:00
committed by intellij-monorepo-bot
parent f0233d4e68
commit fdc80d017c
3 changed files with 14 additions and 4 deletions

View File

@@ -680,18 +680,20 @@ public class RedundantCastUtil {
}
}
}
@Override
public void visitSynchronizedStatement(PsiSynchronizedStatement statement) {
PsiExpression lockExpression = deparenthesizeExpression(statement.getLockExpression());
if (lockExpression instanceof PsiTypeCastExpression) {
PsiExpression operand = deparenthesizeExpression(((PsiTypeCastExpression)lockExpression).getOperand());
while (operand instanceof PsiTypeCastExpression) {
operand = deparenthesizeExpression(((PsiTypeCastExpression)operand).getOperand());
}
if (operand != null) {
PsiType opType = operand.getType();
if (operand instanceof PsiFunctionalExpression || opType instanceof PsiPrimitiveType || opType == null) {
return;
if (!(operand instanceof PsiFunctionalExpression) && !(opType instanceof PsiPrimitiveType) && opType != null) {
addIfNarrowing((PsiTypeCastExpression)lockExpression, opType, null);
}
addIfNarrowing((PsiTypeCastExpression)lockExpression, opType, null);
}
}
super.visitSynchronizedStatement(statement);

View File

@@ -0,0 +1,7 @@
class MyTest {
void dblCast() {
synchronized ((Runnable) (<warning descr="Casting '() -> {...}' to 'Runnable' is redundant">Runnable</warning>) () -> {}) {}
synchronized ((Runnable) (<warning descr="Casting '(Runnable)() -> {...}' to 'Runnable' is redundant">Runnable</warning>)(<warning descr="Casting '() -> {...}' to 'Runnable' is redundant">Runnable</warning>) () -> {}) {}
}
}

View File

@@ -86,6 +86,7 @@ public class LambdaRedundantCastTest extends LightDaemonAnalyzerTestCase {
public void testCastInNeighbourArgument() { doTest(); }
public void testErasedTargetType() { doTest(); }
public void testThrowsStatementInLambdaBody() { doTest(); }
public void testSynchronizeCasts() { doTest(); }
public void testRejectReturnTypeChange() {
doTest();
}