APEX - SOQL in loops is not working in SonarQube version 8.9

Must-share information (formatted with Markdown):

  • SonarQube Enterprise Edition Version 8.9 (build 43852)
  • My current version of the SonarQube is not detecting SOQL in for loops. This is a very basic rule and should exist in code scan. Any idea when will this rule be available?
  • None

Hey there.

Maybe you can provide a full code snippet where you expect an issue. Today, we do have one rule that sounds similar: Apex static code analysis: DML statements should not be used inside loops

Hi Colin,

DML statements are different from SOQLs. Example pseudo-code is given below.

Example 1:

for(Account a : accountList){
        List<Contact> cList = [SELECT id from Contact]; // SOQL query running in a loop.
}

Example 2:

for(integer i=0; i<100; i++){
      List<Contact> cList = [SELECT id from Contact]; // SOQL query running in a loop.
}

Example 3:

for(Account a : [SELECT Id, Name FROM Account] ){
       List<Contact> cList = [SELECT id from Contact]; // SOQL query running in a loop.
}

In all the above 3 examples, if we use a DML statement that includes insert/update/delete, the existing SONAR rule works.

Hello @npathuri0531 and thanks for the suggestion.

We already specified rule S5383: SOQL queries should not be used inside loops. That being said, I have to say that the implementation is not the priority currently. We take good note of your request and will be happy to reconsider if we see general interest.

In any way, you can track the progress on this ticket: SONARSLANG-384.

Best,
Quentin

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.