gradle-baseline icon indicating copy to clipboard operation
gradle-baseline copied to clipboard

`StrictUnusedVariable` check should suggest removing variables assigned in constructor but never read

Open IlanaRadinsky opened this issue 2 years ago • 0 comments

What happened?

If you have a class like this

public class Test {
    private final String myField;

    public Test(String myField) {
        this.myField = myField;
    }
}

then the StrictUsedVariable check fails with

error: [StrictUnusedVariable] The field 'myField' is never read. Intentional occurrences are acknowledged by renaming the unused variable with a leading underscore. '_myField', for example.
    private final String myField;
                         ^

But if I apply the suggested fix

public class Test {
    private final String _myField;

    public Test(String myField) {
        this._myField = myField;
    }
}

I also get a StrictUnusedVariable error

error: [StrictUnusedVariable] The field '_myField' is read but has 'StrictUnusedVariable' suppressed because of its name.
    private final String _myField;
                         ^

What did you want to happen?

The check should suggest removing the variable and assignment entirely rather than allowing underscores.

IlanaRadinsky avatar Aug 11 '22 17:08 IlanaRadinsky