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

False-positive MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR for method reference #461

Closed
nmatt opened this issue Jan 22, 2024 · 2 comments
Closed

Comments

@nmatt
Copy link

nmatt commented Jan 22, 2024

MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR is reported for mere method references, without them being invoked. This happens with sb-contrib version 7.6.4 (current release).

Example code:

public class Example
{
    public String s;
    
    public boolean overridable(String s) { return this.s.equals(s); }
    
    public final Predicate<String> predicate1 = this::overridable;

    public final Predicate<String> predicate2 = Predicate.not(this::overridable);
}

Both predicate1 and predicate2 produce the bug. In case of predicate1, the method is clearly not called. In the case of predicate2, it might of course theoretically be called, but there are also a lot of use cases (like here) where the method reference is just passed to be stored for later use. (One of the actual use cases I have is a memoization wrapper for the overridable method, stored as a field.)

One issue is that this also cannot be worked around by adding @SuppressFBWarnings("MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR") to the field declaration.

Maybe, if the analysis cannot determine that the overridable method is being passed for immediate invocation (like when passed to well-known methods like Map::compute), this should instead map to a separate bug code.

@mebigfatguy
Copy link
Owner

This is part of SpotBugs itself, please post the issue here

https://github.com/spotbugs/spotbugs/issues

@nmatt
Copy link
Author

nmatt commented Jan 24, 2024

Resubmitted as SpotBugs issue: spotbugs/spotbugs#2837

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

2 participants