rspec/rules/S2437/vbnet/rule.adoc

49 lines
2.0 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2023-06-01 17:53:53 +02:00
Certain https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators[bitwise operations] are not needed and should not be performed because their results are predictable.
2021-01-21 04:09:13 +00:00
2023-06-07 15:41:34 +02:00
Specifically, using `And -1` with any value always results in the original value.
2023-06-01 17:53:53 +02:00
That is because the binary representation of `-1` on a https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types[numeric data type] supporting negative numbers, such as `Integer` or `Long`, is based on https://en.wikipedia.org/wiki/Two%27s_complement[two's complement] and made of all 1s: `&B111...111`.
Performing `And` between a value and `&B111...111` means applying the `And` operator to each bit of the value and the bit `1`, resulting in a value equal to the provided one, bit by bit.
[source,vbnet]
----
anyValue And -1 ' Noncompliant
anyValue ' Compliant
----
Similarly, `anyValue Or 0` always results in `anyValue`, because the binary representation of `0` is always `&B000...000` and the `Or` operator returns its first input when the second is `0`.
[source,vbnet]
----
anyValue Or 0 ' Noncompliant
anyValue ' Compliant
----
The same applies to `anyValue Xor 0`: the `Xor` operator returns `1` when its two input bits are different (`1` and `0` or `0` and `1`) and returns `0` when its two input bits are the same (both `0` or both `1`).
When `Xor` is applied with `0`, the result would be `1` if the other input is `1`, because the two input bits are different, and `0` if the other input bit is `0`, because the two input are the same. That results in returning `anyValue`.
[source,vbnet]
----
anyValue Xor 0 ' Noncompliant
anyValue ' Compliant
----
include::../resources-dotnet.adoc[]
2021-02-02 15:02:10 +01:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2023-06-01 17:53:53 +02:00
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]