Skip to content

Commit abb0338

Browse files
committed
Python doesn't need braces
1 parent 88abd41 commit abb0338

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

src/main/java/org/openrewrite/staticanalysis/NeedBraces.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6060
}
6161

6262
private static class NeedBracesVisitor extends JavaIsoVisitor<ExecutionContext> {
63+
64+
@SuppressWarnings("NotNullFieldNotInitialized")
6365
NeedBracesStyle needBracesStyle;
6466

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

9496
@Override
95-
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
96-
if (tree instanceof JavaSourceFile) {
97+
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
98+
if (tree instanceof SourceFile) {
9799
SourceFile cu = (SourceFile) requireNonNull(tree);
98-
needBracesStyle = cu.getStyle(NeedBracesStyle.class) == null ? Checkstyle.needBracesStyle() : cu.getStyle(NeedBracesStyle.class);
100+
// Python don't need none of your curly braces
101+
if (cu.getSourcePath().toString().endsWith(".py")) {
102+
return (J) tree;
103+
}
104+
needBracesStyle = cu.getStyle(NeedBracesStyle.class) == null ?
105+
Checkstyle.needBracesStyle() :
106+
cu.getStyle(NeedBracesStyle.class, new NeedBracesStyle(false, false));
99107
}
100108
return super.visit(tree, ctx);
101109
}
@@ -201,6 +209,4 @@ public J.ForLoop visitForLoop(J.ForLoop forLoop, ExecutionContext ctx) {
201209
return elem;
202210
}
203211
}
204-
205-
;
206212
}

src/test/java/org/openrewrite/staticanalysis/NeedBracesTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
"UnusedAssignment",
4141
"ConstantConditions",
4242
"ClassInitializerMayBeStatic",
43-
"UnnecessaryReturnStatement"
44-
})
43+
"UnnecessaryReturnStatement",
44+
"DuplicateCondition"})
4545
class NeedBracesTest implements RewriteTest {
4646
@Override
4747
public void defaults(RecipeSpec spec) {
@@ -68,31 +68,31 @@ class Test {
6868
static void addToWhile() {
6969
while (true) ;
7070
}
71-
71+
7272
static void addToWhileWithBody() {
7373
while (true) return;
7474
}
75-
75+
7676
static void addToIf(int n) {
7777
if (n == 1) return;
7878
// foo
7979
}
80-
80+
8181
static void addToIfElse(int n) {
8282
if (n == 1) return;
8383
else return;
8484
}
85-
85+
8686
static void addToIfElseIfElse(int n) {
8787
if (n == 1) return;
8888
else if (n == 2) return;
8989
else return;
9090
}
91-
91+
9292
static void addToDoWhile(Object obj) {
9393
do obj.notify(); while (true);
9494
}
95-
95+
9696
static void addToIterativeFor(Object obj) {
9797
for (int i = 0; ; ) obj.notify();
9898
}
@@ -104,28 +104,28 @@ static void addToWhile() {
104104
while (true) {
105105
}
106106
}
107-
107+
108108
static void addToWhileWithBody() {
109109
while (true) {
110110
return;
111111
}
112112
}
113-
113+
114114
static void addToIf(int n) {
115115
if (n == 1) {
116116
return;
117117
}
118118
// foo
119119
}
120-
120+
121121
static void addToIfElse(int n) {
122122
if (n == 1) {
123123
return;
124124
} else {
125125
return;
126126
}
127127
}
128-
128+
129129
static void addToIfElseIfElse(int n) {
130130
if (n == 1) {
131131
return;
@@ -135,13 +135,13 @@ static void addToIfElseIfElse(int n) {
135135
return;
136136
}
137137
}
138-
138+
139139
static void addToDoWhile(Object obj) {
140140
do {
141141
obj.notify();
142142
} while (true);
143143
}
144-
144+
145145
static void addToIterativeFor(Object obj) {
146146
for (int i = 0; ; ) {
147147
obj.notify();
@@ -164,7 +164,7 @@ class Test {
164164
static void emptyWhile() {
165165
while (true) ;
166166
}
167-
167+
168168
static void emptyForIterative() {
169169
for (int i = 0; i < 10; i++) ;
170170
}
@@ -185,26 +185,26 @@ class Test {
185185
static void allowIf(int n) {
186186
if (n == 1) return;
187187
}
188-
188+
189189
static void allowIfElse(int n) {
190190
if (n == 1) return;
191191
else return;
192192
}
193-
193+
194194
static void allowIfElseIfElse(int n) {
195195
if (n == 1) return;
196196
else if (n == 2) return;
197197
else return;
198198
}
199-
199+
200200
static void allowWhileWithBody() {
201201
while (true) return;
202202
}
203-
203+
204204
static void allowDoWhileWithBody(Object obj) {
205205
do obj.notify(); while (true);
206206
}
207-
207+
208208
static void allowForIterativeWithBody(Object obj) {
209209
for (int i = 0; ; ) obj.notify();
210210
}
@@ -225,11 +225,11 @@ class Test {
225225
static void doNotAllowWhileWithEmptyBody() {
226226
while (true) ;
227227
}
228-
228+
229229
static void doNotAllowDoWhileWithEmptyBody(Object obj) {
230230
do ; while (true);
231231
}
232-
232+
233233
static void doNotAllowForIterativeWithEmptyBody(Object obj) {
234234
for (int i = 0; ; ) ;
235235
}
@@ -241,12 +241,12 @@ static void doNotAllowWhileWithEmptyBody() {
241241
while (true) {
242242
}
243243
}
244-
244+
245245
static void doNotAllowDoWhileWithEmptyBody(Object obj) {
246246
do {
247247
} while (true);
248248
}
249-
249+
250250
static void doNotAllowForIterativeWithEmptyBody(Object obj) {
251251
for (int i = 0; ; ) {
252252
}

0 commit comments

Comments
 (0)