@@ -54,7 +54,8 @@ public void initialize(Context context) {
54
54
}
55
55
56
56
private static void handleIfStatement (IfStatement ifStmt , SubscriptionContext ctx ) {
57
- if (ifStmt .elseBranch () == null ) {
57
+ ElseClause elseBranch = ifStmt .elseBranch ();
58
+ if (elseBranch == null ) {
58
59
return ;
59
60
}
60
61
StatementList body = ifStmt .body ();
@@ -64,13 +65,13 @@ private static void handleIfStatement(IfStatement ifStmt, SubscriptionContext ct
64
65
return ;
65
66
}
66
67
}
67
- if (!CheckUtils .areEquivalent (body , ifStmt . elseBranch () .body ())) {
68
+ if (!CheckUtils .areEquivalent (body , elseBranch .body ())) {
68
69
return ;
69
70
}
70
71
PreciseIssue issue = ctx .addIssue (ifStmt .keyword (), "Remove this if statement or edit its code blocks so that they're not all the same." );
71
72
issue .secondary (issueLocation (ifStmt .body ()));
72
73
ifStmt .elifBranches ().forEach (e -> issue .secondary (issueLocation (e .body ())));
73
- issue .secondary (issueLocation (ifStmt . elseBranch () .body ()));
74
+ issue .secondary (issueLocation (elseBranch .body ()));
74
75
createQuickFix ((IssueWithQuickFix ) issue , ifStmt , ifStmt .body ().statements ());
75
76
}
76
77
@@ -126,8 +127,9 @@ private static void createQuickFixConditional(IssueWithQuickFix issue, Tree tree
126
127
List <Tree > children = tree .children ();
127
128
Token lastTokenOfFirst = children .get (0 ).lastToken ();
128
129
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 ());
131
133
132
134
PythonQuickFix quickFix = PythonQuickFix .newQuickFix ("Remove the if statement" )
133
135
.addTextEdit (edit )
@@ -168,8 +170,8 @@ private static void createQuickFix(IssueWithQuickFix issue, Tree tree, List<Stat
168
170
// Remove else branch
169
171
elseBranch .ifPresent (branch -> quickFixBuilder .addTextEdit (PythonTextEdit .remove (branch )));
170
172
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 ) {
173
175
lineElseBranch .ifPresent (lineElse -> quickFixBuilder .addTextEdit (editIndentAtLine (lineElse )));
174
176
}
175
177
0 commit comments