rspec/rules/S1940/csharp/rule.adoc

52 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:47:33 +02:00
include::../description.adoc[]
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
if ( !(a == 2)) { ...} // Noncompliant
bool b = !(i < 10); // Noncompliant
----
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
if (a != 2) { ...}
bool b = (i >= 10);
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
=== on 30 Sep 2019, 23:05:18 Sergey Adamovski wrote:
\"``++!(a>2)++``" is not always equivalent to \"``++a<=2++``".
In case a == NaN (not a number), any compare to it will give "false". ``++(a>2) == false++`` and ``++(a<=2) == false++``. Therefore, "!(a>2)" will be ``++true++`` while "a+<=+2" ``++false++``.
Equivalent of \"``++!(a>2)++``" would be \"``++(a<=2) || (double.IsNaN(a))++``" or \"``++(a<=2) || (float.IsNaN(a))++``" which is easier to read but longer to execute.
One can
* check whether comparison contains any float/double values that can be Not-A-Number _or_
* at least warn a user that it is not the same _or_
* remove this restriction
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]