summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryushao2 <36848472+yushao2@users.noreply.github.com>2021-07-01 00:45:16 +0800
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-07-01 10:13:58 +0200
commit62ce45080bfd8212700184006af67112775e2570 (patch)
tree6ee13b40db29010326d907eba9c3e8e34d63c877
parent86970f027c1b391556632dc99865ad7df9350a28 (diff)
downloadpylint-git-62ce45080bfd8212700184006af67112775e2570.tar.gz
Apply suggestions from code review
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
-rw-r--r--pylint/checkers/refactoring/recommendation_checker.py3
-rw-r--r--pylint/checkers/refactoring/refactoring_checker.py10
-rw-r--r--tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py1
-rw-r--r--tests/functional/u/unnecessary/unnecessary_dict_index_lookup.txt21
4 files changed, 14 insertions, 21 deletions
diff --git a/pylint/checkers/refactoring/recommendation_checker.py b/pylint/checkers/refactoring/recommendation_checker.py
index d7d48e031..795015b71 100644
--- a/pylint/checkers/refactoring/recommendation_checker.py
+++ b/pylint/checkers/refactoring/recommendation_checker.py
@@ -233,7 +233,8 @@ class RecommendationChecker(checkers.BaseChecker):
and subscript == subscript.parent.target
):
# Ignore this subscript if it is the target of an assignment
- return # Early termination as dict index lookup is necessary
+ # Early termination as dict index lookup is necessary
+ return
self.add_message("consider-using-dict-items", node=node)
return
diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py
index c17545ded..df8f4f271 100644
--- a/pylint/checkers/refactoring/refactoring_checker.py
+++ b/pylint/checkers/refactoring/refactoring_checker.py
@@ -1689,14 +1689,8 @@ class RefactoringChecker(checkers.BaseTokenChecker):
and subscript == subscript.parent.target
):
# Ignore this subscript if it is the target of an assignment
- if subscript.as_string() == subscript.parent.value.as_string():
- # Fire error as d[k] += d[k] has an unnecessary index lookup
- self.add_message(
- "unnecessary-dict-index-lookup",
- node=subscript,
- args=(node.target.elts[1].as_string()),
- )
- return # Early termination; after reassignment dict index lookup will be necessary
+ # Early termination; after reassignment dict index lookup will be necessary
+ return
# Case where .items is assigned to k,v (i.e., for k, v in d.items())
if isinstance(value, astroid.Name):
diff --git a/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py b/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py
index 1e56b046b..68e254c16 100644
--- a/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py
+++ b/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py
@@ -34,7 +34,6 @@ class Foo:
for k, v in Foo.c_dict.items():
print(b_dict[k]) # Should not emit warning, accessing other dictionary
print(Foo.c_dict[k]) # [unnecessary-dict-index-lookup]
- Foo.c_dict[k] += Foo.c_dict[k] # [unnecessary-dict-index-lookup]
Foo.c_dict[k] += v # key access necessary
# Tests on comprehensions
diff --git a/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.txt b/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.txt
index c0d5d6d16..e780f669f 100644
--- a/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.txt
+++ b/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.txt
@@ -8,16 +8,15 @@ unnecessary-dict-index-lookup:26:1::Unnecessary dictionary index lookup, use 'v'
unnecessary-dict-index-lookup:27:1::Unnecessary dictionary index lookup, use 'v' instead:HIGH
unnecessary-dict-index-lookup:27:41::Unnecessary dictionary index lookup, use 'v' instead:HIGH
unnecessary-dict-index-lookup:36:10::Unnecessary dictionary index lookup, use 'v' instead:HIGH
-unnecessary-dict-index-lookup:37:4::Unnecessary dictionary index lookup, use 'v' instead:HIGH
-unnecessary-dict-index-lookup:41:40::Unnecessary dictionary index lookup, use 'v' instead:HIGH
+unnecessary-dict-index-lookup:40:40::Unnecessary dictionary index lookup, use 'v' instead:HIGH
+unnecessary-dict-index-lookup:42:1::Unnecessary dictionary index lookup, use 'v' instead:HIGH
unnecessary-dict-index-lookup:43:1::Unnecessary dictionary index lookup, use 'v' instead:HIGH
-unnecessary-dict-index-lookup:44:1::Unnecessary dictionary index lookup, use 'v' instead:HIGH
-unnecessary-dict-index-lookup:44:52::Unnecessary dictionary index lookup, use 'v' instead:HIGH
-unnecessary-dict-index-lookup:46:37::Unnecessary dictionary index lookup, use 'v' instead:HIGH
+unnecessary-dict-index-lookup:43:52::Unnecessary dictionary index lookup, use 'v' instead:HIGH
+unnecessary-dict-index-lookup:45:37::Unnecessary dictionary index lookup, use 'v' instead:HIGH
+unnecessary-dict-index-lookup:47:1::Unnecessary dictionary index lookup, use 'v' instead:HIGH
unnecessary-dict-index-lookup:48:1::Unnecessary dictionary index lookup, use 'v' instead:HIGH
-unnecessary-dict-index-lookup:49:1::Unnecessary dictionary index lookup, use 'v' instead:HIGH
-unnecessary-dict-index-lookup:49:49::Unnecessary dictionary index lookup, use 'v' instead:HIGH
-unnecessary-dict-index-lookup:55:10::Unnecessary dictionary index lookup, use 'item[1]' instead:HIGH
-unnecessary-dict-index-lookup:58:1::Unnecessary dictionary index lookup, use 'item[1]' instead:HIGH
-unnecessary-dict-index-lookup:63:10::Unnecessary dictionary index lookup, use 'item[1]' instead:HIGH
-unnecessary-dict-index-lookup:80:14::Unnecessary dictionary index lookup, use '_' instead:HIGH
+unnecessary-dict-index-lookup:48:49::Unnecessary dictionary index lookup, use 'v' instead:HIGH
+unnecessary-dict-index-lookup:54:10::Unnecessary dictionary index lookup, use 'item[1]' instead:HIGH
+unnecessary-dict-index-lookup:57:1::Unnecessary dictionary index lookup, use 'item[1]' instead:HIGH
+unnecessary-dict-index-lookup:62:10::Unnecessary dictionary index lookup, use 'item[1]' instead:HIGH
+unnecessary-dict-index-lookup:79:14::Unnecessary dictionary index lookup, use '_' instead:HIGH