All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S2761.html Maven / Gradle / Ivy

Why is this an issue?

The repetition of a unary operator is usually a typo. The second operator invalidates the first one in most cases:

int i = 1;

int j = - - -i;  // Noncompliant: equivalent to "-i"
int k = ~~~i;    // Noncompliant: equivalent to "~i"
int m = + +i;    // Noncompliant: equivalent to "i"

boolean b = false;
boolean c = !!!b;   // Noncompliant

On the other hand, while repeating the increment and decrement operators is technically correct, it obfuscates the meaning:

int i = 1;
int j = ++ ++i;  // Noncompliant
int k = i-- --; // Noncompliant

Using += or -= improves readability:

int i = 1;
i += 2;
int j = i;
int k = i;
i -=2;

This rule raises an issue for repetitions of !, ~, -, +, prefix increments ++ and prefix decrements --.

Exceptions

Overflow handling for GWT compilation using ~~ is ignored.





© 2015 - 2025 Weber Informatics LLC | Privacy Policy