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

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

Open
IlanaRadinsky opened this issue Aug 11, 2022 · 0 comments · May be fixed by #2593

Comments

@IlanaRadinsky
Copy link

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.

mpritham pushed a commit that referenced this issue Jun 13, 2023
Refs #2361. Change the severity to `SUGGESTION`. Remove suggested fixes to prefix unused variables with underscores, but keep the suggested fix which renames used variables prefixed with underscores.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant