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

[java] LocalVariableCouldBeFinal on 'size' variable in for loop #1619

Open
RocketRider opened this issue Jan 25, 2019 · 5 comments · May be fixed by #5003
Open

[java] LocalVariableCouldBeFinal on 'size' variable in for loop #1619

RocketRider opened this issue Jan 25, 2019 · 5 comments · May be fixed by #5003
Assignees
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@RocketRider
Copy link

RocketRider commented Jan 25, 2019

Affects PMD Version: 7.0.0

Rule: LocalVariableCouldBeFinal

Description:
The following line causes a LocalVariableCouldBeFinal warning, but this can't be fixed. As "size" can't be declared final.

for (int i = 0, size = loaders.size(); i < size; ++i) {
@jsotuyod
Copy link
Member

@RocketRider thanks for the report, and sorry for the delay. I'm catching up on some old PMD emails… why can't you move the declaration outside the for and declare it final?

@KristofSzabados
Copy link

I agree with the proposal.

Technically one could extract the declaration outside the for loop, but
1)
It gets pretty ugly really soon.

final int size_1 = loader.size();
for (.....

final int size_2 = other_list.size();
for (....

final int size_3 = outler_list.size();
final int size_4 = inner_list.size();
for (..) {
for (...)
{...
}}

extracting the declaration outside of the for loop, increases the scope of the declaration ... inviting missuse (and as seen above name clashes).

@adangel
Copy link
Member

adangel commented Sep 2, 2020

Do you need a separate size variable at all? What about this?

for (int i = 0; i < loaders.size(); ++i) {

@RocketRider
Copy link
Author

The idea is to keep the scope of the size variable as small as possible. It does not need to be a separate variable but it is nice if the variable is not visible outside of the loop.

The Issue with the last examle is, that it will call "size()" on every iteration.

@oowekyala oowekyala added the a:false-positive PMD flags a piece of code that is not problematic label Apr 26, 2021
@jsotuyod jsotuyod added the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Mar 17, 2024
@adangel adangel removed the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Mar 21, 2024
@adangel
Copy link
Member

adangel commented Mar 21, 2024

The issue is still reproducible under PMD 7.0.0.

@oowekyala oowekyala self-assigned this Apr 23, 2024
@oowekyala oowekyala added this to the 7.2.0 milestone May 11, 2024
oowekyala added a commit to oowekyala/pmd that referenced this issue May 11, 2024
@oowekyala oowekyala linked a pull request May 11, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants