42 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

== 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 ``++|++``.
2021-04-28 16:49:39 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
[source,cpp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
int fun(int a) {
return a || 4; // Noncompliant: did you mean to use bitwise operator '|'?
2021-04-28 16:49:39 +02:00
}
----
=== Compliant solution
2021-04-28 16:49:39 +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;
}
----
== 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[]