Inspectopedia
 
2024.3

Unnecessary unary minus

Warning
New
Last modified: 03 December 2024

Reports unnecessary unary minuses. Such expressions might be hard to understand and might contain errors.

For example:

The following quick fixes are suggested here:

  • Remove - operators before the i variable:

    void unaryMinus(int i) {
        int x = i;
      }
  • Replace - operators with the prefix decrement operator:

    void unaryMinus(int i) {
        int x = --i;
      }

Another example:

After the quick-fix is applied: