openrewrite / rewrite-static-analysis

OpenRewrite recipes for identifying and fixing static analysis issues.
Apache License 2.0
27 stars 43 forks source link

`RemoveUnusedPrivateMethods` should ignore class annotated with `@SupressWarning("unused")` #293

Closed Dinozavvvr closed 1 month ago

Dinozavvvr commented 1 month ago

Fix #294

timtebeek commented 1 month ago

Thanks a lot for the start @Dinozavvvr ! As indicated in the issue we'll likely want to add an override of visitClassDeclaration, that uses FindAnnotations to find the suppress annotation on the class, inspects the value, and if that contains unused then do not call super.visitClassDeclaration, but return the original argument classDeclaration. That'll be in between these lines in the recipe itself. https://github.com/openrewrite/rewrite-static-analysis/blob/b7b1bd1fd82c5e713d7ea5777375c6daf6eb9067/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethods.java#L55-L57

Dinozavvvr commented 1 month ago

@timtebeek thank you a lot for your suggestion!

Dinozavvvr commented 1 month ago

@timtebeek I found that solution based on visiting J.ClassDeclaration ignores nested classes (I'm not sure should it or not), so if is it I will move this check to visitMethodDeclaration

Dinozavvvr commented 1 month ago

Did it

timtebeek commented 1 month ago

Great to see you've worked this out @Dinozavvvr ! I've added a small polishing commit to better fit in with our code conventions. Seems like a handy pattern to limit recipe execution by looking for suppressed warnings. We might just want to apply that in a few more cases, or pull this up to a higher level even. Neat stuff!