Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/org/openrewrite/staticanalysis/NeedBraces.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,21 @@ public J.ForLoop visitForLoop(J.ForLoop forLoop, ExecutionContext ctx) {
}
return elem;
}

@Override
public J.ForEachLoop visitForEachLoop(J.ForEachLoop forEachLoop, ExecutionContext ctx) {
J.ForEachLoop elem = super.visitForEachLoop(forEachLoop, ctx);
boolean hasAllowableBodyType = needBracesStyle.getAllowEmptyLoopBody() ?
elem.getBody() instanceof J.Block || elem.getBody() instanceof J.Empty :
elem.getBody() instanceof J.Block;
if (!needBracesStyle.getAllowEmptyLoopBody() && elem.getBody() instanceof J.Empty) {
J.Block b = buildBlock(elem.getBody());
elem = maybeAutoFormat(elem, elem.withBody(b), ctx);
} else if (!needBracesStyle.getAllowSingleLineStatement() && !hasAllowableBodyType) {
J.Block b = buildBlock(elem.getBody());
elem = maybeAutoFormat(elem, elem.withBody(b), ctx);
}
return elem;
}
}
}
82 changes: 82 additions & 0 deletions src/test/java/org/openrewrite/staticanalysis/NeedBracesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ static void addToDoWhile(Object obj) {
static void addToIterativeFor(Object obj) {
for (int i = 0; ; ) obj.notify();
}

static void addToForEach(int[] arr) {
for (int i : arr) System.out.println(i);
}
}
""",
"""
Expand Down Expand Up @@ -139,6 +143,12 @@ static void addToIterativeFor(Object obj) {
obj.notify();
}
}

static void addToForEach(int[] arr) {
for (int i : arr) {
System.out.println(i);
}
}
}
"""
)
Expand Down Expand Up @@ -169,6 +179,10 @@ static void emptyWhile() {
static void emptyForIterative() {
for (int i = 0; i < 10; i++) ;
}

static void emptyForEach(int[] arr) {
for (int i : arr) ;
}
}
"""
)
Expand Down Expand Up @@ -209,6 +223,10 @@ static void allowDoWhileWithBody(Object obj) {
static void allowForIterativeWithBody(Object obj) {
for (int i = 0; ; ) obj.notify();
}

static void allowForEachWithBody(int[] arr) {
for (int i : arr) System.out.println(i);
}
}
"""
)
Expand All @@ -234,6 +252,10 @@ static void doNotAllowDoWhileWithEmptyBody(Object obj) {
static void doNotAllowForIterativeWithEmptyBody(Object obj) {
for (int i = 0; ; ) ;
}

static void doNotAllowForEachWithEmptyBody(int[] arr) {
for (int i : arr) ;
}
}
""",
"""
Expand All @@ -252,6 +274,11 @@ static void doNotAllowForIterativeWithEmptyBody(Object obj) {
for (int i = 0; ; ) {
}
}

static void doNotAllowForEachWithEmptyBody(int[] arr) {
for (int i : arr) {
}
}
}
"""
)
Expand Down Expand Up @@ -320,6 +347,10 @@ static void commentIterative(Object obj) {
for (int i = 0; ; ); // iterative comment
for (int i = 0; ; ) obj.notify(); // iterative with body comment
}
static void commentForEach(int[] arr) {
for (int i : arr); // foreach comment
for (int i : arr) System.out.println(i); // foreach with body comment
}
}
""",
"""
Expand Down Expand Up @@ -350,6 +381,57 @@ static void commentIterative(Object obj) {
obj.notify(); // iterative with body comment
}
}
static void commentForEach(int[] arr) {
for (int i : arr) { // foreach comment
}
for (int i : arr) {
System.out.println(i); // foreach with body comment
}
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/748")
@Test
void forEachLoop() {
rewriteRun(
//language=java
java(
"""
import java.util.Arrays;
import java.util.List;

class Foo {
void foo() {
List<String> strings = Arrays.asList("a", "b", "c");

for (int i = 0; i < strings.size(); i++)
System.out.println(strings.get(i));

for (String s : strings)
System.out.println(s);
}
}
""",
"""
import java.util.Arrays;
import java.util.List;

class Foo {
void foo() {
List<String> strings = Arrays.asList("a", "b", "c");

for (int i = 0; i < strings.size(); i++) {
System.out.println(strings.get(i));
}

for (String s : strings) {
System.out.println(s);
}
}
}
"""
)
Expand Down