(true/*7*/) /*8*/ {
+ if(i >= 12/*1*/)/*2*/ break;/*3*/
+ /*4*/
+ i =/*5*/ i + 1;/*6*/
+ }
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/MoveConditionToLoopInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/MoveConditionToLoopInspectionTest.java
new file mode 100644
index 000000000000..dc7acba59d19
--- /dev/null
+++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/MoveConditionToLoopInspectionTest.java
@@ -0,0 +1,26 @@
+// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
+
+package com.intellij.java.codeInsight.daemon.quickFix;
+
+import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
+import com.intellij.codeInspection.LocalInspectionTool;
+import com.intellij.codeInspection.MoveConditionToLoopInspection;
+import org.jetbrains.annotations.NotNull;
+
+public class MoveConditionToLoopInspectionTest extends LightQuickFixParameterizedTestCase {
+ @NotNull
+ @Override
+ protected LocalInspectionTool[] configureLocalInspectionTools() {
+ return new LocalInspectionTool[]{new MoveConditionToLoopInspection()};
+ }
+
+ public void test() {
+ doAllTests();
+ }
+
+
+ @Override
+ protected String getBasePath() {
+ return "/codeInsight/moveConditionToLoop/";
+ }
+}
\ No newline at end of file
diff --git a/platform/platform-resources-en/src/messages/InspectionsBundle.properties b/platform/platform-resources-en/src/messages/InspectionsBundle.properties
index 6bb8eff1c1fb..af73962402ef 100644
--- a/platform/platform-resources-en/src/messages/InspectionsBundle.properties
+++ b/platform/platform-resources-en/src/messages/InspectionsBundle.properties
@@ -929,4 +929,8 @@ navigate.to.duplicate.fix=Navigate to duplicate
inspection.idempotent.loop.body=Idempotent loop body
inspection.undeclared.service.usage.name=Usage of service not declared in module-info
-inspection.undeclared.service.usage.message=Usage of service ''{0}'' is not declared in module-info
\ No newline at end of file
+inspection.undeclared.service.usage.message=Usage of service ''{0}'' is not declared in module-info
+
+inspection.move.condition.to.loop=Move condition to loop
+inspection.move.condition.to.loop.no.conversion.to.do.while=Don't suggest to replace to 'do while'
+inspection.move.condition.to.loop.description=Conditional break inside infinite loop
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java
index 81e71f1b2dc3..7a2abd87eaf0 100644
--- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java
+++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java
@@ -127,6 +127,25 @@ public class ControlFlowUtils {
}
}
+ @Contract(value = "null -> false", pure = true)
+ public static boolean isEndlessLoop(@Nullable PsiLoopStatement loopStatement) {
+ if(loopStatement == null) return false;
+ if (loopStatement instanceof PsiWhileStatement) {
+ return BoolUtils.isTrue(((PsiWhileStatement)loopStatement).getCondition());
+ }
+ if (loopStatement instanceof PsiDoWhileStatement) {
+ return BoolUtils.isTrue(((PsiDoWhileStatement)loopStatement).getCondition());
+ }
+ if (loopStatement instanceof PsiForStatement) {
+ PsiForStatement forStatement = (PsiForStatement)loopStatement;
+ PsiExpression condition = forStatement.getCondition();
+ if(condition != null && !BoolUtils.isTrue(condition)) return false;
+ return (forStatement.getInitialization() == null || forStatement.getInitialization() instanceof PsiEmptyStatement)
+ && (forStatement.getUpdate() == null || forStatement.getUpdate() instanceof PsiEmptyStatement);
+ }
+ return false;
+ }
+
private static boolean doWhileStatementMayCompleteNormally(@NotNull PsiDoWhileStatement loopStatement) {
final PsiExpression condition = loopStatement.getCondition();
final Object value = ExpressionUtils.computeConstantExpression(condition);
diff --git a/resources-en/src/inspectionDescriptions/MoveConditionToLoop.html b/resources-en/src/inspectionDescriptions/MoveConditionToLoop.html
new file mode 100644
index 000000000000..e9a6f520ca22
--- /dev/null
+++ b/resources-en/src/inspectionDescriptions/MoveConditionToLoop.html
@@ -0,0 +1,19 @@
+
+
+This inspection detects conditional breaks at the end or beginning and suggests move condition to loop
+
+Example:
+
+ while(true) {
+ if(i == 23) break;
+ i++;
+ }
+
+Will be replaced with:
+
+ while(i != 23) {
+ i++;
+ }
+
+
+
\ No newline at end of file