spotbugs / discuss

SpotBugs mailing list
6 stars 1 forks source link

EI_EXPOSE_REP and EI_EXPOSE_REP2 failures when using Immutable objects from Guava. How should this be corrected? #113

Open callum-kilby opened 3 years ago

callum-kilby commented 3 years ago

We are using Guava Immutable objects in our project, and from SpotBugs 4.3.0 we have SpotBugs failures EI_EXPOSE_REP and EI_EXPOSE_REP2 when directly setting/retrieving class fields as Immutable objects.

For example:

private Map<String, String> overrides;

public CLISetup(String commandLine, ImmutableMap<String, String> overrides) {
    this.commandLine = commandLine;
    this.overrides = overrides;
}

results in an EI_EXPOSE_REP2 failure from this.overrides = overrides.

This failure is incorrect because the object being passed in and set for the class field is shallowly immutable. What can be done to fix these failures? I'm hoping we don't need to completely exclude EI_EXPOSE_REP and EI_EXPOSE_REP2, or annotate everywhere we do something like this.

baloghadamsoftware commented 3 years ago

Thank you for your report, I will add ImmutableMap & co. to the list of immutable objects. (Unfortunately I cannot assign this bug to myself since I am not yet an official project member.)

baloghadamsoftware commented 3 years ago

Just a side note: Either make this an issue (https://github.com/spotbugs/spotbugs/issues) or move this discussion to here: https://github.com/spotbugs/spotbugs/discussions.

callum-kilby commented 3 years ago

:+1: thanks very much, I've created an issue with the same description here: https://github.com/spotbugs/spotbugs/issues/1601