From 972f4dc5fbe2035a3a97d2cdc5bdeee53fc4c4f1 Mon Sep 17 00:00:00 2001 From: Gregory Paidis <115458417+gregory-paidis-sonarsource@users.noreply.github.com> Date: Wed, 15 Feb 2023 15:00:19 +0100 Subject: [PATCH] S2198: Add C# (#1534) * Change description and add some comments * Update the description of what the C# implementation actually does for now * Fix a typo * Small formatting change on S2198 * Small formatting changes on S2198 * Small formatting change on S2198 --- rules/S2198/csharp/comments-and-links.adoc | 3 +++ rules/S2198/csharp/rule.adoc | 24 ++++++++++++++++++++-- rules/S2198/rule.adoc | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 rules/S2198/csharp/comments-and-links.adoc diff --git a/rules/S2198/csharp/comments-and-links.adoc b/rules/S2198/csharp/comments-and-links.adoc new file mode 100644 index 00000000000..fd842728f7a --- /dev/null +++ b/rules/S2198/csharp/comments-and-links.adoc @@ -0,0 +1,3 @@ +=== on 31 Jan 2023, 15:54:54 Grigorios Paidis wrote: + +The rule is complementary to https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0652?f1url=%3FappId%3Droslyn%26k%3Dk(CS0652)[CS0652]. Thus, it only extends the functionality of this Compiler Warning. \ No newline at end of file diff --git a/rules/S2198/csharp/rule.adoc b/rules/S2198/csharp/rule.adoc index 79d6542388c..e7708f030e5 100644 --- a/rules/S2198/csharp/rule.adoc +++ b/rules/S2198/csharp/rule.adoc @@ -1,4 +1,20 @@ -include::../rule.adoc[] +Certain mathematical comparisons will always return the same value, and should simply not be made. + +These comparisons will return either always `true` or always `false` depending on the kind of comparison: + +* Comparing a `char` with a numeric constant that is outside of the range of `char`. +* Comparing a `float` with a numeric constant that is outside of the range of `float`. +* Comparing a `long` with a numeric constant that is outside of the range of `long`. +* Comparing a `ulong` with a numeric constant that is outside of the range of `ulong`. + +== Noncompliant Code Example + +[source,text] +---- +float f = 42.0f; +if (f <= double.MaxValue) { } // Noncompliant +---- + ifdef::env-github,rspecator-view[] ''' @@ -7,4 +23,8 @@ ifdef::env-github,rspecator-view[] include::../message.adoc[] -endif::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] \ No newline at end of file diff --git a/rules/S2198/rule.adoc b/rules/S2198/rule.adoc index f96a2f620b0..33f65d75fb5 100644 --- a/rules/S2198/rule.adoc +++ b/rules/S2198/rule.adoc @@ -13,7 +13,7 @@ These will always return ``++true++``: * comparing ``++aByte <= Byte.MAX_VALUE++`` and ``++aByte >= Byte.MIN_VALUE++`` * comparing ``++anInt <= Integer.MAX_VALUE++`` and ``++anInt >= Integer.MIN_VALUE++`` -* comparing ``++aLong <= Long.MAX_VALUE++`` and ``++aLong >= Long.MIN_VALLUE++`` +* comparing ``++aLong <= Long.MAX_VALUE++`` and ``++aLong >= Long.MIN_VALUE++`` == Noncompliant Code Example