Can S1515 (no functions declared inside a loop) be smarter?

After the SonarQube instance I use was updated recently, I got a lot of flags for the new rule S1515. All the code looked like this:

const tasks = [];
while(collection.length > 0) {
  const batch = collection.splice(0, batchSize);
  tasks.push(new Task(() => doSomethingWith(batch)));
}

Smart static analysis could tell us that the function defined inside the Task constructor does not access any state outside the loop, and is therefore safe. Is this possible with the current JS/TS engine?

Hey!

Thanks for reporting the problem, ticket is created to make the rule smarter https://github.com/SonarSource/SonarJS/issues/1998

1 Like

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