rspec/rules/S2589/csharp/rule.adoc

76 lines
1.2 KiB
Plaintext
Raw Normal View History

2020-06-30 12:48:07 +02:00
include::../description.adoc[]
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
public void Sample(bool b, bool c, string s)
{
var a = true;
if (a) // Noncompliant
{
DoSomething();
}
if (b && a) // Noncompliant; "a" is always "true"
{
DoSomething();
}
if (c || !a) // Noncompliant; "!a" is always "false"
{
DoSomething();
}
string d = null;
var v1 = d ?? "value"; // Noncompliant, d is always null
var v2 = s ?? d; // Noncompliant, d is always null and the produced value is always equal to s. The condition to check the value of s is gratuitous.
}
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
public void Sample(bool b, bool c, string s)
{
var a = true;
if (Foo(a))
{
DoSomething();
}
if (b)
{
DoSomething();
}
if (c)
{
DoSomething();
}
var v1 = "value";
var v2 = s;
}
----
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)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]