rspec/rules/S1121/csharp/rule.adoc

100 lines
1.8 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:47:33 +02:00
include::../description.adoc[]
=== Exceptions
2020-06-30 12:47:33 +02:00
Assignments inside lambda and delegate expressions are allowed.
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
----
var result = Foo(() =>
2020-06-30 12:47:33 +02:00
{
int x = 100; // dead store, but ignored
x = 200;
return x;
2020-06-30 12:47:33 +02:00
}
----
The rule also ignores the following patterns:
2020-06-30 12:47:33 +02:00
* Chained assignments
[source,csharp]
2020-06-30 12:47:33 +02:00
----
var a = b = c = 10;
----
* Assignments that are part of a condition of an `if` statement or a loop
[source,csharp]
2020-06-30 12:47:33 +02:00
----
while ((val = GetNewValue()) > 0)
{
...
}
----
* Assignment in the right-hand side of a coalescing operator
[source,csharp]
2020-06-30 12:47:33 +02:00
----
private MyClass instance;
public MyClass Instance => instance ?? (instance = new MyClass());
----
== How to fix it
include::../how-to-fix-it.adoc[]
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
if (string.IsNullOrEmpty(result = str.Substring(index, length))) // Noncompliant
{
// do something with "result"
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
var result = str.Substring(index, length);
if (string.IsNullOrEmpty(result))
2020-06-30 12:47:33 +02:00
{
// do something with "result"
2020-06-30 12:47:33 +02:00
}
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
=== on 21 Jan 2016, 11:09:06 Dinesh Bolkensteyn wrote:
This rule doesn't cover the R# compiler warning (which is out of the box in Roslyn).
See \https://msdn.microsoft.com/en-us/library/c1sde1ax.aspx for details on how that one behaves.
As this more generalized rule is way more crappy, I'm disabling it by default for C#
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]