Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented new check consider-using-str-partition
- Loading branch information
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
tests/functional/c/consider/consider_using_str_partition.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""Emit a message for iteration through dict keys and subscripting dict with key.""" | ||
# pylint: disable=line-too-long,missing-docstring,unsubscriptable-object,too-few-public-methods,invalid-name,redefined-builtin | ||
from collections.abc import Iterable | ||
from typing import Any | ||
|
||
SEQ = '1,2,3' | ||
get_first = SEQ.split(',')[0] # [consider-using-str-partition] | ||
get_last = SEQ.split(',')[-1] # [consider-using-str-partition] | ||
get_first = SEQ.rsplit(',')[0] # [consider-using-str-partition] | ||
get_last = SEQ.rsplit(',')[-1] # [consider-using-str-partition] | ||
|
||
get_mid = SEQ.split(',')[1] # This is okay | ||
get_mid = SEQ.split(',')[-2] # This is okay | ||
|
||
# Test with storing splits as an object | ||
split1 = SEQ.split(',') | ||
get_first = split1[0] # [consider-using-str-partition] | ||
get_last = split1[-1] # [consider-using-str-partition] | ||
get_last = split1[len(split1)-1] # [consider-using-str-partition] | ||
|
||
split2 = SEQ.rsplit(',') | ||
get_first = split2[0] # [consider-using-str-partition] | ||
get_last = split2[-1] # [consider-using-str-partition] | ||
get_last = split2[len(split2)-1] # [consider-using-str-partition] | ||
|
||
print(split1[0], split1[-1]) # [consider-using-str-partition, consider-using-str-partition] | ||
|
||
# Test when running len on another iterable | ||
some_list = [] | ||
get_last = split1[len(split2)-1] # Should not throw an error | ||
|
||
# Tests on class attributes | ||
class Foo(): | ||
class_str = '1,2,3' | ||
|
||
def __init__(self): | ||
self.my_str = '1,2,3' | ||
|
||
def get_string(self) -> str: | ||
return self.my_str | ||
|
||
# Class attributes | ||
get_first = Foo.class_str.split(',')[0] # [consider-using-str-partition] | ||
get_last = Foo.class_str.split(',')[-1] # [consider-using-str-partition] | ||
get_first = Foo.class_str.rsplit(',')[0] # [consider-using-str-partition] | ||
get_last = Foo.class_str.rsplit(',')[-1] # [consider-using-str-partition] | ||
|
||
get_mid = Foo.class_str.split(',')[1] | ||
get_mid = Foo.class_str.split(',')[-2] | ||
|
||
split2 = Foo.class_str.split(',') | ||
get_first = split2[0] # [consider-using-str-partition] | ||
get_last = split2[-1] # [consider-using-str-partition] | ||
get_last = split2[len(split2)-1] # [consider-using-str-partition] | ||
|
||
# Test with accessors | ||
bar = Foo() | ||
get_first = bar.get_string().split(',')[0] # [consider-using-str-partition] | ||
get_last = bar.get_string().split(',')[-1] # [consider-using-str-partition] | ||
|
||
get_mid = bar.get_string().split(',')[1] | ||
get_mid = bar.get_string().split(',')[-2] | ||
|
||
# Test with user-defined len function | ||
def len(x: Iterable[Any]) -> str: | ||
return f"Hello, world! {x[2]}" | ||
|
||
get_last = split2[len(split2)-1] # This won't throw the warning as the len function has been redefined |
21 changes: 21 additions & 0 deletions
21
tests/functional/c/consider/consider_using_str_partition.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
consider-using-str-partition:7:12::Consider using str.partition() | ||
consider-using-str-partition:8:11::Consider using str.partition() | ||
consider-using-str-partition:9:12::Consider using str.partition() | ||
consider-using-str-partition:10:11::Consider using str.partition() | ||
consider-using-str-partition:17:12::Consider using str.partition() | ||
consider-using-str-partition:18:11::Consider using str.partition() | ||
consider-using-str-partition:19:11::Consider using str.partition() | ||
consider-using-str-partition:22:12::Consider using str.partition() | ||
consider-using-str-partition:23:11::Consider using str.partition() | ||
consider-using-str-partition:24:11::Consider using str.partition() | ||
consider-using-str-partition:26:6::Consider using str.partition() | ||
consider-using-str-partition:26:17::Consider using str.partition() | ||
consider-using-str-partition:43:12::Consider using str.partition() | ||
consider-using-str-partition:44:11::Consider using str.partition() | ||
consider-using-str-partition:45:12::Consider using str.partition() | ||
consider-using-str-partition:46:11::Consider using str.partition() | ||
consider-using-str-partition:52:12::Consider using str.partition() | ||
consider-using-str-partition:53:11::Consider using str.partition() | ||
consider-using-str-partition:54:11::Consider using str.partition() | ||
consider-using-str-partition:58:12::Consider using str.partition() | ||
consider-using-str-partition:59:11::Consider using str.partition() |