fb-contrib icon indicating copy to clipboard operation
fb-contrib copied to clipboard

OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION: False Positive on final static initialized fields

Open aepfli opened this issue 5 years ago • 7 comments

OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION is producing false positives when using static methods which are returning statically initialized fields, like Collections.emptyList().

List list = Optional.of(null).orElse(Collections.emptyList());

This will cause an issue. But Collections.emptyList() is returning Collections.EMPTY_LIST which is a final static field with an empty list. hence that this is producing a false positive.

This false positive applies at least for:

  • https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#emptyList()
  • https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#emptySet()
  • https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#emptyMap()
  • https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/ImmutableList.html#of--
  • https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/ImmutableSet.html#of--
  • https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/ImmutableMap.html#of--

With a little guidance, i might be able to fix this issue and provide a pull request. if this is easily achievable.

aepfli avatar Mar 06 '19 08:03 aepfli

There are probably a bunch of methods that are so non-impactful in nature that reporting this violation is pointless. The problem is how to you determine which methods do you consider basically no-ops. The simple solution is to just have a list of commonly known simple methods to ignore, which i've done in the past for other things. Certainly would be fine to do here as well, but it's a hack, at it's basis.

mebigfatguy avatar Mar 07 '19 05:03 mebigfatguy

@aepfli You could just use Collections::emptyList, right?

ThrawnCA avatar Mar 07 '19 05:03 ThrawnCA

@ThrawnCA no we cant, because this is not lambda but an object -> we could use .orElseGet(Collections::emptyList) as a work arround.

Assigning a raw type to a generic one is not type safe, and will generate a warning. The old EMPTY_... fields of the Collections class return raw types, whereas the newer empty...() methods return generic ones. https://rules.sonarsource.com/java/RSPEC-1596

aepfli avatar Mar 07 '19 06:03 aepfli

i am not that familiar with spotbugs and its capabilities

i would love to try to detect if a method just returns a final static field - and if so do not report the issue. as a first step. if you think that is suitable @mebigfatguy. else i will go for the whitelisting, but as you are said already, there are most likely a lot of those methods, and white-listing will cause more maintainance, in the end to keep it up to date (find all usages). so i would rather for a deterministic approach :D

aepfli avatar Mar 07 '19 07:03 aepfli

FindBugs is pass-based. so you can run over the code base multiple times collecting info. In an earlier pass you could determine what methods just return a static constant, and build a set up of those methods, then in the later pass, where this detector runs, you can look at that set to see if the method in question is one of them.

mebigfatguy avatar Jun 30 '19 19:06 mebigfatguy

Also applies when constants are used directly:

int x = Optional.ofNullable(getSomeInteger()).orElse(42);

zman0900 avatar Sep 20 '19 00:09 zman0900

yeah, definitely a stupid report. it's reporting the Integer.valueOf(42) call

mebigfatguy avatar Sep 24 '19 14:09 mebigfatguy