Skip to content

Conversation

@Anmol202005
Copy link
Collaborator

Fixes: #33
Implemented FinalLocalVariable recipe.

@Anmol202005 Anmol202005 marked this pull request as draft July 8, 2025 19:49
@Anmol202005 Anmol202005 marked this pull request as ready for review July 8, 2025 20:01
@Anmol202005
Copy link
Collaborator Author

the recipe currently has partial coverage: When multiple variables are declared together like int a, b, c; and only one of them has a violation, the recipe won't process it.

@Anmol202005
Copy link
Collaborator Author

@rdiachenko kindly review.

@rdiachenko rdiachenko requested a review from timurt July 8, 2025 20:49
import org.junit.jupiter.api.Test;
import org.openrewrite.Recipe;

public class FinalLocalVariableTest extends AbstractRecipeTest {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that https://checkstyle.sourceforge.io/checks/coding/finallocalvariable.html#FinalLocalVariable has different params

Can you please add tests covering the following cases to make sure it works correctly

for (String item : items) {
for (int i = 0; i < 10; i++)
public void processData(String data, int count)
final int alreadyFinal
try (FileReader fr = new FileReader(filename)) {
int a, b, c;
items.forEach(item -> System.out.println(item));

If it requires too much rework, let me know

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will apply fixes on the bases of violation report.
None of the param will be helpful.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, let me clarify

Not asking to support FinalLocalVariable's params

What I meant is that Checkstyle's FinalLocalVariable might fail for different positions based on the configurations

For example, validateEnhancedForLoopVariable means that the FinalLocalVariable check might fail for for (String item : items) { - will your recipe fix it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Any questions or blockers here?

@Anmol202005
Copy link
Collaborator Author

@timurt kindly review.

Added new tests.

@Anmol202005
Copy link
Collaborator Author

was thinking to make this PR small by raising PR to separate Logic for line:column calculation like: #47

But idk if @rdiachenko prefer this :(

Copy link
Collaborator

@timurt timurt left a comment

Choose a reason for hiding this comment

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

minor comments, but overall LGTM

{
final int[] squares = {0, 1, 4, 9, 16, 25};
int x;
for (final int i : squares) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You probably want to remove the final here, to test that it is being added by the recipe

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

{
final java.util.List<Object> list = new java.util.ArrayList<>();

for(final Object a : list){
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

Copy link
Member

@rdiachenko rdiachenko left a comment

Choose a reason for hiding this comment

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

lgtm

@rdiachenko
Copy link
Member

the recipe currently has partial coverage: When multiple variables are declared together like int a, b, c; and only one of them has a violation, the recipe won't process it.

@Anmol202005 is this still an issue? If yes, let's try to fix it in a new PR

@rdiachenko rdiachenko merged commit b2a2f2b into checkstyle:main Aug 5, 2025
3 checks passed
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

Successfully merging this pull request may close these issues.

Implement recipe for FinalLocalVariable

3 participants