Skip to content

GCI106 AvoidSqrtInLoop #Python #DLG #Build #79

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- add rule GCI106 Avoid SQRT in a loop

### Changed

- compatibility updates for SonarQube 25.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRe
AvoidFullSQLRequest.class,
AvoidListComprehensionInIterations.class,
DetectUnoptimizedImageFormat.class,
AvoidMultipleIfElseStatementCheck.class
AvoidMultipleIfElseStatementCheck.class,
AvoidSqrtInLoop.class
);

public static final String LANGUAGE = "py";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.python.checks;

import org.sonar.check.Rule;
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
import org.sonar.plugins.python.api.SubscriptionContext;
import org.sonar.plugins.python.api.tree.CallExpression;
import org.sonar.plugins.python.api.tree.Expression;
import org.sonar.plugins.python.api.tree.QualifiedExpression;
import org.sonar.plugins.python.api.tree.Tree;

@Rule(key = "GCI106")
public class AvoidSqrtInLoop extends PythonSubscriptionCheck {

public static final String DESCRIPTION = "Avoid using scalar sqrt functions in loops. Apply vectorized sqrt operations on arrays directly.";

@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, this::checkCallExpression);
}

private void checkCallExpression(SubscriptionContext context) {
CallExpression callExpression = (CallExpression) context.syntaxNode();

if (isSqrtCall(callExpression) && hasLoopParent(callExpression)) {
context.addIssue(callExpression, DESCRIPTION);
}
}

private boolean isSqrtCall(CallExpression callExpression) {
Expression callee = callExpression.callee();

// Check for direct calls to math.sqrt
if (callee.is(Tree.Kind.QUALIFIED_EXPR)) {
QualifiedExpression qualifiedExpression = (QualifiedExpression) callee;
String methodName = qualifiedExpression.name().name();

if ("sqrt".equals(methodName)) {
Expression qualifier = qualifiedExpression.qualifier();
if (qualifier != null && qualifier.is(Tree.Kind.NAME)) {
String qualifierName = qualifier.firstToken().value();
return "math".equals(qualifierName) || "np".equals(qualifierName) || "numpy".equals(qualifierName);
}
}
}

return false;
}

private boolean hasLoopParent(Tree tree) {
for (Tree parent = tree.parent(); parent != null; parent = parent.parent()) {
Tree.Kind kind = parent.getKind();
if (kind == Tree.Kind.FOR_STMT || kind == Tree.Kind.WHILE_STMT) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.python.checks;

import org.junit.Test;
import org.sonar.python.checks.utils.PythonCheckVerifier;

public class AvoidSqrtInLoopTest {
@Test
public void test() {
PythonCheckVerifier.verify("src/test/resources/checks/avoidSqrtInLoop.py", new AvoidSqrtInLoop());
}
}
75 changes: 75 additions & 0 deletions src/test/resources/checks/avoidSqrtInLoop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import math
import numpy as np

results = []
numbers = [1, 2, 3, 4, 5]
for n in numbers:
results.append(math.sqrt(n)) # Noncompliant {{Avoid using scalar sqrt functions in loops. Apply vectorized sqrt operations on arrays directly.}}

results = []
for x in [9, 16, 25]:
results.append(np.sqrt(x)) # Noncompliant {{Avoid using scalar sqrt functions in loops. Apply vectorized sqrt operations on arrays directly.}}

results = []
i = 0
while i < 3:
results.append(np.sqrt(i)) # Noncompliant {{Avoid using scalar sqrt functions in loops. Apply vectorized sqrt operations on arrays directly.}}
i += 1

results = []
for n in range(10):
val = math.sqrt(n) # Noncompliant {{Avoid using scalar sqrt functions in loops. Apply vectorized sqrt operations on arrays directly.}}
results.append(val)

class DataProcessor:
numbers = np.array([1, 4, 9])
sqrt_values = np.sqrt(numbers)

x = math.sqrt(25)

class Analyzer:
def __init__(self, data):
self.results = np.sqrt(np.array(data))

intermediate = []
for n in range(3):
intermediate.append(n)
final = np.sqrt(np.array(intermediate))

results = np.sqrt([x for x in range(5)])

def non_compliant_math_sqrt_in_for_loop():
numbers = [1, 2, 3, 4, 5]
results = []
for num in numbers:
results.append(math.sqrt(num)) # Noncompliant {{Avoid using scalar sqrt functions in loops. Apply vectorized sqrt operations on arrays directly.}}
return results

def non_compliant_numpy_scalar_sqrt_in_for_loop():
numbers = [1, 2, 3, 4, 5]
results = []
for num in numbers:
results.append(np.sqrt(num)) # Noncompliant {{Avoid using scalar sqrt functions in loops. Apply vectorized sqrt operations on arrays directly.}}
return results

def non_compliant_math_sqrt_in_while_loop():
numbers = [1, 2, 3, 4, 5]
results = []
i = 0
while i < len(numbers):
results.append(math.sqrt(numbers[i])) # Noncompliant {{Avoid using scalar sqrt functions in loops. Apply vectorized sqrt operations on arrays directly.}}
i += 1
return results

def compliant_numpy_vectorized_sqrt():
numbers = np.array([1, 2, 3, 4, 5])
results = np.sqrt(numbers)
return results

def compliant_list_comprehension_with_vectorized_operation():
numbers = [1, 2, 3, 4, 5]
return np.sqrt(np.array(numbers))

def compliant_math_sqrt_outside_loop():
value = 16
return math.sqrt(value)