diff --git a/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/bsl/check/QueryInLoopCheck.java b/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/bsl/check/QueryInLoopCheck.java index 7da82e9a..6ff1996f 100644 --- a/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/bsl/check/QueryInLoopCheck.java +++ b/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/bsl/check/QueryInLoopCheck.java @@ -43,6 +43,7 @@ import com._1c.g5.v8.dt.bsl.model.Invocation; import com._1c.g5.v8.dt.bsl.model.LoopStatement; import com._1c.g5.v8.dt.bsl.model.Method; import com._1c.g5.v8.dt.bsl.model.Module; +import com._1c.g5.v8.dt.bsl.model.Statement; import com._1c.g5.v8.dt.bsl.model.StaticFeatureAccess; import com._1c.g5.v8.dt.bsl.model.WhileStatement; import com._1c.g5.v8.dt.bsl.model.util.BslUtil; @@ -387,6 +388,28 @@ public class QueryInLoopCheck return predicate instanceof BooleanLiteral; } + private Collection getQueryInLoopFeatures(LoopStatement loopStatement, + Map methodsWithQuery, Set queryExecutionMethods) + { + Collection result = new ArrayList<>(); + + for (Statement statement : loopStatement.getStatements()) + { + for (FeatureAccess featureAccess : EcoreUtil2.eAllOfType(statement, FeatureAccess.class)) + { + if (featureAccess instanceof StaticFeatureAccess + && isMethodWithQueryCalled((StaticFeatureAccess)featureAccess, methodsWithQuery) + || featureAccess instanceof DynamicFeatureAccess + && isQueryExecution((DynamicFeatureAccess)featureAccess, queryExecutionMethods)) + { + result.add(featureAccess); + } + } + } + + return result; + } + private Collection getQueryInLoopCallers(Module module, Map methodsWithQuery, Set queryExecutionMethods, boolean checkQueryInInfiniteLoop, IProgressMonitor monitor) { @@ -404,16 +427,7 @@ public class QueryInLoopCheck continue; } - for (FeatureAccess featureAccess : EcoreUtil2.eAllOfType(loopStatement, FeatureAccess.class)) - { - if (featureAccess instanceof StaticFeatureAccess - && isMethodWithQueryCalled((StaticFeatureAccess)featureAccess, methodsWithQuery) - || featureAccess instanceof DynamicFeatureAccess - && isQueryExecution((DynamicFeatureAccess)featureAccess, queryExecutionMethods)) - { - result.add(featureAccess); - } - } + result.addAll(getQueryInLoopFeatures(loopStatement, methodsWithQuery, queryExecutionMethods)); } return result; diff --git a/tests/com.e1c.v8codestyle.bsl.itests/resources/query-in-loop.bsl b/tests/com.e1c.v8codestyle.bsl.itests/resources/query-in-loop.bsl index 93f69adf..d07d2aa7 100644 --- a/tests/com.e1c.v8codestyle.bsl.itests/resources/query-in-loop.bsl +++ b/tests/com.e1c.v8codestyle.bsl.itests/resources/query-in-loop.bsl @@ -1,3 +1,5 @@ +// @strict-types + Procedure QueryCorrect(SomeParameter) Export SimpleQuery = New Query; @@ -26,6 +28,8 @@ Procedure MethodCallsQueryCorrect(SomeParameter) Export EndProcedure +// Parameters: +// SomeArray - Array Procedure ForEachStatementIncorrect(SomeArray) Export ForEachQuery = New Query; @@ -42,6 +46,8 @@ Procedure ForEachStatementIncorrect(SomeArray) Export EndProcedure +// Parameters: +// SomeArray - Array Procedure ForToStatementIncorrect(SomeArray) Export ForToQuery = New Query; @@ -58,6 +64,8 @@ Procedure ForToStatementIncorrect(SomeArray) Export EndProcedure +// Parameters: +// SomeArray - Array Procedure WhileStatementIncorrect(SomeArray) Export WhileQuery = New Query; @@ -72,12 +80,16 @@ Procedure WhileStatementIncorrect(SomeArray) Export EndProcedure +// Parameters: +// SomeArray - Array Procedure MethodCallsIncorrectMethodCorrect(SomeArray) Export ForEachStatementIncorrect(SomeArray); EndProcedure +// Parameters: +// SomeParameter - Number Procedure LoopCallsMethodIncorrect(SomeParameter) Export LoopCallQuery = New Query; @@ -93,6 +105,8 @@ Procedure LoopCallsMethodIncorrect(SomeParameter) Export EndProcedure +// Parameters: +// SomeParameter - Number Procedure LoopCallsMethodWithOtherMethodIncorrect(SomeParameter) Export MethodCallQuery = New Query; @@ -107,6 +121,8 @@ Procedure LoopCallsMethodWithOtherMethodIncorrect(SomeParameter) Export EndProcedure +// Parameters: +// SomeArray - Array Procedure LoopWithConditionsIncorrect(SomeArray) Export While SomeArray.Count() = 0 Do @@ -124,6 +140,11 @@ Procedure LoopWithConditionsIncorrect(SomeArray) Export EndProcedure +// Parameters: +// ArrayElement - String +// +// Returns: +// Query Function GetNewQuery(ArrayElement) FunctionQuery = New Query; @@ -138,6 +159,8 @@ Function GetNewQuery(ArrayElement) EndFunction +// Parameters: +// SomeArray - Array Procedure QueryTypeFromFunctionIncorrect(SomeArray) Export For Each ArrayElement In SomeArray Do @@ -146,3 +169,25 @@ Procedure QueryTypeFromFunctionIncorrect(SomeArray) Export EndDo; EndProcedure + +Function QueryResultColumn() Export + + FunctionQuery = New Query; + + FunctionQuery.Text = + "SELECT + | 1"; + + Return FunctionQuery.Execute().Unload().UnloadColumn(0); + +EndFunction + +Procedure ForEachParamQueryMethodCorrect() Export + + Result = 0; + + For Each Num In QueryResultColumn() Do + Result = Result + Num; + EndDo; + +EndProcedure \ No newline at end of file diff --git a/tests/com.e1c.v8codestyle.bsl.itests/src/com/e1c/v8codestyle/bsl/check/itests/QueryInLoopCheckTest.java b/tests/com.e1c.v8codestyle.bsl.itests/src/com/e1c/v8codestyle/bsl/check/itests/QueryInLoopCheckTest.java index cce97abd..c353021b 100644 --- a/tests/com.e1c.v8codestyle.bsl.itests/src/com/e1c/v8codestyle/bsl/check/itests/QueryInLoopCheckTest.java +++ b/tests/com.e1c.v8codestyle.bsl.itests/src/com/e1c/v8codestyle/bsl/check/itests/QueryInLoopCheckTest.java @@ -80,6 +80,8 @@ public class QueryInLoopCheckTest case "MethodCallsQueryCorrect": case "MethodCallsIncorrectMethodCorrect": case "GetNewQuery": + case "QueryResultColumn": + case "ForEachParamQueryMethodCorrect": { // Those methods doesn't have errors break;