[PY-16194] Fix passing other calls besides append on list in list creation quick fix

GitOrigin-RevId: b699acf7cf42cdd47ff8a7b2f0ab4f404eddb152
This commit is contained in:
andrey.matveev
2020-07-07 04:37:07 +00:00
committed by intellij-monorepo-bot
parent f5f8de6460
commit eb701312fd
4 changed files with 32 additions and 17 deletions
@@ -64,26 +64,29 @@ public class PyListCreationInspection extends PyInspection {
final PyCallExpression callExpression = (PyCallExpression)statement;
final PyExpression callee = callExpression.getCallee();
if (callee instanceof PyQualifiedExpression) {
final PyExpression qualifier = ((PyQualifiedExpression)callee).getQualifier();
final String funcName = ((PyQualifiedExpression)callee).getReferencedName();
if (qualifier != null && name.equals(qualifier.getText()) && "append".equals(funcName)) {
final PyArgumentList argList = callExpression.getArgumentList();
if (argList != null) {
for (PyExpression argument : argList.getArguments()) {
if (argument.getText().equals(name)) {
if (quickFix != null)
registerProblem(node, message, quickFix);
return;
}
}
if (quickFix == null) {
quickFix = new ListCreationQuickFix(node);
}
quickFix.addStatement((PyExpressionStatement)expressionStatement);
if (!(callee instanceof PyQualifiedExpression)) break;
final PyExpression qualifier = ((PyQualifiedExpression)callee).getQualifier();
if (qualifier == null || !name.equals(qualifier.getText())) break;
final String funcName = ((PyQualifiedExpression)callee).getReferencedName();
if (!"append".equals(funcName)) break;
final PyArgumentList argList = callExpression.getArgumentList();
if (argList != null) {
for (PyExpression argument : argList.getArguments()) {
if (argument.getText().equals(name)) {
if (quickFix != null)
registerProblem(node, message, quickFix);
return;
}
}
if (quickFix == null) {
quickFix = new ListCreationQuickFix(node);
}
quickFix.addStatement((PyExpressionStatement)expressionStatement);
}
if (quickFix == null) {
return;
}
@@ -0,0 +1,4 @@
<weak_warning descr="This list creation could be rewritten as a list literal">b<caret>ar = []</weak_warning>
bar.append(1)
bar.extend([2, 3])
bar.append(4)
@@ -0,0 +1,3 @@
bar = [1]
bar.extend([2, 3])
bar.append(4)
@@ -359,6 +359,11 @@ public class PyQuickFixTest extends PyTestCase {
doInspectionTest(PyListCreationInspection.class, PyPsiBundle.message("QFIX.list.creation"), true, true);
}
// PY-16194
public void testListCreationOnlyConsecutiveAppends() {
doInspectionTest(PyListCreationInspection.class, PyPsiBundle.message("QFIX.list.creation"), true, true);
}
// PY-1445
public void testConvertSingleQuotedDocstring() {
getIndentOptions().INDENT_SIZE = 2;