JetBrains Rider 2024.1 Help

Code Inspection: Return value of pure method is not used

Category

Potential Code Quality Issues

ID

ReturnValueOfPureMethodIsNotUsed

EditorConfig

resharper_return_value_of_pure_method_is_not_used_highlighting

Default severity

Warning

Language

C#, VB.NET

Requires SWA

No

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, 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 JetBrains 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 JetBrains 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.

Last modified: 10 April 2024