Skip to content

Commit

Permalink
Python doesn't need braces
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Sep 9, 2024
1 parent 88abd41 commit abb0338
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
16 changes: 11 additions & 5 deletions src/main/java/org/openrewrite/staticanalysis/NeedBraces.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}

private static class NeedBracesVisitor extends JavaIsoVisitor<ExecutionContext> {

@SuppressWarnings("NotNullFieldNotInitialized")
NeedBracesStyle needBracesStyle;

/**
Expand Down Expand Up @@ -92,10 +94,16 @@ private <T extends Statement> J.Block buildBlock(Statement owner, T element) {
}

@Override
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
SourceFile cu = (SourceFile) requireNonNull(tree);
needBracesStyle = cu.getStyle(NeedBracesStyle.class) == null ? Checkstyle.needBracesStyle() : cu.getStyle(NeedBracesStyle.class);
// Python don't need none of your curly braces
if (cu.getSourcePath().toString().endsWith(".py")) {
return (J) tree;
}
needBracesStyle = cu.getStyle(NeedBracesStyle.class) == null ?
Checkstyle.needBracesStyle() :
cu.getStyle(NeedBracesStyle.class, new NeedBracesStyle(false, false));
}
return super.visit(tree, ctx);
}
Expand Down Expand Up @@ -201,6 +209,4 @@ public J.ForLoop visitForLoop(J.ForLoop forLoop, ExecutionContext ctx) {
return elem;
}
}

;
}
48 changes: 24 additions & 24 deletions src/test/java/org/openrewrite/staticanalysis/NeedBracesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"UnusedAssignment",
"ConstantConditions",
"ClassInitializerMayBeStatic",
"UnnecessaryReturnStatement"
})
"UnnecessaryReturnStatement",
"DuplicateCondition"})
class NeedBracesTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
Expand All @@ -68,31 +68,31 @@ class Test {
static void addToWhile() {
while (true) ;
}
static void addToWhileWithBody() {
while (true) return;
}
static void addToIf(int n) {
if (n == 1) return;
// foo
}
static void addToIfElse(int n) {
if (n == 1) return;
else return;
}
static void addToIfElseIfElse(int n) {
if (n == 1) return;
else if (n == 2) return;
else return;
}
static void addToDoWhile(Object obj) {
do obj.notify(); while (true);
}
static void addToIterativeFor(Object obj) {
for (int i = 0; ; ) obj.notify();
}
Expand All @@ -104,28 +104,28 @@ static void addToWhile() {
while (true) {
}
}
static void addToWhileWithBody() {
while (true) {
return;
}
}
static void addToIf(int n) {
if (n == 1) {
return;
}
// foo
}
static void addToIfElse(int n) {
if (n == 1) {
return;
} else {
return;
}
}
static void addToIfElseIfElse(int n) {
if (n == 1) {
return;
Expand All @@ -135,13 +135,13 @@ static void addToIfElseIfElse(int n) {
return;
}
}
static void addToDoWhile(Object obj) {
do {
obj.notify();
} while (true);
}
static void addToIterativeFor(Object obj) {
for (int i = 0; ; ) {
obj.notify();
Expand All @@ -164,7 +164,7 @@ class Test {
static void emptyWhile() {
while (true) ;
}
static void emptyForIterative() {
for (int i = 0; i < 10; i++) ;
}
Expand All @@ -185,26 +185,26 @@ class Test {
static void allowIf(int n) {
if (n == 1) return;
}
static void allowIfElse(int n) {
if (n == 1) return;
else return;
}
static void allowIfElseIfElse(int n) {
if (n == 1) return;
else if (n == 2) return;
else return;
}
static void allowWhileWithBody() {
while (true) return;
}
static void allowDoWhileWithBody(Object obj) {
do obj.notify(); while (true);
}
static void allowForIterativeWithBody(Object obj) {
for (int i = 0; ; ) obj.notify();
}
Expand All @@ -225,11 +225,11 @@ class Test {
static void doNotAllowWhileWithEmptyBody() {
while (true) ;
}
static void doNotAllowDoWhileWithEmptyBody(Object obj) {
do ; while (true);
}
static void doNotAllowForIterativeWithEmptyBody(Object obj) {
for (int i = 0; ; ) ;
}
Expand All @@ -241,12 +241,12 @@ static void doNotAllowWhileWithEmptyBody() {
while (true) {
}
}
static void doNotAllowDoWhileWithEmptyBody(Object obj) {
do {
} while (true);
}
static void doNotAllowForIterativeWithEmptyBody(Object obj) {
for (int i = 0; ; ) {
}
Expand Down

0 comments on commit abb0338

Please sign in to comment.