Overview

edit

The analyzer of Meziantou.Framework.TaggedValues reports its diagnostics with the MFTV prefix. Every message names both declarations and their tags, so a build log is enough to act on.

Rules

RuleReportsSeverityCode fix
MFTV0001Values with different tags are comparedWarning—
MFTV0002A value flows to a declaration that expects another tagWarningChange the tag of the target
MFTV0003Values with different tags are combined into one valueWarning—
MFTV0004An override or an implementation has other tags than its base memberWarningChange the tag of the override
MFTV0005A [ValueTag] attribute or a ValueTag comment is invalidWarningRemove the annotation
MFTV0006Two Id members get the same tag from the naming conventionWarning—
MFTV0007A [ValueTag] attribute repeats the tag of the naming conventionInfoRemove the attribute
MFTV0008A method returns tagged values, but its return value is not taggedInfoAdd the tag
MFTV0009In strict mode, a tagged value is mixed with an untagged valueWarningAdd the tag

MFTV0006 and MFTV0007 are only reported when the naming conventions are enabled, and MFTV0009 only in strict mode.

The examples of these pages

The examples of the rule pages use these types:

public sealed class Order
{
    [ValueTag("OrderId")] public Guid Id { get; set; }
    [ValueTag("ProjectId")] public Guid ProjectId { get; set; }
}

public sealed class Project
{
    [ValueTag("ProjectId")] public Guid Id { get; set; }
}

public sealed class OrderRepository
{
    public Order? Find([ValueTag("OrderId")] Guid orderId) => null;
}

Code fixes

The code fixes change the tag of the target of a flow or of an override, add the suggested tag to a return value or to an untagged declaration, and remove invalid or redundant annotations.

  • A code fix that changes a tag changes every part of a partial member.
  • The code fix does not change a tag inherited from a base member, or declared by an [assembly: ValueTag] attribute: fix the base member or the assembly attribute yourself.
  • On a local variable, the code fix changes the ValueTag comment. When the comment also tags other variables of the declaration, it adds a comment next to the name of the variable instead.
  • The code fixes that change or add a tag support Fix All, in a document, a project, or a solution. When several diagnostics target the same declaration, or the same local variable, Fix All writes the tag once. When they suggest different tags, Fix All leaves the declaration unchanged: fix these diagnostics one by one.

Configure the analyzer

The analyzer reads two options from the .editorconfig file:

OptionDefaultEffect
taggedvalues.infer_tags_from_namesfalseInfers tags from the names of Id, xxxId and id declarations. See Naming conventions.
taggedvalues.strictfalseAlso reports tagged values mixed with untagged values. See Strict mode.

The severity of each rule is configured like the severity of any Roslyn rule. For instance, to turn the suggestions to tag return values into warnings, and to disable the comparison rule in the tests:

[*.cs]
taggedvalues.infer_tags_from_names = true
dotnet_diagnostic.MFTV0008.severity = warning

[tests/**.cs]
dotnet_diagnostic.MFTV0001.severity = none