Skip to content

Commit 79ada60

Browse files
joke1196sonartech
authored andcommitted
SONARPY-3940 Fix QG (#976)
GitOrigin-RevId: a3f1b7805bd02b915f03b140dd9ae07758a7ef4e
1 parent e5433af commit 79ada60

File tree

5 files changed

+17
-29
lines changed

5 files changed

+17
-29
lines changed

python-checks/src/main/java/org/sonar/python/checks/ConstantConditionCheck.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.sonar.plugins.python.api.tree.ComprehensionIf;
2929
import org.sonar.plugins.python.api.tree.ConditionalExpression;
3030
import org.sonar.plugins.python.api.tree.Expression;
31-
import org.sonar.plugins.python.api.tree.FileInput;
3231
import org.sonar.plugins.python.api.tree.HasSymbol;
3332
import org.sonar.plugins.python.api.tree.IfStatement;
3433
import org.sonar.plugins.python.api.tree.Name;

python-checks/src/main/java/org/sonar/python/checks/HttpNoContentNonEmptyBodyCheck.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ public class HttpNoContentNonEmptyBodyCheck extends PythonSubscriptionCheck {
5151
TypeMatchers.withFQN("fastapi.applications.FastAPI.patch"),
5252
TypeMatchers.withFQN("fastapi.applications.FastAPI.options"),
5353
TypeMatchers.withFQN("fastapi.applications.FastAPI.head"),
54-
TypeMatchers.withFQN("fastapi.applications.FastAPI.trace")
55-
);
54+
TypeMatchers.withFQN("fastapi.applications.FastAPI.trace"));
5655

5756
@Override
5857
public void initialize(Context context) {
59-
context.registerSyntaxNodeConsumer(Tree.Kind.FUNCDEF, this::checkFunctionDef);
58+
context.registerSyntaxNodeConsumer(Tree.Kind.FUNCDEF, HttpNoContentNonEmptyBodyCheck::checkFunctionDef);
6059
}
6160

