FP s1481 inside lambda function

*SonarQube 7.9.2, Scanner 5.3.0, SonarJava 6.0.2
Below code:

    private Observable<CompanyUser> findInAssociatedCompany(final Company company, final String pii, final String email) {
  
        return this.api.getEvent(a.COMPANY, companySearch, email, cs.class)
                .flatMap(cs-> {
                   if (cs.getCompanies() == null) {
                       return this.action();
                   }
                   List<Company> associatedCompany = cs.getCompanies()
                           .stream()
                           .filter(someCompany -> this.findStaff(someCompany, pii).isPresent())
                           .collect(c.toList());
                   if(!associatedCompany.isEmpty()) {
                       return xxxxxxx;
                   }
                    return xxxxxx
                });
    }

Reports
‚Äúremove this unused ‚ÄúassociatedCompany‚ÄĚ local variable‚ÄĚ
image
What is unexpected as associatedCompany is used
The same code

                   List<Company> associatedCompany = cs.getCompanies()
                           .stream()
                           .filter(someCompany -> this.findStaff(someCompany, pii).isPresent())
                           .collect(c.toList());
                   if(!associatedCompany.isEmpty()) {
                       return xxxxxxx;
                   }

Is placed outside of lambda function, then there is no issue reported.

I have the same problem (I think).

Sonarqube 8.1.0
Sonar-Scanner-Jenkins 2.11
SonarJava 6.0.2

Hey @aplaczek & @KajNystrom,

Problems are indeed related and impacting two rules

  • S1481: Unused local variables
  • S1854: Unused assignments should be removed

From what we investigated, however, this seems to be a problem in terms of project configuration (so on your side).

When some dependencies are missing, or the provided bytecode is partial, our semantic engine can not resolve correctly the lambdas (i.e. map them to their functional interfaces). As a consequence, the usages of variable is broken, leading to FPs.

From what you both described, you are using sonar-scanner for your analyses. Can you check that you are providing all the dependencies to your project, as well as the bytecode of your project?

You might want to have a look at the documentation of the java plugin: Java Analysis and Bytecode

In the meantime, the following ticket will try to reduce the noise when bytecode is missing or partial: SONARJAVA-3281

Cheers,
Michael

2 Likes

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