Showing warning when function result is not assigned to variable

You could use an out parameter, so the call would look like this:

obj.Foo(param, out obj);

You can use Resharper to assist with this issue; you need to decorate your method with the [Pure] attribute:

[Pure]
public static IList<T> RemoveItem<T>(this IEnumerable<T> thisList, T item)
{
    var list = thisList.ToList();
    list.Remove(item);
    return list;
}

then when you call it without assigning the return value you will see:

enter image description here

The [Pure] attribute is defined in Resharpers Data Annotations: You need to copy the classes into your project so you can reference them (many very useful other annotations too)

enter image description here


It's totally legal and often desirable to not assign the return parameter so it would be wrong to have a warning for it. Henrik's answer to use an out parameter is what I'd recommend too to ensure the result is assigned everytime.

Tags:

C#

Function