62-
private void checkFunctionDef(SubscriptionContext ctx) {
61+
private static void checkFunctionDef(SubscriptionContext ctx) {
6362
FunctionDef functionDef = (FunctionDef) ctx.syntaxNode();
6463

6564
if (!isFastApiEndpointWithNoContentStatus(ctx, functionDef)) {
@@ -73,9 +72,7 @@ private static boolean isFastApiEndpointWithNoContentStatus(SubscriptionContext
7372
for (Decorator decorator : functionDef.decorators()) {
7473
Expression decoratorExpression = decorator.expression();
7574

76-
if (decoratorExpression.is(Tree.Kind.CALL_EXPR)) {
77-
CallExpression callExpr = (CallExpression) decoratorExpression;
78-
75+
if (decoratorExpression instanceof CallExpression callExpr) {
7976
if (!FASTAPI_METHODS_MATCHER.isTrueFor(callExpr.callee(), ctx)) {
8077
continue;
8178
}
@@ -100,7 +97,7 @@ private static boolean isNoContentStatusValue(Expression expr) {
10097
return false;
10198
}
10299

103-
private void findProblematicReturns(SubscriptionContext ctx, FunctionDef functionDef) {
100+
private static void findProblematicReturns(SubscriptionContext ctx, FunctionDef functionDef) {
104101
List<ReturnStatement> allReturns = new ArrayList<>();
105102
collectReturnStatements(functionDef.body(), allReturns);
106103

@@ -135,7 +132,7 @@ private static void collectReturnStatements(Tree tree, List<ReturnStatement> ret
135132
tree.children().forEach(child -> collectReturnStatements(child, returns));
136133
}
137134

138-
private ValidationResult isValidReturnStatement(SubscriptionContext ctx, ReturnStatement returnStmt) {
135+
private static ValidationResult isValidReturnStatement(SubscriptionContext ctx, ReturnStatement returnStmt) {
139136
List<Expression> expressions = returnStmt.expressions();
140137

141138
if (expressions.isEmpty()) {
@@ -161,39 +158,31 @@ private ValidationResult isValidReturnStatement(SubscriptionContext ctx, ReturnS
161158
return new ValidationResult(false);
162159
}
163160

164-
private ValidationResult isValidResponseObject(SubscriptionContext ctx, Expression expr) {
161+
private static ValidationResult isValidResponseObject(SubscriptionContext ctx, Expression expr) {
165162
if (!FASTAPI_RESPONSE_INSTANCE.isTrueFor(expr, ctx)) {
166163
return new ValidationResult(false);
167164
}
168165

169166
List<Tree> secondaryLocations = new ArrayList<>();
170167

171-
if (expr.is(Tree.Kind.NAME)) {
172-
Name name = (Name) expr;
168+
if (expr instanceof Name name) {
173169
var assignedValues = ctx.valuesAtLocation(name);
174170

175171
boolean anyInvalid = false;
176172
for (Expression assignedValue : assignedValues) {
177-
if (assignedValue.is(Tree.Kind.CALL_EXPR)) {
178-
CallExpression callExpr = (CallExpression) assignedValue;
179-
173+
if (assignedValue instanceof CallExpression callExpr && isInvalidResponseCall(callExpr)) {
180174
// Check if this Response has invalid arguments
181-
if (isInvalidResponseCall(callExpr)) {
182-
anyInvalid = true;
183-
secondaryLocations.add(assignedValue);
184-
}
175+
anyInvalid = true;
176+
secondaryLocations.add(assignedValue);
185177
}
186178
}
187179

188180
if (anyInvalid) {
189181
return new ValidationResult(false, secondaryLocations);
190182
}
191-
} else if (expr.is(Tree.Kind.CALL_EXPR)) {
183+
} else if (expr instanceof CallExpression callExpr && isInvalidResponseCall(callExpr)) {
192184
// Direct Response constructor call
193-
CallExpression callExpr = (CallExpression) expr;
194-
if (isInvalidResponseCall(callExpr)) {
195-
return new ValidationResult(false);
196-
}
185+
return new ValidationResult(false);
197186
}
198187

199188
return new ValidationResult(true);

python-checks/src/main/java/org/sonar/python/checks/NumpyListOverGeneratorCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private void checkNumpyArrayCall(SubscriptionContext ctx) {
5050
.ifPresent(fqn -> checkGeneratorCallee(call, ctx));
5151
}
5252

53-
private void checkGeneratorCallee(CallExpression call, SubscriptionContext ctx) {
53+
private static void checkGeneratorCallee(CallExpression call, SubscriptionContext ctx) {
5454
List<Argument> argList = call.arguments();
5555
if (argList.isEmpty()) {
5656
return;

python-checks/src/main/java/org/sonar/python/checks/PytzTimeZoneInDatetimeConstructorCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void checkCallExpression(SubscriptionContext context) {
5353
}
5454
}
5555

56-
private void checkArgument(RegularArgument argument, SubscriptionContext context) {
56+
private static void checkArgument(RegularArgument argument, SubscriptionContext context) {
5757
if (argument.expression().is(Tree.Kind.CALL_EXPR)) {
5858
CallExpression callExpression = (CallExpression) argument.expression();
5959
Symbol calleeSymbol = callExpression.calleeSymbol();

python-checks/src/main/java/org/sonar/python/checks/tests/UnconditionalAssertionCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ private static boolean isFalseOrZeroLiteral(Expression expression) {
108108
return false;
109109
}
110110

111-
private void checkNoneAssertion(SubscriptionContext ctx, CallExpression call, RegularArgument arg) {
111+
private static void checkNoneAssertion(SubscriptionContext ctx, CallExpression call, RegularArgument arg) {
112112
if (isUnconditional(arg, ctx)) {
113113
ctx.addIssue(call, NONE_MESSAGE);
114114
}
115115
}
116116

117-
private void checkBooleanAssertion(SubscriptionContext ctx, RegularArgument arg) {
117+
private static void checkBooleanAssertion(SubscriptionContext ctx, RegularArgument arg) {
118118
if (isUnconditional(arg, ctx)) {
119119
ctx.addIssue(arg, BOOLEAN_MESSAGE);
120120
}

0 commit comments

Comments
 (0)