|
30 | 30 | import org.sonar.plugins.python.api.PythonSubscriptionCheck;
|
31 | 31 | import org.sonar.plugins.python.api.SubscriptionContext;
|
32 | 32 | import org.sonar.plugins.python.api.quickfix.PythonQuickFix;
|
| 33 | +import org.sonar.plugins.python.api.quickfix.PythonTextEdit; |
33 | 34 | import org.sonar.plugins.python.api.symbols.Symbol;
|
34 | 35 | import org.sonar.plugins.python.api.symbols.Usage;
|
35 | 36 | import org.sonar.plugins.python.api.tree.AnnotatedAssignment;
|
| 37 | +import org.sonar.plugins.python.api.tree.AssignmentExpression; |
36 | 38 | import org.sonar.plugins.python.api.tree.AssignmentStatement;
|
37 | 39 | import org.sonar.plugins.python.api.tree.ComprehensionExpression;
|
38 | 40 | import org.sonar.plugins.python.api.tree.DictCompExpression;
|
@@ -161,6 +163,23 @@ private static void createAssignmentQuickFix(Usage usage, PreciseIssue issue) {
|
161 | 163 | TextEditUtils.removeUntil(usage.tree(), assignedValue.firstToken()));
|
162 | 164 | issue.addQuickFix(quickFix);
|
163 | 165 | });
|
| 166 | + |
| 167 | + Tree assignmentTree = TreeUtils.firstAncestorOfKind(usage.tree(), Kind.ASSIGNMENT_EXPRESSION); |
| 168 | + Optional.ofNullable(assignmentTree).map(AssignmentExpression.class::cast).ifPresent(assignmentExpr -> { |
| 169 | + PythonQuickFix quickFix = PythonQuickFix.newQuickFix(ASSIGNMENT_QUICK_FIX_MESSAGE, |
| 170 | + createAssignmentExpressionQuickFix(usage, assignmentExpr)); |
| 171 | + issue.addQuickFix(quickFix); |
| 172 | + }); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static PythonTextEdit createAssignmentExpressionQuickFix(final Usage usage, final AssignmentExpression assignmentExpression) { |
| 177 | + var expression = assignmentExpression.expression(); |
| 178 | + var parent = assignmentExpression.parent(); |
| 179 | + if (parent.is(Kind.PARENTHESIZED) && expression instanceof Name nameExpr) { |
| 180 | + return TextEditUtils.replace(parent, nameExpr.name()); |
| 181 | + } else { |
| 182 | + return TextEditUtils.removeUntil(usage.tree(), expression.firstToken()); |
164 | 183 | }
|
165 | 184 | }
|
166 | 185 |
|
|
0 commit comments