rspec/rules/S2123/csharp/rule.adoc

76 lines
2.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2023-07-05 16:46:07 +02:00
When using the https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#postfix-increment-operator[postfix increment] operator, it is important to know that the result of the expression `x++` is the value **before** the operation `x`.
2020-06-30 12:48:07 +02:00
2023-07-05 16:46:07 +02:00
This means that in some cases, the result might not be what you expect:
2020-06-30 12:48:07 +02:00
2023-07-05 16:46:07 +02:00
* When assigning `x++` to `x`, it's the same as assigning `x` to itself, since the value is assigned before the increment takes place
* When returning `x++`, the returning value is `x`, not `x+1`
The same applies to the postfix and prefix https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#decrement-operator---[decrement] operators.
== How to fix it
To solve the issue in assignments, eliminate the assignment, since `x\++` mutates `x` anyways.
To solve the issue in return statements, consider using the https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#prefix-increment-operator[prefix increment] operator, since it works in reverse: the result of the expression `++x` is the value **after** the operation, which is `x+1`, as one might expect.
The same applies to the postfix and prefix https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#decrement-operator---[decrement] operators.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
2020-06-30 12:48:07 +02:00
----
2023-07-05 16:46:07 +02:00
int PickNumber()
2020-06-30 12:48:07 +02:00
{
int i = 0;
int j = 0;
2023-07-05 16:46:07 +02:00
i = i++; // Noncompliant: i is still 0
return j--; // Noncompliant: returns 0
2020-06-30 12:48:07 +02:00
}
----
2023-07-05 16:46:07 +02:00
==== Compliant solution
2020-06-30 12:48:07 +02:00
2023-07-05 16:46:07 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2020-06-30 12:48:07 +02:00
----
2023-07-05 16:46:07 +02:00
int PickNumber()
2020-06-30 12:48:07 +02:00
{
int i = 0;
int j = 0;
2023-07-05 16:46:07 +02:00
i++; // Compliant: i is incremented to 1
return --j; // Compliant: returns -1
2020-06-30 12:48:07 +02:00
}
----
2023-07-05 16:46:07 +02:00
== Resources
=== Documentation
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators[Arithmetic operators (C# reference)]
=== Articles & blog posts
* StackOverflow - https://stackoverflow.com/a/3346729["What is the difference between i++ and ++i in C#?" - Eric Lippert's answer]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]