From ea5e79ca145e196c59c3b22853ec800d5fda8b04 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 10 Aug 2020 15:36:57 +0700 Subject: [PATCH] [java-inspections] StreamApiMigration: parentheses like (br).readLine() GitOrigin-RevId: 9f71c7b1a41cd74abd0e51a0637a3da13ce955b6 --- .../StreamApiMigrationInspection.java | 43 ++++++++++--------- .../beforeBufferedReaderCollectForStyle.java | 2 +- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java index 8fd947e80081..e6a6c26831d0 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java @@ -24,6 +24,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.psi.util.TypeConversionUtil; import com.intellij.util.ArrayUtil; +import com.intellij.util.containers.ContainerUtil; import com.siyeh.ig.callMatcher.CallMatcher; import com.siyeh.ig.psiutils.*; import one.util.streamex.StreamEx; @@ -474,8 +475,8 @@ public class StreamApiMigrationInspection extends AbstractBaseJavaLocalInspectio } Collection exitPoints = tb.findExitPoints(controlFlow); if (exitPoints == null) return null; - boolean onlyNonLabeledContinue = StreamEx.of(exitPoints) - .allMatch(statement -> statement instanceof PsiContinueStatement && ((PsiContinueStatement)statement).getLabelIdentifier() == null); + boolean onlyNonLabeledContinue = ContainerUtil.and( + exitPoints, statement -> statement instanceof PsiContinueStatement && ((PsiContinueStatement)statement).getLabelIdentifier() == null); if (onlyNonLabeledContinue && nonFinalVariables.isEmpty()) { boolean shouldWarn = suggestForeach && (replaceTrivialForEach || @@ -536,25 +537,27 @@ public class StreamApiMigrationInspection extends AbstractBaseJavaLocalInspectio private static BaseStreamApiMigration findMigrationForReturn(PsiStatement statement, TerminalBlock tb, boolean replaceTrivialForEach) { boolean shouldWarn = replaceTrivialForEach || tb.hasOperations(); PsiReturnStatement returnStatement = (PsiReturnStatement)tb.getSingleStatement(); - PsiExpression value = returnStatement.getReturnValue(); + if (returnStatement == null) return null; + PsiExpression value = PsiUtil.skipParenthesizedExprDown(returnStatement.getReturnValue()); PsiReturnStatement nextReturnStatement = ControlFlowUtils.getNextReturnStatement(statement); - if (nextReturnStatement != null && - (ExpressionUtils.isLiteral(value, Boolean.TRUE) || ExpressionUtils.isLiteral(value, Boolean.FALSE))) { - boolean foundResult = (boolean)((PsiLiteralExpression)value).getValue(); - String methodName; - if (foundResult) { - methodName = "anyMatch"; - } - else { - methodName = "noneMatch"; - FilterOp lastFilter = tb.getLastOperation(FilterOp.class); - if (lastFilter != null && (lastFilter.isNegated() ^ BoolUtils.isNegation(lastFilter.getExpression()))) { - methodName = "allMatch"; + if (nextReturnStatement != null && value instanceof PsiLiteralExpression) { + Boolean foundResult = tryCast(((PsiLiteralExpression)value).getValue(), Boolean.class); + if (foundResult != null) { + String methodName; + if (foundResult) { + methodName = "anyMatch"; + } + else { + methodName = "noneMatch"; + FilterOp lastFilter = tb.getLastOperation(FilterOp.class); + if (lastFilter != null && (lastFilter.isNegated() ^ BoolUtils.isNegation(lastFilter.getExpression()))) { + methodName = "allMatch"; + } + } + if (nextReturnStatement.getParent() == statement.getParent() || + ExpressionUtils.isLiteral(nextReturnStatement.getReturnValue(), !foundResult)) { + return new MatchMigration(shouldWarn, methodName); } - } - if (nextReturnStatement.getParent() == statement.getParent() || - ExpressionUtils.isLiteral(nextReturnStatement.getReturnValue(), !foundResult)) { - return new MatchMigration(shouldWarn, methodName); } } if (!VariableAccessUtils.variableIsUsed(tb.getVariable(), value)) { @@ -1022,7 +1025,7 @@ public class StreamApiMigrationInspection extends AbstractBaseJavaLocalInspectio } PsiMethodCallExpression maybeReadLines = tryCast(PsiUtil.skipParenthesizedExprDown(lineVar.getInitializer()), PsiMethodCallExpression.class); if (!BUFFERED_READER_READ_LINE.test(maybeReadLines)) return null; - PsiExpression reader = maybeReadLines.getMethodExpression().getQualifierExpression(); + PsiExpression reader = PsiUtil.skipParenthesizedExprDown(maybeReadLines.getMethodExpression().getQualifierExpression()); PsiReferenceExpression readerRef = tryCast(reader, PsiReferenceExpression.class); if (readerRef == null) return null; PsiVariable readerVar = tryCast(readerRef.resolve(), PsiVariable.class); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/bufferedReader/beforeBufferedReaderCollectForStyle.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/bufferedReader/beforeBufferedReaderCollectForStyle.java index 8bff50eac361..2a13708b6cf0 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/bufferedReader/beforeBufferedReaderCollectForStyle.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/bufferedReader/beforeBufferedReaderCollectForStyle.java @@ -8,7 +8,7 @@ import java.util.List; public class Main { List test(BufferedReader br) throws IOException { List result = new ArrayList<>(); - for (String line = ((br.readLine())); line != null; line = (br.readLine())) { + for (String line = (((br).readLine())); line != null; line = (br.readLine())) { result.add(line.trim()); } return result;