rspec/rules/S2201/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

64 lines
1.7 KiB
Plaintext

== Why is this an issue?
When the call to a function doesn't have any side effects, what is the point of making the call if the results are ignored? In such case, either the function call is useless and should be dropped or the source code doesn't behave as expected.
This rule raises an issue when the results of the following methods are ignored:
* LINQ method,
* ``++[Pure]++`` method,
* any method on ``++string++``, ``++int++``, ..., ``++System.Collections.Immutable.ImmutableArray<T>++``, ``++ImmutableHashSet<T>++``, ...
Notes:
* although ``++string.Intern++`` has a side effect, ignoring its return value is still suspicious as it is the only reference ensured to point to the intern pool.
* Link methods can have side effects if they are misused. Example:
----
tests.All(c => { c.myfield = "foo"; return true; });
----
Such code should be rewritten as a normal loop.
=== Noncompliant code example
[source,csharp]
----
coll.Where(i => i > 5).Select(i => i*i); // Noncompliant
"this string".Equals("other string"); // Noncompliant
----
=== Compliant solution
[source,csharp]
----
var res = coll.Where(i => i > 5).Select(i => i*i);
var isEqual = "this string".Equals("other string");
----
=== Exceptions
This rule doesn't report issues on method calls with ``++out++`` or ``++ref++`` arguments.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use the return value of method "[method name]".
'''
== Comments And Links
(visible only on this page)
=== on 14 Mar 2016, 09:53:02 Dinesh Bolkensteyn wrote:
perhaps we can add some methods on the ``++String++`` class such as ``++ToUpper++``, ``++Substring++``
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]