Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EI_EXPOSE_REP false positive #2083

Open
oliveryasuna opened this issue Jun 4, 2022 · 4 comments
Open

EI_EXPOSE_REP false positive #2083

oliveryasuna opened this issue Jun 4, 2022 · 4 comments

Comments

@oliveryasuna
Copy link

The below should not report EI_EXPOSE_REP, but does.

public enum UIState implements {

  INITIALIZING(Collections.emptySet()),

  RUNNING(Collections.singleton(INITIALIZING)),

  TERMINATED(Collections.singleton(RUNNING));

  UIState(final Set<UIState> transitions) {
    this.transitions = Collections.unmodifiableSet(transitions);
  }

  private final Set<UIState> transitions;

  public final Set<UIState> transitions() {
    return transitions;
  }

}

Changing to the following, it is not reported.

public enum UIState implements {

  INITIALIZING(Collections.emptySet()),

  RUNNING(Collections.singleton(INITIALIZING)),

  TERMINATED(Collections.singleton(RUNNING));

  UIState(final Set<UIState> transitions) {
    this.transitions = transitions;
  }

  private final Set<UIState> transitions;

  public final Set<UIState> transitions() {
    return Collections.unmodifiableSet(transitions);
  }

}
@welcome
Copy link

welcome bot commented Jun 4, 2022

Thanks for opening your first issue here! 😃
Please check our contributing guideline. Especially when you report a problem, make sure you share a Minimal, Complete, and Verifiable example to reproduce it in this issue.

@baloghadamsoftware
Copy link
Contributor

Hello! You are right, this is indeed a false positive. The reason for the report is that the FindReturnRef detector checks the methods one by one: if it finds a return statement which returns a private variable whose static type is (probably) mutable then it reports the bug. What the detector is missing is that it should check each assignment to the attribute and not report the bug if all the assignments are immutable such as Collections.unmodifiable*() which means that its dynamic type is always immutable. However, this requires some fundamental changes in the detector. I will think a bit about the best possible solution.

@osiegmar
Copy link

osiegmar commented Aug 8, 2022

Similar to #1771

maisonobe added a commit to Hipparchus-Math/hipparchus that referenced this issue Apr 24, 2023
These false positives correspond to regular object oriented design.
SpotBugs has just become too stringent (see
spotbugs/spotbugs#1601,
       spotbugs/spotbugs#1771,
spotbugs/spotbugs#2083,
       spotbugs/spotbugs#2344,
spotbugs/spotbugs#2356,
       spotbugs/spotbugs-gradle-plugin#731)
@nmatt
Copy link

nmatt commented Jan 21, 2024

I'd like to add that this makes EI_EXPOSE_REP close to useless, since having e.g. a private field List that was constructed as unmodifiable in the constructor, is the standard way to prevent exposing mutable collection-valued fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants