mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Distinguish same number and string keys by quoting strings (PY-30423)
This commit is contained in:
@@ -20,6 +20,7 @@ import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiElementVisitor;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
@@ -115,7 +116,7 @@ public class PyDictDuplicateKeysInspection extends PyInspection {
|
||||
@Nullable
|
||||
private String getKeyValue(@NotNull PsiElement node) {
|
||||
if (node instanceof PyStringLiteralExpression) {
|
||||
return ((PyStringLiteralExpression)node).getStringValue();
|
||||
return wrapStringKey(((PyStringLiteralExpression)node).getStringValue());
|
||||
}
|
||||
|
||||
if (node instanceof PyNumericLiteralExpression) {
|
||||
@@ -139,7 +140,7 @@ public class PyDictDuplicateKeysInspection extends PyInspection {
|
||||
|
||||
if (keys.size() > 1) {
|
||||
for (PsiElement key : keys) {
|
||||
registerProblem(key, "Dictionary contains duplicate keys '" + keyValue + "'", quickFixes);
|
||||
registerProblem(key, "Dictionary contains duplicate keys '" + unwrapStringKey(keyValue) + "'", quickFixes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,7 +163,7 @@ public class PyDictDuplicateKeysInspection extends PyInspection {
|
||||
final ASTNode node = ((PyKeywordArgument)argument).getKeywordNode();
|
||||
final String keyValue = ((PyKeywordArgument)argument).getKeyword();
|
||||
if (node != null && keyValue != null) {
|
||||
return Pair.createNonNull(node.getPsi(), keyValue);
|
||||
return Pair.createNonNull(node.getPsi(), wrapStringKey(keyValue));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,5 +174,15 @@ public class PyDictDuplicateKeysInspection extends PyInspection {
|
||||
final PyExpression callee = expression.getCallee();
|
||||
return callee != null && "dict".equals(callee.getText());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String wrapStringKey(@NotNull String key) {
|
||||
return "'" + key + "'";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String unwrapStringKey(@NotNull String key) {
|
||||
return StringUtil.unquoteString(key, '\'');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,4 +39,10 @@ d = {<warning descr="Dictionary contains duplicate keys '0'">0j</warning>: 1, <w
|
||||
<warning descr="Dictionary contains duplicate keys 'a'">'a'</warning>: 1,
|
||||
<warning descr="Dictionary contains duplicate keys 'a'">'a'</warning>: 2,
|
||||
<warning descr="Dictionary contains duplicate keys 'a'">'a'</warning>: 3,
|
||||
}
|
||||
|
||||
# PY-30423
|
||||
a = {
|
||||
1: 2,
|
||||
'1': 2,
|
||||
}
|
||||
Reference in New Issue
Block a user