IntelliJ - Remove this unused private method

475 views
Skip to first unread message

bes...@gmail.com

unread,
Mar 22, 2017, 9:59:33 AM3/22/17
to SonarLint
Hi,

i did install SonarLint for IntelliJ some weeks ago and I'm very happy with the on-the-fly analysis!

I did see multiple false positives with the "Remove this unused private method" warning.

For example here validateProjectSegmentAllocation is marked as unused but it's clearly used:

private List<String> validateProjectSegmentAllocations() {
 
return flatHistogramStore.getAllProjectAllocationData()
 
.valuesAsList()
 
.stream()
 
.flatMap(List::stream)
 
.map(this::validateProjectSegmentAllocation) //<- Used here
 .filter(Optional::isPresent)
 
.map(Optional::get)
 
.collect(Collectors.toList());
}

private Optional<String> validateProjectSegmentAllocation(ProjectSegmentAllocation projectSegmentAllocation) {
 
ProjectSegmentAllocationData projectSegmentAllocationModel = projectSegmentAllocation.getProjectAllocationModel();
 
Optional<ProjectSegmentAllocationData> allocationStoreModel = allocationStore.getProjectSegmentAllocationData(projectSegmentAllocationModel.getProjectSegmentAllocationReference());
 
if (!Optional.of(projectSegmentAllocationModel).equals(allocationStoreModel)) {
 
return Optional.of("Out of sync");
 
}
 
return Optional.empty();
}


SonarLint 2.8.0.1735
IntelliJ 2017.1
Java 1.8

Duarte Meneses

unread,
Mar 22, 2017, 10:22:25 AM3/22/17
to bes...@gmail.com, SonarLint
Hi,

Great that you find the on-the-fly analysis useful.
Some of the rules use the bytecode in the compiled class. Could you please try to compile the class and see if FPs disappear? 

--
You received this message because you are subscribed to the Google Groups "SonarLint" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarlint+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarlint/328fd66c-a9ac-4cbf-9401-080359e2742c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Duarte MENESES | SonarSource

bes...@gmail.com

unread,
Mar 22, 2017, 10:35:34 AM3/22/17
to SonarLint, bes...@gmail.com
Hi Duarte,

thank you for your fast reply but compiling doesn't change anything.
The project is a GWT 2.8 project (but that shouldn't change anything.

To unsubscribe from this group and stop receiving emails from it, send an email to sonarlint+...@googlegroups.com.

duarte.meneses

unread,
Mar 22, 2017, 11:01:06 AM3/22/17
to SonarLint, bes...@gmail.com
Ok, thanks. I'm not able to reproduce the problem with similar code.
Could you please go to the SonarLint tool window, active the debug logging and see what is the configuration of the analysis?
There is the possibility that the usage of GWT is affecting somehow the way SonarLint discovers the bytecode (.class file) of the class.

You should see in "extraProperties", sonar.binaries with the path where the compiled classes are.

bes...@gmail.com

unread,
Mar 22, 2017, 12:47:44 PM3/22/17
to SonarLint, bes...@gmail.com
The configuration log output (shortened and anonymised):

Starting analysis with configuration:
[
  baseDir: C:\appname\application
  workDir: C:\appname\application\.idea\sonarlint
  extraProperties: {sonar.java.target=1.8, sonar.java.libraries=... sonar.java.source=8, sonar.java.binaries=C:/appname/application/web-client/target/classes, ...}
  inputFiles: [
    C:/appname/application/web-client/src/main/java/de/company/appname/client/utils/ClassName.java
  ]
]

The .class file is under: C:\appname\application\web-client\target\classes\de\company\appname\client\utils\ClassName.class

bes...@gmail.com

unread,
Mar 22, 2017, 1:40:19 PM3/22/17
to SonarLint, bes...@gmail.com
You don't need GWT to reproduce this false positive.

I have created a minimal example:

Create a simple Java commandline project and add this code:

import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
   
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());

   
private Main() {
   
}

   
public static void main(String[] args) {
       
new Main().anyMethod();
   
}

   
private void anyMethod() {
       
List<String> list = Arrays.asList("A", "B", "C");
       
List<String> strings = Stream.of(list, list)
               
.flatMap(List::stream)
               
.map(this::usedMethod)
               
.collect(Collectors.toList());

       
if(strings.isEmpty())
           
LOGGER.log(Level.FINE, strings::toString);
   
}

   
private String usedMethod(String s) {
       
return !s.isEmpty() ? "" : "error";
   
}
}

You will see that usedMethod will be marked with "Remove this unused private "usedMethod" method."

I hope you can fix this bug.

janos....@sonarsource.com

unread,
Mar 23, 2017, 10:19:34 AM3/23/17
to SonarLint, bes...@gmail.com
Hi,

Thanks for the reproducer.

I believe this is the same as this issue:

You can track progress by monitoring this ticket.

Cheers,
Janos
Reply all
Reply to author
Forward
0 new messages