rspec/rules/S1854/csharp/rule.adoc
2023-10-13 12:26:37 +02:00

65 lines
1.4 KiB
Plaintext

include::../why.adoc[]
=== Exceptions
No issue is reported when
* the analyzed method body contains `try` blocks
* a lambda expression captures the local variable
* the variable is unused (case covered by Rule S1481)
* it's an initialization to `-1`, `0`, `1`, `null`, `true`, `false`, `""` or `string.Empty`
include::../howtofixit.adoc[]
You can also use https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/discards[discards] (rather than a variable) to express that result of a method call is ignored on purpose.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
int Foo(int y)
{
int x = 100; // Noncompliant: dead store
x = 150; // Noncompliant: dead store
x = 200;
return x + y;
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
int Foo(int y)
{
int x = 200; // Compliant: no unnecessary assignment
return x + y;
}
----
include::../see.adoc[]
=== Related rules
* S2583 - Conditionally executed code should be reachable
* S2589 - Boolean expressions should not be gratuitous
* S3626 - Jump statements should not be redundant
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[]