Inspectopedia Help

Unused assignment

Reports assignment values that are not used after assignment. If the assignment value is unused, it's better to remove the assignment to shorten the code and avoid redundant allocations.

The following cases are reported:

  • variables that are never read after assignment

  • variables that are always overwritten with a new value before being read

  • variable initializers that are redundant (for one of the two reasons above)

Configure the inspection:

Use the Report redundant initializers option to report redundant initializers:

int getI() { int i = 0; // redundant initialization i = 2; return i; }

Use the Report prefix expressions that can be replaced with binary expressions option to report cases where an ++i expression may be replaced with i + 1:

int preInc(int value) { int res = value; return ++res; }

Use the Report postfix expressions where the changed value is not used option to report i++ cases where the value of i is not used later:

int postInc(int value) { int res = value; return res++; }

Use the Report pattern variables whose values are never used option to report cases where the value of a pattern variable is overwritten before it is read:

if (object instanceof String s) { s = "hello"; System.out.println(s); }

Use the Report iteration parameters whose values are never used option to report cases where the value of an iteration parameter of an enhanced for statements is overwritten before it is read:

for (String arg : args) { arg = "test"; System.out.println(arg); }

Inspection options

Here you can find the description of settings available for the Unused assignment inspection, and the reference of their default values.

Report redundant initializers

Default: Selected

Report prefix expressions that can be replaced with a binary expression

Not selected

Report postfix expressions where the changed value is not used

Default: Selected

Report pattern variables whose values are never used

Default: Selected

Report iteration parameters whose values are never used

Default: Selected

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin:

Java, 241.16690

Last modified: 29 April 2024