rspec/rules/S2761/java/rule.adoc

58 lines
1.2 KiB
Plaintext
Raw Normal View History

== 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:
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:48:07 +02:00
----
int i = 1;
int j = - - -i; // Noncompliant: equivalent to "-i"
int k = ~~~i; // Noncompliant: equivalent to "~i"
int m = + +i; // Noncompliant: equivalent to "i"
2020-06-30 12:48:07 +02:00
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:
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:48:07 +02:00
----
int i = 1;
int j = ++ ++i; // Noncompliant
int k = i-- --; // Noncompliant
----
2020-06-30 12:48:07 +02:00
Using ``+=`` or ``-=`` improves readability:
2020-06-30 12:48:07 +02:00
[source,java]
2020-06-30 12:48:07 +02:00
----
int i = 1;
i += 2;
int j = i;
int k = i;
i -=2;
----
This rule raises an issue for repetitions of ``++!++``, ``++~++``, ``++-++``, ``{plus}``, prefix increments ``{plus}{plus}`` and prefix decrements ``--``.
2020-06-30 12:48:07 +02:00
=== Exceptions
2020-06-30 12:48:07 +02:00
2021-01-27 13:42:22 +01:00
Overflow handling for GWT compilation using ``++~~++`` is ignored.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]