42 lines
1.1 KiB
Plaintext
42 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
||
|
||
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 ``++|++``.
|
||
|
||
|
||
=== Noncompliant code example
|
||
|
||
[source,cpp,diff-id=1,diff-type=noncompliant]
|
||
----
|
||
int fun(int a) {
|
||
return a || 4; // Noncompliant: did you mean to use bitwise operator '|'?
|
||
}
|
||
----
|
||
|
||
|
||
=== Compliant solution
|
||
|
||
[source,cpp,diff-id=1,diff-type=compliant]
|
||
----
|
||
int fun(int a) {
|
||
return a | 4;
|
||
}
|
||
----
|
||
|
||
== 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]
|
||
|
||
ifdef::env-github,rspecator-view[]
|
||
|
||
'''
|
||
== Implementation Specification
|
||
(visible only on this page)
|
||
|
||
=== Message
|
||
|
||
Review this logical (&& | ||) expression with constant operand.
|
||
|
||
|
||
endif::env-github,rspecator-view[]
|