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

Remove unused local variables despite potential side effects after removing feature flag #35

Merged
merged 4 commits into from
Oct 23, 2024

Conversation

kavitha186
Copy link
Contributor

@timtebeek I would like to first fix this one RemoveUnusedLocalVariables does not clean up LDContext when the assignment might have side effects, such as when using the builder. Might need a separate recipe that ignores the potential builder side effect.

I'm thinking to approach this as below

  1. To update the below recipe to handle the Local variable removal when there is a builder assigned and if the variable is not use in the method? https://github.com/openrewrite/rewrite-static-analysis/blob/cba8ef0a14f87056d42e1d7f4d26b0617af4e614/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java#L37

Please confirm if it would work or should I create a new recipe to handle below special cases while removing local variable.

  • with builder assigned
  • if it just used in logging context.toString()
  • if it is just used in logging as log.info("log the context", context.toString())

@kavitha186 kavitha186 closed this Oct 20, 2024
@kavitha186 kavitha186 reopened this Oct 20, 2024
@kavitha186 kavitha186 marked this pull request as draft October 20, 2024 04:02
@kavitha186 kavitha186 force-pushed the feat/removeunusedLocalVariable branch from 8b1b29b to 57a6ecf Compare October 21, 2024 01:51
@kavitha186
Copy link
Contributor Author

@timtebeek I had 2 test cases and one of them where LDContext had assigned builder methods - Expectation is the builder methods also has to be cleaned up.

An another test cases where I tried to use Create method on LDContext and builderWithContext, I'm expecting it to clear everything , but both the actual and expected result is so unexpected . Can you please what is expected in the testcase "removeUnusedLDContextWithBuilderContext". thanks .

@timtebeek
Copy link
Contributor

hi @kavitha186 ! Great to see these test cases added; I think indeed you're right in expecting these LDContext objects to be cleared out where possible.

Rather than a separate recipe it might make most sense to add a new nullable argument to RemoveUnusedLocalVariables for something like: Boolean withSideEffects, and then never set mightSideEffect to true for method invocations here. We could tackle that as a separate contribution, such that this PR ideally only needs to add that new argument to constructor in

doAfterVisit(new RemoveUnusedLocalVariables(null).getVisitor());

Would you be willing to push up that matching change as well? Much appreciated that you're chiming in here for the improvements. :)

@timtebeek
Copy link
Contributor

I've pushed up a small PR based on the above that should unblock this use case here; thanks again for the suggestion!

timtebeek added a commit to openrewrite/rewrite-static-analysis that referenced this pull request Oct 23, 2024
@timtebeek timtebeek added the enhancement New feature or request label Oct 23, 2024
@timtebeek timtebeek marked this pull request as ready for review October 23, 2024 10:25
@timtebeek timtebeek changed the title feat(test): added failing test for RemoveUnusedLocalVariables Remove unused local variables despite potential side effects after removing feature flag Oct 23, 2024
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition @kavitha186 ; I think we can merge this increment already, even if there's potentially still those unused fields to clear out, as this is already a nice improvement.

@timtebeek timtebeek merged commit e481645 into openrewrite:main Oct 23, 2024
2 checks passed
@kavitha186
Copy link
Contributor Author

Thanks @timtebeek for merging this Pr. I will pass boolean flag from this RemoveBoolVariation as true and will implement the get visitor on Removeunusedlocalvariable to check for any builder or any other methods used from LDContext.

@timtebeek
Copy link
Contributor

Hi @kavitha186 that should mostly be covered already with the changes I added here. But let me know if there's anything left to do still!

@kavitha186
Copy link
Contributor Author

yes looks like all covered . Will take up next item from the issues..thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants