MFTV0002: Use a value with the tag the target expects

edit

Cause

A value is assigned, passed, or returned where a value with a different tag is expected:

_ = repository.Find(project.Id); // MFTV0002, a ProjectId is passed where an OrderId is expected
order.ProjectId = order.Id;      // MFTV0002, an OrderId is assigned to a ProjectId

The rule covers every flow of a value:

  • assignments, deconstructions, object initializers, with expressions, field and property initializers,
  • arguments, including ref, out, each argument of params, and generic arguments that must share a type,
  • return and yield return,
  • the elements added to a tagged collection, and the values matched by a pattern or read by a foreach into a tagged local.

An out argument flows from the parameter to the variable. The message names the value, the target, and where the target is declared:

property 'Project.Id' is [ValueTag("ProjectId")] and flows to parameter 'orderId' of 'OrderRepository.Find' (Model.cs:17), which is [ValueTag("OrderId")]

How to fix

Use a value with the expected tag:

_ = repository.Find(order.Id);
order.ProjectId = project.Id;

When the value is right and the target is the one tagged incorrectly, the code fix changes the tag of the target. For instance, it offers to change the parameter of Find to [ValueTag("ProjectId")]. On a local variable tagged by a comment, it changes the comment. It does not change a tag inherited from a base member or declared by an [assembly: ValueTag] attribute.