2020-12-21 15:38:52 +01:00
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.
2021-02-02 15:02:10 +01:00
2020-12-21 15:38:52 +01:00
This rule raises an issue when the results of the following methods are ignored:
* LINQ method,
2021-01-27 13:42:22 +01:00
* ``++[Pure]++`` method,
* any method on ``++string++``, ``++int++``, ..., ``++System.Collections.Immutable.ImmutableArray<T>++``, ``++ImmutableHashSet<T>++``, ...
2020-12-21 15:38:52 +01:00
Notes:
2021-01-27 13:42:22 +01:00
* 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.
2020-12-21 15:38:52 +01:00
* 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
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-12-21 15:38:52 +01:00
----
coll.Where(i => i > 5).Select(i => i*i); // Noncompliant
"this string".Equals("other string"); // Noncompliant
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-12-21 15:38:52 +01:00
----
var res = coll.Where(i => i > 5).Select(i => i*i);
var isEqual = "this string".Equals("other string");
----
== Exceptions
2021-01-27 13:42:22 +01:00
This rule doesn't report issues on method calls with ``++out++`` or ``++ref++`` arguments.
2020-12-21 15:38:52 +01:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]