Code inspection: Return value of pure method is not used
Consider the following code snippet:
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, for example, 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 ReSharper 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 ReSharper 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.