Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify rule S2198: LaYC format #2156

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rules/S2198/csharp/metadata.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{

"quickfix": "targeted"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the quickfix in your opinion?
Is it just removing the comparison?
I am afraid that might change the behaviour of the code. Imagine for example:

If (f > double.Maxvalue)
{
Console.WriteLine("I am never printed");
}

If you remove the check, the code will actually execute. Would you in this case remove also the body of the if as it was unreachable code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix would either remove the entire if block, or just the if condition.
For example, if the code is if (f > double.MaxValue) Body the entire if would be removed.
If the code is if (f <= double.MaxValue) Body the if would be replaced with Body.
The same would apply to conditions like f < double.MinValue and f >= double.MinValue.

Indeed, there is actually the risk of changing the behavior of the code, for example in the following example:

if (SomeMethod() > int.MaxValue) Console.WriteLine(""Never being executed");

int SomeMethod() => throw new Exception();

If we would apply the code fix here, the entire if block would be removed, and the code would not raise an exception anymore. However, we can limit the code fix to fields only, if we want to avoid this corner case.

Anyway, even if there are subtleties to be taken care of, I would keep the targeted, since there is a realm where in principle the code fix is possible. So I would keep the door open for now, and defer the deeper analysis to a possible future task to specify the code fix behavior.

}
34 changes: 11 additions & 23 deletions rules/S2198/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
== Why is this an issue?

Certain mathematical comparisons will always return the same value, and should simply not be made.
Certain https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators[mathematical comparisons] will always return the same value, and should not be performed.

These comparisons will return either always `true` or always `false` depending on the kind of comparison:
Specifically, the following 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`.
* 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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be worth it linking the msdn documentation to the types? Like
https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/ulong-data-type for ulong?
This would enable users to find the ranges more quickly.

Copy link
Contributor Author

@antonioaversa antonioaversa Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added two links:

  • to the paragraph on the page about integral numeric types, that shows ranges for all numeric types
  • to the page for the char data type, that shows the range for the type

I have also added etc. since the list provided here is not comprehensive.

* etc.

=== Noncompliant code example

[source,csharp]
----
float f = 42.0f;
if (f <= double.MaxValue) { } // Noncompliant
if (f <= double.MaxValue) { } // Noncompliant: always true
if (f > double.MaxValue) { } // Noncompliant: always false
----

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

== Comments And Links
(visible only on this page)

=== 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.

endif::env-github,rspecator-view[]
include::../resources.adoc[]
include::../rspecator.adoc[]
7 changes: 7 additions & 0 deletions rules/S2198/resources.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
== Resources

=== Documentation

* Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators[Comparison operators (C# reference)]
* Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#characteristics-of-the-integral-types[Ranges for integral numeric types (C# reference)]
* Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/char[Range for char (C# reference)]
16 changes: 16 additions & 0 deletions rules/S2198/rspecator.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::message.adoc[]

== Comments And Links
(visible only on this page)

=== 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.

endif::env-github,rspecator-view[]
33 changes: 0 additions & 33 deletions rules/S2198/rule.adoc

This file was deleted.