rspec/rules/S2583/csharp/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

99 lines
1.7 KiB
Plaintext

== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
[source,csharp]
----
public void Sample(bool b)
{
bool a = false;
if (a) // Noncompliant
{
DoSomething(); // never executed
}
if (!a || b) // Noncompliant; "!a" is always "true", "b" is never evaluated
{
DoSomething();
}
else
{
DoSomethingElse(); // never executed
}
var d = "xxx";
var res = d ?? "value"; // Noncompliant, d is always not null, "value" is never used
}
----
=== Compliant solution
[source,csharp]
----
public void Sample(bool b)
{
bool a = false;
if (Foo(a))
{
DoSomething();
}
if (b)
{
DoSomething();
}
else
{
DoSomethingElse();
}
var d = "xxx";
var res = d;
}
----
=== Exceptions
This rule will not raise an issue in either of these cases:
* When the condition is a single ``++const bool++``
[source,csharp]
----
const bool debug = false;
//...
if (debug)
{
// Print something
}
----
* When the condition is the literal ``++true++`` or ``++false++``.
In these cases it is obvious the code is as intended.
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Change this condition so that it does not always evaluate to "[true|false]"; some subsequent code is never executed.
* Change this expression which always evaluates to "not null"; some subsequent code is never executed.
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]