-
Notifications
You must be signed in to change notification settings - Fork 28
Rules/gci22 remove unnecessary method calls #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Loup-K
wants to merge
4
commits into
green-code-initiative:main
Choose a base branch
from
snail-unamur:rules/gci22-remove-unnecessary-method-calls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -559,4 +559,17 @@ void testGCI404() { | |
| checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_15MIN); | ||
| } | ||
|
|
||
| @Test | ||
| void testGCI22() { | ||
| String filePath = "src/GCI22/avoidUseOfMethodForBasicOperations.py"; | ||
| String ruleId = "creedengo-python:GCI22"; | ||
| String ruleMsg = "Avoid using methods for simple basic operations."; | ||
| int[] startLines = new int[]{9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 26, 30, 35, 36, 41, 42, 44, 45, 46, 47, 48, 51, 52, 57, 58, 59, 65, 70, 73, 76, 79, 82, 85 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use multi-line declaration like : int[] startLines = new int[]{
9, 10, 11, 12, 13,
14, 17, 18, 19, 20,
21, 22, 26, 30, 35,
...
}; |
||
| }; | ||
| int[] endLines = new int[]{9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 26, 30, 35, 36, 41, 42, 44, 45, 46, 47, 48, 51, 52, 57, 58, 59, 65, 70, 73, 76, 79, 82, 85 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use multi-line declaration like : int[] endLines = new int[]{
9, 10, 11, 12, 13,
14, 17, 18, 19, 20,
21, 22, 26, 30, 35,
...
}; |
||
| }; | ||
|
|
||
| checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_5MIN); | ||
| } | ||
|
|
||
| } | ||
160 changes: 160 additions & 0 deletions
160
...ects/creedengo-python-plugin-test-project/src/GCI22/avoidUseOfMethodForBasicOperations.py
This file contains hidden or 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,160 @@ | ||
| a = 10 | ||
| b = 3 | ||
| x = 5 | ||
| y = 8 | ||
| n1 = 0b1010 | ||
| n2 = 0b1100 | ||
|
|
||
| #Arithmetic operations: | ||
| result_add = a.__add__(b) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| result_sub = a.__sub__(b) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| result_mul = a.__mul__(b) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| result_div = a.__truediv__(b) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| result_mod = a.__mod__(b) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| result_pow = a.__pow__(b) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| #Comparisons: | ||
| is_equal = x.__eq__(y) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| is_greater = x.__gt__(y) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| is_less = x.__lt__(y) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| is_gte = x.__ge__(y) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| is_lte = x.__le__(y) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| is_not_eq = x.__ne__(y) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| #Collection size: | ||
| my_list = [1, 2, 3, 4, 5] | ||
| size = my_list.__len__() # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| #Found element: | ||
| words = ["This", "is", "a", "test"] | ||
| found = list.__contains__(words, "test") # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| #String concatenation: | ||
| first = "Hello" | ||
| last = "World" | ||
| greeting = first.__add__(", " + last) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| greeting = "".join([first, ", ", last]) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| #Operations: | ||
| flag_a = True | ||
| flag_b = False | ||
| result_and = flag_a.__and__(flag_b) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| result_or = flag_a.__or__(flag_b) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| bit_and = n1.__and__(n2) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| bit_or = n1.__or__(n2) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| bit_xor = n1.__xor__(n2) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| bit_lshift = n1.__lshift__(2) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| bit_rshift = n1.__rshift__(2) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| #Sequence repetition: | ||
| repeated_str = "abc".__mul__(3) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| repeated_list = [0].__mul__(5) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| #Access / modification / deletion of elements: | ||
| data = {"key": 42} | ||
| my_list2 = [10, 20, 30] | ||
| val = data.__getitem__("key") # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| my_list2.__setitem__(1, 99) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
| my_list2.__delitem__(0) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| #Accumulation in a loop: | ||
| numbers = range(10) | ||
| squares = [] | ||
| for n in numbers: | ||
| squares.append(n ** 2) # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| #Trivial user-defined functions wrapping a basic operation: | ||
|
|
||
| def add(a, b): | ||
| return a + b # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| def multiply(a, b): | ||
| return a * b # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| def is_eq(a, b): | ||
| return a == b # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| def is_gt(a, b): | ||
| return a > b # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| def contains(collection, item): | ||
| return item in collection # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
| def concat(a, b): | ||
| return a + b # Noncompliant {{Avoid using methods for simple basic operations.}} | ||
|
|
||
|
|
||
| # =========================================================================== | ||
|
|
||
| a = 10 | ||
| b = 3 | ||
| x = 5 | ||
| y = 8 | ||
| n1 = 0b1010 | ||
| n2 = 0b1100 | ||
|
|
||
| result_add = a + b # Compliant {{Native operator.}} | ||
| result_sub = a - b # Compliant {{Native operator.}} | ||
| result_mul = a * b # Compliant {{Native operator.}} | ||
| result_div = a / b # Compliant {{Native operator.}} | ||
| result_mod = a % b # Compliant {{Native operator.}} | ||
| result_pow = a ** b # Compliant {{Native operator.}} | ||
|
|
||
| is_equal = x == y # Compliant {{Native operator.}} | ||
| is_greater = x > y # Compliant {{Native operator.}} | ||
| is_less = x < y # Compliant {{Native operator.}} | ||
| is_gte = x >= y # Compliant {{Native operator.}} | ||
| is_lte = x <= y # Compliant {{Native operator.}} | ||
| is_not_eq = x != y # Compliant {{Native operator.}} | ||
|
|
||
| my_list = [1, 2, 3, 4, 5] | ||
| size = len(my_list) # Compliant {{use of len().}} | ||
|
|
||
| words = ["This", "is", "a", "test"] | ||
| found = "test" in words # Compliant {{use of in.}} | ||
|
|
||
| first = "Hello" | ||
| last = "World" | ||
| greeting = first + ", " + last # Compliant {{Native operator.}} | ||
| greeting = f"{first}, {last}" # Compliant {{use of f-string.}} | ||
|
|
||
| words = ["word"] * 1000 | ||
| sentence = " ".join(words) # Compliant {{use of join().}} | ||
|
|
||
| flag_a = True | ||
| flag_b = False | ||
| result_and = flag_a and flag_b # Compliant {{Native operator.}} | ||
| result_or = flag_a or flag_b # Compliant {{Native operator.}} | ||
|
|
||
| bit_and = n1 & n2 # Compliant {{Native operator.}} | ||
| bit_or = n1 | n2 # Compliant {{Native operator.}} | ||
| bit_xor = n1 ^ n2 # Compliant {{Native operator.}} | ||
| bit_lshift = n1 << 2 # Compliant {{Native operator.}} | ||
| bit_rshift = n1 >> 2 # Compliant {{Native operator.}} | ||
|
|
||
| repeated_str = "abc" * 3 # Compliant {{Native operator.}} | ||
| repeated_list = [0] * 5 # Compliant {{Native operator.}} | ||
|
|
||
| data = {"key": 42} | ||
| my_list2 = [10, 20, 30] | ||
| val = data["key"] # Compliant {{Native subscript syntax.}} | ||
| my_list2[1] = 99 # Compliant {{Native subscript syntax.}} | ||
| del my_list2[0] # Compliant {{Native operator.}} | ||
|
|
||
| numbers = range(10) | ||
| squares = [n ** 2 for n in numbers] # Compliant {{Native syntax.}} | ||
|
|
||
| values = [3, 1, 7, 2] | ||
| total = sum(values) # Compliant {Use of sum().}} | ||
| minimum = min(values) # Compliant {{Use of min().}} | ||
| maximum = max(values) # Compliant {{Use of max().}} | ||
| flags = [True, False, True] | ||
| check = all(flags) # Compliant {{Use of all().}} | ||
| check = any(flags) # Compliant {{Use of any().}} | ||
|
|
||
| def is_adult(age): | ||
| return age >= 18 # Compliant {{Not a trivial operator wrapper.}} | ||
|
|
||
| def tax_rate_applies(amount): | ||
| return amount > 1000 # Compliant {{Not a trivial operator wrapper.}} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Loup-K,
Please remove this change.