-
Notifications
You must be signed in to change notification settings - Fork 578
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
Fix for detector UncallableMethodOfAnonymousClass
to not report unused methods of method-local enumerations and records
#2170
Conversation
…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))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From next, please consider to make two commits in one PR: one is only for failing test cases, and next is fix with documents. Then we can use GitHub Actions to verify that test and fix are enough or not.
if (Values.DOTTED_JAVA_LANG_ENUM.equals(obj.getSuperclassName()) || | ||
Values.DOTTED_JAVA_LANG_RECORD.equals(obj.getSuperclassName())) { | ||
return; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test passes even when I revert this change. I assume that the test case is not enough to reproduce the reported problem.
$ ./gradlew clean spotbugs-tests:test --tests=Issue2120Test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for noticing this. I forgot to add the inner classes to the test. (I always do the tests first, run them manually to see them fail, and then I do the fix.)
Good idea! Thank you! I will do as you suggested. |
@@ -59,6 +59,11 @@ public UncallableMethodOfAnonymousClass(BugReporter bugReporter) { | |||
|
|||
@Override | |||
public void visitJavaClass(JavaClass obj) { | |||
if (Values.DOTTED_JAVA_LANG_ENUM.equals(obj.getSuperclassName()) || | |||
Values.DOTTED_JAVA_LANG_RECORD.equals(obj.getSuperclassName())) { | |||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There could be extra methods added to an Enum that would validly benefit from the rule, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, you are right. I will change this to only disregard auto-generated methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changelog has a conflict.
No new false positives or crashes on some open-source projects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
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)Make sure these boxes are checked before submitting your PR -- thank you!
CHANGELOG.md
if you have changed SpotBugs code