mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
[java-inspections] --IDEA-320618 Invert if generates incorrect code inside loops with label (java)
GitOrigin-RevId: 8670f5b5414dba816a6254f4b0fc03acf79194d6
This commit is contained in:
committed by
intellij-monorepo-bot
parent
f4ebb69281
commit
3e995dc765
@@ -1,9 +1,9 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.modcommand.ModPsiUpdater;
|
||||
import com.intellij.codeInspection.PsiUpdateModCommandAction;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.modcommand.ModPsiUpdater;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
@@ -326,8 +326,9 @@ public class InvertIfConditionAction extends PsiUpdateModCommandAction<PsiElemen
|
||||
}
|
||||
}
|
||||
}
|
||||
if (thenBranch instanceof PsiContinueStatement ||
|
||||
thenBranch instanceof PsiReturnStatement && ((PsiReturnStatement)thenBranch).getReturnValue() == null) {
|
||||
if ((thenBranch instanceof PsiContinueStatement continueStatement &&
|
||||
(continueStatement.getLabelIdentifier() == null || theSameLabel(continueStatement.getLabelIdentifier(), loopStmt))) ||
|
||||
(thenBranch instanceof PsiReturnStatement && ((PsiReturnStatement)thenBranch).getReturnValue() == null)) {
|
||||
PsiStatement elseBranch = ifStatement.getElseBranch();
|
||||
if (elseBranch != null) {
|
||||
elseBranch.delete();
|
||||
@@ -344,6 +345,16 @@ public class InvertIfConditionAction extends PsiUpdateModCommandAction<PsiElemen
|
||||
ifStatement.setElseBranch(ct.markUnchanged(thenBranch));
|
||||
}
|
||||
|
||||
private static boolean theSameLabel(@NotNull PsiIdentifier label, @Nullable PsiLoopStatement stmt) {
|
||||
if (stmt == null) {
|
||||
return false;
|
||||
}
|
||||
if (stmt.getParent() instanceof PsiLabeledStatement labeledStatement) {
|
||||
return labeledStatement.getLabelIdentifier() == label;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static PsiStatement wrapWithCodeBlock(@NotNull PsiStatement statement) {
|
||||
final Project project = statement.getProject();
|
||||
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// "Invert 'if' condition" "true-preview"
|
||||
class A {
|
||||
void foo () {
|
||||
outer: for(int i = 0; i < 5; i++) {
|
||||
for(int j = 0; j < 5; j++) {
|
||||
if (j != 3) {
|
||||
System.out.println("J:" + j);
|
||||
}
|
||||
else {
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
System.out.println("I:" + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Invert 'if' condition" "true-preview"
|
||||
class A {
|
||||
void foo () {
|
||||
outer: for(int i = 0; i < 5; i++) {
|
||||
for(int j = 0; j < 5; j++) {
|
||||
i<caret>f (j == 3)
|
||||
continue outer;
|
||||
System.out.println("J:" + j);
|
||||
}
|
||||
System.out.println("I:" + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user