Skip to content

Commit 8384e42

Browse files
SONARPY-1042: Fix the quick-fix for S3923 (AllBranchesAreIdenticalCheck) (#1153)
1 parent 6a344fe commit 8384e42

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

python-checks/src/main/java/org/sonar/python/checks/AllBranchesAreIdenticalCheck.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public void initialize(Context context) {
5454
}
5555

5656
private static void handleIfStatement(IfStatement ifStmt, SubscriptionContext ctx) {
57-
if (ifStmt.elseBranch() == null) {
57+
ElseClause elseBranch = ifStmt.elseBranch();
58+
if (elseBranch == null) {
5859
return;
5960
}
6061
StatementList body = ifStmt.body();
@@ -64,13 +65,13 @@ private static void handleIfStatement(IfStatement ifStmt, SubscriptionContext ct
6465
return;
6566
}
6667
}
67-
if (!CheckUtils.areEquivalent(body, ifStmt.elseBranch().body())) {
68+
if (!CheckUtils.areEquivalent(body, elseBranch.body())) {
6869
return;
6970
}
7071
PreciseIssue issue = ctx.addIssue(ifStmt.keyword(), "Remove this if statement or edit its code blocks so that they're not all the same.");
7172
issue.secondary(issueLocation(ifStmt.body()));
7273
ifStmt.elifBranches().forEach(e -> issue.secondary(issueLocation(e.body())));
73-
issue.secondary(issueLocation(ifStmt.elseBranch().body()));
74+
issue.secondary(issueLocation(elseBranch.body()));
7475
createQuickFix((IssueWithQuickFix) issue, ifStmt, ifStmt.body().statements());
7576
}
7677

@@ -126,8 +127,9 @@ private static void createQuickFixConditional(IssueWithQuickFix issue, Tree tree
126127
List<Tree> children = tree.children();
127128
Token lastTokenOfFirst = children.get(0).lastToken();
128129
Token lastTokenOfConditional = children.get(children.size() - 1).lastToken();
129-
PythonTextEdit edit = new PythonTextEdit("", lastTokenOfFirst.line(), lastTokenOfFirst.column(),
130-
lastTokenOfConditional.line(), lastTokenOfConditional.column());
130+
// Keep the first statement and remove the rest
131+
PythonTextEdit edit = new PythonTextEdit("", lastTokenOfFirst.line(), lastTokenOfFirst.column() + lastTokenOfFirst.value().length(),
132+
lastTokenOfConditional.line(), lastTokenOfConditional.column() + lastTokenOfConditional.value().length());
131133

132134
PythonQuickFix quickFix = PythonQuickFix.newQuickFix("Remove the if statement")
133135
.addTextEdit(edit)
@@ -168,8 +170,8 @@ private static void createQuickFix(IssueWithQuickFix issue, Tree tree, List<Stat
168170
// Remove else branch
169171
elseBranch.ifPresent(branch -> quickFixBuilder.addTextEdit(PythonTextEdit.remove(branch)));
170172

171-
// Remove the indent on the else line
172-
if (ifStatement.elifBranches().isEmpty()) {
173+
// Remove the indent on the else line if it doesn't start at column 0
174+
if (ifStatement.elifBranches().isEmpty() && elseBranch.map(b -> b.firstToken().column()).orElse(0) != 0) {
173175
lineElseBranch.ifPresent(lineElse -> quickFixBuilder.addTextEdit(editIndentAtLine(lineElse)));
174176
}
175177

python-checks/src/test/java/org/sonar/python/checks/AllBranchesAreIdenticalCheckTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
public class AllBranchesAreIdenticalCheckTest {
2828

29-
private PythonCheck check = new AllBranchesAreIdenticalCheck();
29+
private final PythonCheck check = new AllBranchesAreIdenticalCheck();
3030
@Test
3131
public void test() {
3232
PythonCheckVerifier.verify("src/test/resources/checks/allBranchesAreIdentical.py", check);
@@ -113,6 +113,10 @@ public void multiple_conditional_statements(){
113113
String codeWithIssue = "a = 1 if x else 1 if y else 1 if z else 1";
114114
String codeFixed = "a = 1";
115115
PythonQuickFixVerifier.verify(check, codeWithIssue, codeFixed);
116+
117+
codeWithIssue = "a = (1 if x else 1) if cond else 1";
118+
codeFixed = "a = (1 if x else 1)"; // SONARPY-1047
119+
PythonQuickFixVerifier.verify(check, codeWithIssue, codeFixed);
116120
}
117121

118122
@Test
@@ -175,4 +179,32 @@ public void test_complex_condition(){
175179
String codeFixed = "a = do_something(a, b, c, do_something_else(d))";
176180
PythonQuickFixVerifier.verify(check, codeWithIssue, codeFixed);
177181
}
182+
183+
@Test
184+
public void test_remove(){
185+
String codeWithIssue = ""+
186+
"if b == 0:\n" +
187+
" doSomething()\n" +
188+
"else:\n" +
189+
" doSomething()\n" +
190+
"\n" + // blank line should not be removed: SONARPY-1048
191+
"a = 1";
192+
String codeFixed = ""+
193+
"doSomething()\n" +
194+
"a = 1";
195+
PythonQuickFixVerifier.verify(check, codeWithIssue, codeFixed);
196+
197+
codeWithIssue = ""+
198+
"if b == 0:\n" +
199+
" doSomething()\n" +
200+
"else:\n" +
201+
" doSomething()\n" +
202+
"\n" +
203+
"\n" +
204+
"a = 1";
205+
codeFixed = ""+
206+
"doSomething()\n" +
207+
"a = 1";
208+
PythonQuickFixVerifier.verify(check, codeWithIssue, codeFixed);
209+
}
178210
}

0 commit comments

Comments
 (0)