Search code examples
angulartypescriptsonarqube

Angular Typescript sonar error The return value of "filter" must be used


I have the error in sonar:

The return value of "filter" must be used

But I see that is using the filter. What could be wrong there?

array.filter(item => {
item.value.split(' ').forEach( i => {
    if ( doSomething(i).toLowerCase().indexOf(doSomething(term).toLowerCase()) === 0 ) {
      arrayResult.push(item);
    }
});

Solution

  • You are using filter but you're ignoring the result it's returning. Calling filter() constructs a new array without modifying the original. The whole point of calling filter is to make this new array, so if you're not using it then the program is doing unnecessary work and filter is most likely the wrong operation to use.

    In your example it looks like you just want to iterate over the items and you're pushing values to another array yourself, so something like forEach might be better.