diff --git a/docs/checks.md b/docs/checks.md index 7c6c231..80b4b43 100644 --- a/docs/checks.md +++ b/docs/checks.md @@ -757,12 +757,10 @@ if "key" in d: Categories: `builtin` `readability` -The `del` statement has it's uses, but for the most part, it can be -replaced with a more flexible and expressive alternative. - -With `dict` and `list` types you can remove a key/index by using the -`.pop()` method. If you want to remove all the elements in a `dict` or -`list`, use `.clear()` instead. +The `del` statement is commonly used for popping single elements from dicts +and lists, though a slice can be used to remove a range of elements +instead. When removing all elements via a slice, use the faster and more +succinct `.clear()` method instead. Bad: @@ -770,8 +768,7 @@ Bad: names = {"key": "value"} nums = [1, 2, 3] -del names["key"] -del nums[0] +del names[:] del nums[:] ``` @@ -781,8 +778,7 @@ Good: names = {"key": "value"} nums = [1, 2, 3] -names.pop("key") -nums.pop(0) +names.clear() nums.clear() ``` ## FURB132: `use-set-discard` diff --git a/refurb/checks/builtin/no_del.py b/refurb/checks/builtin/no_del.py index 71c6c51..52c8469 100644 --- a/refurb/checks/builtin/no_del.py +++ b/refurb/checks/builtin/no_del.py @@ -8,12 +8,10 @@ @dataclass class ErrorInfo(Error): """ - The `del` statement has it's uses, but for the most part, it can be - replaced with a more flexible and expressive alternative. - - With `dict` and `list` types you can remove a key/index by using the - `.pop()` method. If you want to remove all the elements in a `dict` or - `list`, use `.clear()` instead. + The `del` statement is commonly used for popping single elements from dicts + and lists, though a slice can be used to remove a range of elements + instead. When removing all elements via a slice, use the faster and more + succinct `.clear()` method instead. Bad: @@ -21,8 +19,7 @@ class ErrorInfo(Error): names = {"key": "value"} nums = [1, 2, 3] - del names["key"] - del nums[0] + del names[:] del nums[:] ``` @@ -32,8 +29,7 @@ class ErrorInfo(Error): names = {"key": "value"} nums = [1, 2, 3] - names.pop("key") - nums.pop(0) + names.clear() nums.clear() ``` """ @@ -41,6 +37,7 @@ class ErrorInfo(Error): name = "no-del" code = 131 categories = ["builtin", "readability"] + msg: str = "Replace `del x[:]` with `x.clear()`" def check(node: DelStmt, errors: list[Error]) -> None: @@ -50,18 +47,4 @@ def check(node: DelStmt, errors: list[Error]) -> None: ) if str(ty).startswith(("builtins.dict[", "builtins.list[")): match index: case SliceExpr(begin_index=None, end_index=None): - errors.append( - ErrorInfo.from_node( - node, "Replace `del x[:]` with `x.clear()`" - ) - ) - - case SliceExpr(): - pass - - case _: - errors.append( - ErrorInfo.from_node( - node, "Replace `del x[y]` with `x.pop(y)`" - ) - ) + errors.append(ErrorInfo.from_node(node)) diff --git a/test/data/err_131.py b/test/data/err_131.py index e357f49..80bef79 100644 --- a/test/data/err_131.py +++ b/test/data/err_131.py @@ -3,13 +3,14 @@ # these should match -del names["key"] -del nums[0] del nums[:] # these should not +del names["key"] +del nums[0] + x = 1 del x diff --git a/test/data/err_131.txt b/test/data/err_131.txt index e258fc3..463f67c 100644 --- a/test/data/err_131.txt +++ b/test/data/err_131.txt @@ -1,3 +1 @@ -test/data/err_131.py:6:1 [FURB131]: Replace `del x[y]` with `x.pop(y)` -test/data/err_131.py:7:1 [FURB131]: Replace `del x[y]` with `x.pop(y)` -test/data/err_131.py:8:1 [FURB131]: Replace `del x[:]` with `x.clear()` +test/data/err_131.py:6:1 [FURB131]: Replace `del x[:]` with `x.clear()`