2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-10-05 09:58:05 +02:00
While working with bitwise operators ``++&++`` or ``++|++``, it is easy to make a typo and write the equivalent logical operators ``++&&++`` or ``++||++``. This rule raises an issue when the right operand of a logical expression ``++&&++`` or ``++||++`` is a constant of integral type, as the developer probably meant to use the corresponding bitwise operator ``++&++`` or ``++|++``.
2021-04-28 16:49:39 +02:00
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-10-05 09:58:05 +02:00
[source,cpp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
int fun(int a) {
2023-10-05 09:58:05 +02:00
return a || 4; // Noncompliant: did you mean to use bitwise operator '|'?
2021-04-28 16:49:39 +02:00
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2023-10-05 09:58:05 +02:00
[source,cpp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
int fun(int a) {
return a | 4;
}
----
2021-04-28 18:08:03 +02:00
2023-10-05 09:58:05 +02:00
== Resources
* {cpp} reference - https://en.cppreference.com/w/cpp/language/operator_arithmetic#Bitwise_logic_operators[Bitwise logic operators]
* {cpp} reference - https://en.cppreference.com/w/cpp/language/operator_logical[Logical operators]
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Review this logical (&& | ||) expression with constant operand.
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]