From a310b65b13f2c656dc551d580feac8d33178d0af Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Fri, 15 Jul 2016 19:38:55 +0300 Subject: [PATCH] Java inspection: In "Add Braces" and "Remove Braces" inspections don't offer the fix on an if/loop body if it contains another if/loop to avoid ambiguity. Tests added. (IDEA-157727) --- .../siyeh/InspectionGadgetsBundle.properties | 6 +- .../ControlFlowStatementVisitorBase.java | 84 +++++++++++---- ...lFlowStatementWithoutBracesInspection.java | 16 +++ .../SingleStatementInBlockInspection.java | 26 ++++- .../LadderInnerElse.after.java | 13 +++ .../LadderInnerElse.java | 11 ++ .../LadderInnerFor.after.java | 12 +++ .../LadderInnerFor.java | 11 ++ .../LadderInnerIf.after.java | 11 ++ .../LadderInnerIf.java | 11 ++ .../LadderOuterElse.after.java | 13 +++ .../LadderOuterElse.java | 11 ++ .../LadderOuterFor.after.java | 12 +++ .../LadderOuterFor.java | 11 ++ .../LadderOuterIf.after.java | 11 ++ .../LadderOuterIf.java | 11 ++ .../LadderOutside.java | 12 +++ .../SingleStatement.java | 63 +++++++++++ .../style/single_statement_block/expected.xml | 102 ++++++++++++++++++ .../ControlFlowStatements.java | 10 ++ .../statements_without_braces/expected.xml | 42 ++++++++ ...trolFlowStatementWithoutBracesFixTest.java | 8 ++ .../braces/SingleStatementInBlockFixTest.java | 4 +- .../SingleStatementInBlockInspectionTest.java | 27 +++++ 24 files changed, 506 insertions(+), 32 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerElse.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerElse.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerFor.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerFor.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerIf.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerIf.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterElse.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterElse.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterFor.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterFor.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterIf.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterIf.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOutside.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/style/single_statement_block/SingleStatement.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/style/single_statement_block/expected.xml create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/SingleStatementInBlockInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index bbc991519da8..86b92f43163e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1334,7 +1334,7 @@ chained.method.call.ignore.this.super.option=Ignore chained method calls in 'thi introduce.variable.quickfix=Introduce variable introduce.variable.may.change.semantics.quickfix=Introduce variable (may change semantics) flip.comparison.quickfix=Flip comparison -control.flow.statement.without.braces.add.quickfix=Add braces +control.flow.statement.without.braces.add.quickfix=Add braces to statement control.flow.statement.without.braces.message=Add braces to ''{0}'' statement extends.object.remove.quickfix=Remove redundant 'extends Object' implicit.call.to.super.ignore.option=Ignore for direct subclasses of 'java.lang.Object' @@ -2204,4 +2204,6 @@ replace.equality.with.equals.descriptor=Replace ''{0}'' with ''{1}equals()'' replace.equality.with.safe.equals.name=Replace Equality with Safe Equals replace.equality.with.safe.equals.descriptor=Replace ''{0}'' with safe ''{1}equals()'' single.statement.in.block.name=Code Block Contains Single Statement -single.statement.in.block.descriptor=Remove braces from ''{0}'' statement \ No newline at end of file +single.statement.in.block.descriptor=''{0}'' contains single statement +single.statement.in.block.quickfix=Remove braces from ''{0}'' statement +single.statement.in.block.family.quickfix=Remove Braces From Statement \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementVisitorBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementVisitorBase.java index f216c7f6e222..cf2400063ac5 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementVisitorBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementVisitorBase.java @@ -19,11 +19,14 @@ import com.intellij.codeHighlighting.HighlightDisplayLevel; import com.intellij.codeInsight.daemon.HighlightDisplayKey; import com.intellij.codeInspection.InspectionProfile; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Pair; import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.*; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public abstract class ControlFlowStatementVisitorBase extends BaseInspectionVisitor { private final HighlightDisplayKey myKey; @@ -36,33 +39,37 @@ public abstract class ControlFlowStatementVisitorBase extends BaseInspectionVisi @Override public void visitForeachStatement(PsiForeachStatement statement) { super.visitForeachStatement(statement); - if (isApplicable(statement.getBody())) { - registerKeywordOrStatementError(statement, PsiKeyword.FOR); + final PsiStatement body = statement.getBody(); + if (isApplicable(body)) { + registerLoopStatementErrors(statement, body, PsiKeyword.FOR); } } - @Override public void visitForStatement(PsiForStatement statement) { super.visitForStatement(statement); - if (isApplicable(statement.getBody())) { - registerKeywordOrStatementError(statement, PsiKeyword.FOR); + final PsiStatement body = statement.getBody(); + if (isApplicable(body)) { + registerLoopStatementErrors(statement, body, PsiKeyword.FOR); } } + @Override public void visitWhileStatement(PsiWhileStatement statement) { super.visitWhileStatement(statement); - if (isApplicable(statement.getBody())) { - registerKeywordOrStatementError(statement, PsiKeyword.WHILE); + final PsiStatement body = statement.getBody(); + if (isApplicable(body)) { + registerLoopStatementErrors(statement, body, PsiKeyword.WHILE); } } @Override public void visitDoWhileStatement(PsiDoWhileStatement statement) { super.visitDoWhileStatement(statement); - if (isApplicable(statement.getBody())) { - registerKeywordOrStatementError(statement, PsiKeyword.DO); + final PsiStatement body = statement.getBody(); + if (isApplicable(body)) { + registerLoopStatementErrors(statement, body, PsiKeyword.DO); } } @@ -71,34 +78,65 @@ public abstract class ControlFlowStatementVisitorBase extends BaseInspectionVisi super.visitIfStatement(statement); final PsiStatement thenBranch = statement.getThenBranch(); if (isApplicable(thenBranch)) { - registerKeywordOrStatementError(statement.getFirstChild(), thenBranch, PsiKeyword.IF); + registerControlFlowStatementErrors(statement.getFirstChild(), thenBranch.getLastChild(), thenBranch, PsiKeyword.IF); } final PsiStatement elseBranch = statement.getElseBranch(); if (isApplicable(elseBranch)) { - registerKeywordOrStatementError(statement.getElseElement(), elseBranch, PsiKeyword.ELSE); + registerControlFlowStatementErrors(statement.getElseElement(), elseBranch.getLastChild(), elseBranch, PsiKeyword.ELSE); } } @Contract("null->false") protected abstract boolean isApplicable(PsiStatement body); - private void registerKeywordOrStatementError(PsiStatement statement, String text) { - boolean highlightOnlyKeyword = isHighlightOnlyKeyword(statement); - if (highlightOnlyKeyword) { - registerStatementError(statement, text); - } - else { - registerError(statement, text); - } + @Nullable + protected abstract Pair getOmittedBodyBounds(PsiStatement body); + + private void registerLoopStatementErrors(@NotNull PsiLoopStatement statement, @NotNull PsiStatement body, @NotNull String keywordText) { + registerControlFlowStatementErrors(statement.getFirstChild(), statement.getLastChild(), body, keywordText); } - private void registerKeywordOrStatementError(PsiElement keyword, PsiStatement body, String text) { + private void registerControlFlowStatementErrors(@Nullable PsiElement rangeStart, + @Nullable PsiElement rangeEnd, + @NotNull PsiStatement body, + @NotNull String keywordText) { boolean highlightOnlyKeyword = isHighlightOnlyKeyword(body); if (highlightOnlyKeyword) { - registerError(keyword != null ? keyword : body, text); + if (rangeStart != null) { + registerError(rangeStart, keywordText); + } + return; } - else { - registerErrorAtRange(keyword != null ? keyword : body, body, text); + + final Pair omittedBodyBounds = getOmittedBodyBounds(body); + if (omittedBodyBounds == null) { + if (rangeStart != null && rangeEnd != null) { + registerErrorAtRange(rangeStart, rangeEnd, keywordText); + } + return; + } + + if (rangeStart != null) { + final PsiElement beforeOmitted = omittedBodyBounds.getFirst(); + final PsiElement endOfHighlight = beforeOmitted != null ? beforeOmitted : rangeStart; + registerErrorAtRange(rangeStart, endOfHighlight, keywordText); + } + + final PsiElement afterOmitted = omittedBodyBounds.getSecond(); + if (afterOmitted != null) { + PsiElement endOfHighlight = afterOmitted; + if (rangeEnd != null && rangeEnd != afterOmitted) { + if (afterOmitted.getParent() == rangeEnd) { + final PsiElement rangeEndLastChild = rangeEnd.getLastChild(); + if (rangeEndLastChild != null) { + endOfHighlight = rangeEndLastChild; + } + } + else { + endOfHighlight = rangeEnd; + } + } + registerErrorAtRange(afterOmitted, endOfHighlight, keywordText); } } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementWithoutBracesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementWithoutBracesInspection.java index 83c636e6f932..7356a59093bc 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementWithoutBracesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementWithoutBracesInspection.java @@ -17,7 +17,9 @@ package com.siyeh.ig.style; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Pair; import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; @@ -26,6 +28,7 @@ import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.PsiReplacementUtil; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class ControlFlowStatementWithoutBracesInspection extends BaseInspection { @@ -139,5 +142,18 @@ public class ControlFlowStatementWithoutBracesInspection protected boolean isApplicable(PsiStatement body) { return body != null && !(body instanceof PsiBlockStatement); } + + @Nullable + @Override + protected Pair getOmittedBodyBounds(PsiStatement body) { + if (body instanceof PsiLoopStatement || body instanceof PsiIfStatement) { + final PsiElement lastChild = body.getLastChild(); + return Pair.create(PsiTreeUtil.skipSiblingsBackward(body, PsiWhiteSpace.class, PsiComment.class), + lastChild instanceof PsiJavaToken && ((PsiJavaToken)lastChild).getTokenType() == JavaTokenType.SEMICOLON + ? lastChild + : null); + } + return null; + } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java index 8bea4b5b0c4b..5205a4e29a2c 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java @@ -17,6 +17,7 @@ package com.siyeh.ig.style; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Pair; import com.intellij.psi.*; import com.intellij.psi.util.FileTypeUtils; import com.siyeh.InspectionGadgetsBundle; @@ -98,10 +99,9 @@ public class SingleStatementInBlockInspection extends BaseInspection { @Override protected boolean isApplicable(PsiStatement body) { if (body instanceof PsiBlockStatement) { - final PsiBlockStatement statement = (PsiBlockStatement)body; - final PsiStatement[] statements = statement.getCodeBlock().getStatements(); + final PsiStatement[] statements = ((PsiBlockStatement)body).getCodeBlock().getStatements(); if (statements.length == 1 && !(statements[0] instanceof PsiDeclarationStatement)) { - final PsiFile file = statement.getContainingFile(); + final PsiFile file = body.getContainingFile(); //this inspection doesn't work in JSP files, as it can't tell about tags // inside the braces if (!FileTypeUtils.isInServerPageFile(file)) { @@ -111,6 +111,22 @@ public class SingleStatementInBlockInspection extends BaseInspection { } return false; } + + @Nullable + @Override + protected Pair getOmittedBodyBounds(PsiStatement body) { + if (body instanceof PsiBlockStatement) { + final PsiCodeBlock codeBlock = ((PsiBlockStatement)body).getCodeBlock(); + final PsiStatement[] statements = codeBlock.getStatements(); + if (statements.length == 1) { + final PsiStatement statement = statements[0]; + if (statement instanceof PsiLoopStatement || statement instanceof PsiIfStatement) { + return Pair.create(codeBlock.getLBrace(), codeBlock.getRBrace()); + } + } + } + return null; + } } private static class SingleStatementInBlockFix extends InspectionGadgetsFix { @@ -124,14 +140,14 @@ public class SingleStatementInBlockInspection extends BaseInspection { @NotNull @Override public String getName() { - return InspectionGadgetsBundle.message("single.statement.in.block.descriptor", myKeywordText); + return InspectionGadgetsBundle.message("single.statement.in.block.quickfix", myKeywordText); } @Nls @NotNull @Override public String getFamilyName() { - return InspectionGadgetsBundle.message("single.statement.in.block.name"); + return InspectionGadgetsBundle.message("single.statement.in.block.family.quickfix"); } @Override diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerElse.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerElse.after.java new file mode 100644 index 000000000000..8cf30f1f75d8 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerElse.after.java @@ -0,0 +1,13 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else { + System.out.println(0); + } + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerElse.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerElse.java new file mode 100644 index 000000000000..79e7a47210b1 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerElse.java @@ -0,0 +1,11 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerFor.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerFor.after.java new file mode 100644 index 000000000000..66286dd849b7 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerFor.after.java @@ -0,0 +1,12 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) { + System.out.println(arg.charAt(i)); + } + else System.out.println(0); + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerFor.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerFor.java new file mode 100644 index 000000000000..38f9261ff759 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerFor.java @@ -0,0 +1,11 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerIf.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerIf.after.java new file mode 100644 index 000000000000..536fb3debfbf --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerIf.after.java @@ -0,0 +1,11 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) { + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + } else System.out.println(0); + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerIf.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerIf.java new file mode 100644 index 000000000000..a0c5781a06a5 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderInnerIf.java @@ -0,0 +1,11 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterElse.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterElse.after.java new file mode 100644 index 000000000000..6c34b2d4abc8 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterElse.after.java @@ -0,0 +1,13 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + else { + System.out.println("no"); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterElse.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterElse.java new file mode 100644 index 000000000000..060ba51ecd98 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterElse.java @@ -0,0 +1,11 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterFor.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterFor.after.java new file mode 100644 index 000000000000..bc859bed1361 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterFor.after.java @@ -0,0 +1,12 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) { + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + } + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterFor.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterFor.java new file mode 100644 index 000000000000..87abf3479d40 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterFor.java @@ -0,0 +1,11 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterIf.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterIf.after.java new file mode 100644 index 000000000000..adcbddf35aa2 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterIf.after.java @@ -0,0 +1,11 @@ +class X { + void ff(String[] a) { + if (a.length != 0) { + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + } else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterIf.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterIf.java new file mode 100644 index 000000000000..19c9cfc162e3 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOuterIf.java @@ -0,0 +1,11 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + else System.out.println("no"); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOutside.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOutside.java new file mode 100644 index 000000000000..b8f36de83783 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/statement_without_braces/LadderOutside.java @@ -0,0 +1,12 @@ +class X { + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + else System.out.println("no"); + + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/single_statement_block/SingleStatement.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/single_statement_block/SingleStatement.java new file mode 100644 index 000000000000..12af1bec3acf --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/single_statement_block/SingleStatement.java @@ -0,0 +1,63 @@ +class T { + + void f(String[] a) { + for (String s : a) { + System.out.println(s); + } + + if (a.length == 0) { + System.out.println("no"); + } else { + System.out.println(a.length); + } + + if (a.length == 0) { + System.out.println("no"); + } + + if (a.length == 0) { + } else { + System.out.println(a.length); + } + + for (int i = 0; i < a.length; i++) { + System.out.println(a[i]); + } + + int j = 0; + do { + System.out.println(a[j++]); + } + while (j < a.length); + + int k = 0; + while (k < a.length) { + System.out.println(a[k++]); + } + } + + void ff(String[] a) { + if (a.length != 0) { + for (String arg : a) { + if (arg.length() > 1) { + for (int i = 0; i < arg.length(); i++) { + System.out.println(arg.charAt(i)); + } + } else { + System.out.println(0); + } + } + } else { + System.out.println("no"); + } + } + + void decl(String[] a) { + if (a.length == 1) { + String t = a[0]; + } + for (int i = 0; i < a.length; i++) { + String t = a[i]; + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/single_statement_block/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/single_statement_block/expected.xml new file mode 100644 index 000000000000..d049f6bf366a --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/single_statement_block/expected.xml @@ -0,0 +1,102 @@ + + + + + SingleStatement.java + 4 + Code Block Contains Single Statement + 'for' contains single statement + + + + SingleStatement.java + 8 + Code Block Contains Single Statement + 'if' contains single statement + + + + SingleStatement.java + 10 + Code Block Contains Single Statement + 'else' contains single statement + + + + SingleStatement.java + 14 + Code Block Contains Single Statement + 'if' contains single statement + + + + SingleStatement.java + 19 + Code Block Contains Single Statement + 'else' contains single statement + + + + SingleStatement.java + 23 + Code Block Contains Single Statement + 'for' contains single statement + + + + SingleStatement.java + 28 + Code Block Contains Single Statement + 'do' contains single statement + + + + SingleStatement.java + 34 + Code Block Contains Single Statement + 'while' contains single statement + + + + SingleStatement.java + 40 + Code Block Contains Single Statement + 'if' contains single statement + + + + SingleStatement.java + 41 + Code Block Contains Single Statement + 'for' contains single statement + + + + SingleStatement.java + 42 + Code Block Contains Single Statement + 'if' contains single statement + + + + SingleStatement.java + 43 + Code Block Contains Single Statement + 'for' contains single statement + + + + SingleStatement.java + 46 + Code Block Contains Single Statement + 'else' contains single statement + + + + SingleStatement.java + 50 + Code Block Contains Single Statement + 'else' contains single statement + + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/statements_without_braces/ControlFlowStatements.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/statements_without_braces/ControlFlowStatements.java index 5b6f9a1da573..821eacd76b8f 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/statements_without_braces/ControlFlowStatements.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/statements_without_braces/ControlFlowStatements.java @@ -26,4 +26,14 @@ class T { } else System.out.println(a.length); } + + void ff(String[] a) { + if (a.length != 0) + for (String arg : a) + if (arg.length() > 1) + for (int i = 0; i < arg.length(); i++) + System.out.println(arg.charAt(i)); + else System.out.println(0); + else System.out.println("no"); + } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/statements_without_braces/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/statements_without_braces/expected.xml index e5869a1c602e..9f8e60de5df5 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/statements_without_braces/expected.xml +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/statements_without_braces/expected.xml @@ -57,4 +57,46 @@ <code>else</code> without braces #loc + + ControlFlowStatements.java + 31 + Control flow statement without braces + <code>if</code> without braces #loc + + + + ControlFlowStatements.java + 32 + Control flow statement without braces + <code>for</code> without braces #loc + + + + ControlFlowStatements.java + 33 + Control flow statement without braces + <code>if</code> without braces #loc + + + + ControlFlowStatements.java + 34 + Control flow statement without braces + <code>for</code> without braces #loc + + + + ControlFlowStatements.java + 36 + Control flow statement without braces + <code>else</code> without braces #loc + + + + ControlFlowStatements.java + 37 + Control flow statement without braces + <code>else</code> without braces #loc + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/ControlFlowStatementWithoutBracesFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/ControlFlowStatementWithoutBracesFixTest.java index 2d1f76a267fc..9ff183ffd309 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/ControlFlowStatementWithoutBracesFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/ControlFlowStatementWithoutBracesFixTest.java @@ -47,6 +47,14 @@ public class ControlFlowStatementWithoutBracesFixTest extends IGQuickFixesTestCa public void testWhile() { doTest("while"); } public void testWhileOutside() { assertQuickfixNotAvailable(getMessagePrefix()); } + public void testLadderInnerElse() { doTest("else"); } + public void testLadderInnerFor() { doTest("for"); } + public void testLadderInnerIf() { doTest("if"); } + public void testLadderOuterElse() { doTest("else"); } + public void testLadderOuterFor() { doTest("for"); } + public void testLadderOuterIf() { doTest("if"); } + public void testLadderOutside() { assertQuickfixNotAvailable(getMessagePrefix()); } + @Override protected void setUp() throws Exception { super.setUp(); diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java index d7e46fc07426..c72c24ac6c50 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java @@ -50,11 +50,11 @@ public class SingleStatementInBlockFixTest extends IGQuickFixesTestCase { } private static String getMessage(String keyword) { - return InspectionGadgetsBundle.message("single.statement.in.block.descriptor", keyword); + return InspectionGadgetsBundle.message("single.statement.in.block.quickfix", keyword); } private static String getMessagePrefix() { - final String message = InspectionGadgetsBundle.message("single.statement.in.block.descriptor", "@"); + final String message = InspectionGadgetsBundle.message("single.statement.in.block.quickfix", "@"); final int index = message.indexOf("@"); if (index >= 0) return message.substring(0, index); return message; diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/SingleStatementInBlockInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/SingleStatementInBlockInspectionTest.java new file mode 100644 index 000000000000..a2f7defa9802 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/SingleStatementInBlockInspectionTest.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.siyeh.ig.style; + +import com.siyeh.ig.IGInspectionTestCase; + +/** + * @author Pavel.Dolgov + */ +public class SingleStatementInBlockInspectionTest extends IGInspectionTestCase { + public void test() { + doTest("com/siyeh/igtest/style/single_statement_block", new SingleStatementInBlockInspection()); + } +}