FxC2201 : DoNotIgnoreResultOfImmutableMethods

TypeName DoNotIgnoreResultOfImmutableMethods
CheckId FxC2201
Category FxCopContrib.Usage
Breaking Change Non-breaking

Cause

A call to a method on an Immutable Type is being ignored.

Rule Description

When a method on an Immutable Type is called, which in itself also returns an Immutable type, it usually was the intention to overwrite the original value or another local variable.

How to fix violations

To fix this violation save the result of the method to a field or variable or use the result as a parameter for another method.

When to suppress warnings

If the method you're calling has a side-effect and you can safely ignore the return value, you can ignore this warning. Otherwise either remove the method call, or use the return value.

Example

You're trying to add 1 hour to an existing DateTime value

Bad Example
public void BadMethod()
{
     DateTime value = DateTime.UtcNow.Date;
     value.AddHours(5);
     value.SpecifyKind(DateTimeKind.Local);
}

Good Example
public void GoodMethod()
{
     DateTime value = DateTime.UtcNow.Date;
     value = value.AddHours(5);
     value = value.SpecifyKind(DateTimeKind.Local);
}

Remark

This rule uses a static list of ImmutableTypes. If you want to add your own ImmutableTypes, you can do so by creating a file named ImmutableTypes.txt in the folder which contains FxCop.exe or in the folder that contains your FxCop project file.

Last edited Dec 1, 2011 at 9:35 PM by jessehouwing, version 2

Comments

No comments yet.