Code Inspection: Return value of pure method is not used
Consider the following code snippet:
name.Replace(", ", " ");
While it may appear that the code is doing something, it really is not! The reason is that, since strings are immutable,
string.Replace does not replace anything in the original string (unlike, e.g., a
StringBuilder). All you’ve done is wasted a few CPU cycles, since the result of the pure function call did not get assigned to anything.
You may also wonder how Rider knows that
string.Replace is pure. Well, the trick is done with external annotations for the.NET Framework Class Library and other frequently used libraries. These annotations are included in the Rider installation. The [Pure] attribute that triggers this inspection can also be used for your custom pure methods, or you can use the mechanism of external annotations to annotate pure methods in compiled libraries that you use.