简体   繁体   中英

IntelliJ IDEA warning “value is never used”

I wrote a method which calculates the maximum amount of permutations of a word, but IntelliJ IDEA is giving me the warning:

The value length - 1 assigned to 'permutationAmount' is never used

private static int permutationsPossible(String word) {
    //Amount of letters in word.
    int length = word.length();

    //Return length if length is less than or equal to 1.
    if (length <= 1)
        return length;

    //Calculate maximum amount of permutations.
    int permutationAmount = length;
    for (int i = 1; i < length - 1; i++)
        permutationAmount *= (length - i);
    return permutationAmount /= length - 1;
}

However, no warning is displayed when I make a helper int like so:

    permutationAmount = permutationAmount /= length - 1;
    return permutationAmount;

The program works fine both ways, all I want to know is, why does IntelliJ warn me that 'length - 1' is never used?

Screenshot: https://i.gyazo.com/886d7ab8b06398fb7dd0809a7d1bbaf3.png

You are assigning back to permutationAmount in the return statement for no reason (the cause of the warning).

return permutationAmount / (length - 1);

the tricky part may be that while it looks like you are returning the value of permutationAmount in the return statement (and would thus be reading the assigned value), you aren't. the return statement is actually returning the result of the assignment operation, and therefore permutationAmount is never actually read again after assigned.

It's because you're changing the value of permutationAmount for no reason. You are not using the new value again after the return, so there is no point of you actually changing the value of the variable in the return line. Instead of actually changing the value, you can accomplish the same thing by doing

return permutationAmount / length - 1;

In your second example you are using the new value after the change and if you didn't actually change the value, the return would be incorrect.

This is the text that appears when I expand the box in IntelliJ on this warning:

This inspection points out the cases where a variable value is never used after its assignment, ie:

  • the variable never gets read after assignment OR

  • the value is always overwritten with another assignment before the next variable read OR

  • the variable initializer is redundant (for one of the above tow reasons) OR

  • the variable is never used.

It appears that this warning is triggered because of the first reason: you never read the variable again after assigning it. That doesn't make much sense, because you immediately return the variable.

However, it doesn't make much sense to assign a value to a local variable and then immediately return it. This line would perform the same logic, without assigning to the variable:

return permutationAmount / (length - 1);

So it's not that the variable is never used, because we assume that the caller of this method will read the value returned by this method, but the assignment to the variable is useless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM