@@ -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 );
0 commit comments