'Variable should be declared final' reported for variables which cannot be declared final. #258

Closed
ChristianSchulte opened this Issue Aug 21, 2014 · 7 comments

Comments

Projects
None yet
4 participants
@ChristianSchulte

/tmp/checkstyle.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
  <module name="TreeWalker">
    <module name="FinalLocalVariable">
      <property name="tokens" value="VARIABLE_DEF,PARAMETER_DEF"/>
    </module>
  </module>
</module>

/tmp/Test.java

public class Test
{

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

        for ( int i = 0, s0 = list.size(); i < s0; i++ )
        {
        }
    }

}

Invocation

java -Duser.language=en -cp checkstyle-5.7-all.jar com.puppycrawl.tools.checkstyle.Main -c /tmp/checkstyle.xml /tmp/Test.java

Report

Starting audit...
/tmp/Test.java:12:26: Variable 's0' should be declared final.
Audit done.

There is no way to declare variable 's0' final other than moving it out of the 'for' loop scope.

@romani romani added the approved label Aug 21, 2014

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Aug 21, 2014

Member

thanks a lot ! for detailed report.

Member

romani commented Aug 21, 2014

thanks a lot ! for detailed report.

@Bhavik3

This comment has been minimized.

Show comment
Hide comment
@Bhavik3

Bhavik3 Feb 18, 2015

Contributor

for (int i = 0, s0 = list.size(); i < s0;s0++ )
{
}

Here, According to FinalLocalVariableCheck, variable 'i' should be declared final.

but as described above, no way of declaring it final.
@alexkravin

Contributor

Bhavik3 commented Feb 18, 2015

for (int i = 0, s0 = list.size(); i < s0;s0++ )
{
}

Here, According to FinalLocalVariableCheck, variable 'i' should be declared final.

but as described above, no way of declaring it final.
@alexkravin

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Feb 18, 2015

Member

@Bhavik3 , in current fix commit, we did all correctly, in your issue you have different type if FOR loop.

Member

romani commented Feb 18, 2015

@Bhavik3 , in current fix commit, we did all correctly, in your issue you have different type if FOR loop.

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Feb 18, 2015

Member

Current issues is resolved and released in 6.3

Member

romani commented Feb 18, 2015

Current issues is resolved and released in 6.3

@romani romani closed this Feb 18, 2015

@Bhavik3

This comment has been minimized.

Show comment
Hide comment
@Bhavik3

Bhavik3 Feb 18, 2015

Contributor

according to this fix,
If variable is first in initialization than only checkstyle will report it. like,

for ( int i = 0, s0 = list.size(); i < s0; i++ ){
}

here, for above for-loop, value of s0 is not changing in entire loop, but checkstyle will not report it, because of its being second in for-loop initialization.

but what if variable is first in for-loop initialization and can not be declared final but, reported by checkstyle. like,

for (int i = 0, s0 = list.size(); i < s0;s0++ ){
}

here, "i" is not changing in entire for-loop. but can not be declared final. because of "s0" variable.
but checkstyle will report it according to this fix.

this issue is not same as solved before but related. so I observed and posted here.

@romani

Contributor

Bhavik3 commented Feb 18, 2015

according to this fix,
If variable is first in initialization than only checkstyle will report it. like,

for ( int i = 0, s0 = list.size(); i < s0; i++ ){
}

here, for above for-loop, value of s0 is not changing in entire loop, but checkstyle will not report it, because of its being second in for-loop initialization.

but what if variable is first in for-loop initialization and can not be declared final but, reported by checkstyle. like,

for (int i = 0, s0 = list.size(); i < s0;s0++ ){
}

here, "i" is not changing in entire for-loop. but can not be declared final. because of "s0" variable.
but checkstyle will report it according to this fix.

this issue is not same as solved before but related. so I observed and posted here.

@romani

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Feb 18, 2015

Member

reproduced:

public class Test
{

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

        for ( int i = 0, s0 = list.size(); i < s0; s0++ )
        {
        }
    }

}

CLI launch:

12:26 $ java -Duser.language=en -cp checkstyle-6.4-SNAPSHOT-all.jar com.puppycrawl.tools.checkstyle.Main -c checkstyle.xml Test.java
Starting audit...
/home/rivanov/Temp/Test.java:8:19: Variable 'i' should be declared final.
Audit done.
Member

romani commented Feb 18, 2015

reproduced:

public class Test
{

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

        for ( int i = 0, s0 = list.size(); i < s0; s0++ )
        {
        }
    }

}

CLI launch:

12:26 $ java -Duser.language=en -cp checkstyle-6.4-SNAPSHOT-all.jar com.puppycrawl.tools.checkstyle.Main -c checkstyle.xml Test.java
Starting audit...
/home/rivanov/Temp/Test.java:8:19: Variable 'i' should be declared final.
Audit done.
@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Feb 19, 2015

Member

false-positive is fixed, will be released in 6.4

Member

romani commented Feb 19, 2015

false-positive is fixed, will be released in 6.4

@romani romani closed this Feb 19, 2015

kzr-pzr added a commit to kzr-pzr/checkstyle that referenced this issue Jul 14, 2017

Swap links to Google Style Guide issues
Issue #259 is about line wrapping at ellipsis, issue #258 is about line wrapping at array declarator brackets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment