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 for local record classes, local enum classes: UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS #2120

Closed
clo-vis opened this issue Jul 22, 2022 · 1 comment

Comments

@clo-vis
Copy link

clo-vis commented Jul 22, 2022

For the following example, I get several warnings "Uncallable method ... defined in anonymous class".

class FalsePositive {
    static void method() {
        record MyRecord(int from, int to) { /**/ }
        
        enum MyEnum {
            RED, BLUE;
        }
        
        System.out.println(new MyRecord(1, 2));
        System.out.println(MyEnum.RED);
    }
}

The example has no anonymous class (all classes have a name) and all uncallable methods pinpointed are callable (which doesn't mean I need to call them - they were generated by the compiler)

  • MyEnum.valueOf(String)
  • MyEnum.values()
  • MyRecord.from()
  • MyRecord.to()
baloghadamsoftware pushed a commit to baloghadamsoftware/spotbugs that referenced this issue Sep 9, 2022
…sed methods of method-local enumerations and records

During compilation, enumerations and records are turned into Java classes with some auto-generated methods. Some of these methods are called by the code but some of them not. However, this is not a programming error or bad practice, these methods must not be considered as dead code because their generation cannot be prevented. Therefore no bug report `UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS` must be issued to such classes even if they are defined inside a method. See issue ([spotbugs#2120](spotbugs#2120))
KengoTODA added a commit that referenced this issue Oct 11, 2022
…sed methods of method-local enumerations and records (#2170)

* Fix for detector `UncallableMethodOfAnonymousClass` to not report unused methods of method-local enumerations and records

During compilation, enumerations and records are turned into Java classes with some auto-generated methods. Some of these methods are called by the code but some of them not. However, this is not a programming error or bad practice, these methods must not be considered as dead code because their generation cannot be prevented. Therefore no bug report `UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS` must be issued to such classes even if they are defined inside a method. See issue ([#2120](#2120))

* Fix the test case

* Solution changed to only disregard auto-generated methods of enums and records

Co-authored-by: Kengo TODA <skypencil@gmail.com>
@gtoison
Copy link
Contributor

gtoison commented Aug 31, 2023

This was fixed by #2170

@gtoison gtoison closed this as completed Aug 31, 2023
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