checkstyle / checkstyle/checkstyle

Inconsistencies in Final Local Variable Checkstyle - Incorrect Violation Count and Missing Test Method Specifications

Open
#14,018 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
9.6k
Forks
4.2k
Avg merge
22h 23m
Merged PRs (30d)
232

Description

The class `FinalLocalVariableCheckTest` is having inconsistencies of the following manner -

### 1. Test methods does not specifies the expected[] array
```
@Test
public void testInputFinalLocalVariable2Three() throws Exception {

final String[] excepted = {

};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariable2Three.java"), excepted);
}

@Test
public void testInputFinalLocalVariable2Four() throws Exception {

final String[] excepted = {

};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariable2Four.java"), excepted);
}

@Test
public void testInputFinalLocalVariable2Five() throws Exception {

final String[] excepted = {

};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariable2Five.java"), excepted);
}

```

### 2. `// violation` comments are not marked correctly for input files

Input files, for example - https://github.com/checkstyle/checkstyle/blob/ccbfd5f7f68017e214da00fcba6cc6f268e3fa83/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/finallocalvariable/InputFinalLocalVariable2Five.java have more violations than it is marked in the file.

Here's the CLI to verify it -

`/var/tmp $ cat config.xml`
```



```

`/var/tmp $ cat Test.java`
```
/*
FinalLocalVariable
validateEnhancedForLoopVariable = (default)false
tokens = PARAMETER_DEF

*/

package com.puppycrawl.tools.checkstyle.checks.coding.finallocalvariable;

public class InputFinalLocalVariable2Five {
class class42 {
public void foo() {
int shouldBeFinal;
class Bar {
void bar () {
int shouldBeFinal; //Violation
final boolean b = false;
if (b) {
shouldBeFinal = 1;
} else {
shouldBeFinal = 2;
}
}
}
}
}

class class52 {
public void test1(){
final boolean b = false;
int shouldBeFinal; //Violation
if(b){
if(b){
shouldBeFinal = 1;
} else {
shouldBeFinal = 2;
}
}
}
public void test2() {
final int b = 10;
int shouldBeFinal; //Violation

switch (b) {
case 0:
switch (b) {
case 0:
shouldBeFinal = 1;
break;
default:
shouldBeFinal = 2;
break;
}
break;
default:
shouldBeFinal = 3;
break;
}
}
public void test3() {
int x; //No Violation
try {
x = 0;
try {
x = 0;
} catch (final Exception e) {
x = 1;
}
} catch (final Exception e) {
x = 1;
}
}
public void test4() {
int shouldBeFinal;
class Bar {
void bar () {
int shouldBeFinal; //Violation
final boolean b = false;
if (b) {
if (b) {
shouldBeFinal = 1;
} else {
shouldBeFinal = 2;
}
} else {
shouldBeFinal = 2;
}
}
}

abstract class Bar2 {
abstract void method(String param);
}
}

public void test5() {
InputFinalLocalVariableFive table
= new InputFinalLocalVariableFive();
new Runnable() {
@Override
public void run() {
InputFinalLocalVariableFive table = null;
table = new InputFinalLocalVariableFive();
}
};
}
}

}
```

Validation - `$ java -jar checkstyle-10.12.4-all.jar -c config.xml Test.java`
```
Starting audit...
[ERROR] /var/tmp/Test.java:14:17: Variable 'shouldBeFinal' should be declared final. [FinalLocalVariable]
[ERROR] /var/tmp/Test.java:17:25: Variable 'shouldBeFinal' should be declared final. [FinalLocalVariable]
[ERROR] /var/tmp/Test.java:32:17: Variable 'shouldBeFinal' should be declared final. [FinalLocalVariable]
[ERROR] /var/tmp/Test.java:43:17: Variable 'shouldBeFinal' should be declared final. [FinalLocalVariable]
[ERROR] /var/tmp/Test.java:75:17: Variable 'shouldBeFinal' should be declared final. [FinalLocalVariable]
[ERROR] /var/tmp/Test.java:78:25: Variable 'shouldBeFinal' should be declared final. [FinalLocalVariable]
[ERROR] /var/tmp/Test.java:98:41: Variable 'table' should be declared final. [FinalLocalVariable]
Audit done.
Checkstyle ends with 7 errors.
```

As we can see here checkstyle reported 7 errors but input file has marked `// violation` at 4 places, ideally it should be 7.

Contributor guide

Open the contributing guide

Research direction

Start with FinalLocalVariableCheckTest and the referenced InputFinalLocalVariable2Three.java, InputFinalLocalVariable2Four.java, and InputFinalLocalVariable2Five.java resources. Compare the expected arrays and // violation markers with the seven diagnostics produced by the supplied Checkstyle CLI configuration. Done means the test method specifications and input-file markers consistently represent the reported violations.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.