Skip to content

BRO1311: Tuple element names should use correct casing

Element names in tuple types and in tuple literals are PascalCase: (int Count, string Name) and (Count: 3, Name: "bolts"), not (int count, string name) and (count: 3, name: "bolts"). Replaces StyleCop SA1316.

Example

Before

internal class Inventory
{
    public (int count, string name) Largest() => (count: 3, name: "bolts");

    public string Describe()
    {
        var item = Largest();
        return item.name + ": " + item.count;
    }
}

After

internal class Inventory
{
    public (int Count, string Name) Largest() => (Count: 3, Name: "bolts");

    public string Describe()
    {
        var item = Largest();
        return item.Name + ": " + item.Count;
    }
}

Why

Tuple elements are public fields of the tuple, so they read like members: item.Name, as for a property. Consistent casing also keeps a tuple-returning method and a record or class returning the same data interchangeable.

Configuration

[*.cs]
# PascalCase (default) or camelCase (tupleElementNameCasing in stylecop.json).
stylebro_tuple_element_name_casing = camelCase

bro migrate writes it from stylecop.json.

stylebro_rename_public_api = true also renames names other assemblies see (default false, see Not reported; the owner's decision in decisions.md).

How the fix works

Tuple element names belong to the tuple type where they're written, and a use like item.count binds to the element of whichever tuple type item has. So the fix renames the name everywhere at once, in the whole solution:

  • every tuple type element with that name (a method's return type, a field, a local's type, a type argument);
  • every name in a tuple literal converted to one of them ((count: 3, ...) returned from the method above), so the compiler doesn't warn that the literal's name is ignored (CS8123);
  • every name in a tuple literal that keeps its own type (var best = (count: 3, name: "bolts");, a literal in a lambda or passed to a generic method): the literal is where that element is declared;
  • every name in a tuple literal that the compiler ignores, because the literal is converted to a tuple type that names the element differently or not at all ((int A, int B) pair = (count: 3, 4);): only the name changes;
  • every use of those elements: item.count, item?.count, nameof(item.count), item is (count: 1, _);
  • overrides and implementations together with what they override: their names have to match (CS8139).

A tuple literal that takes an element name from a renamed use (var next = (item.count, 1);) gets the new name with it ((item.Count, 1) declares Count), so the uses of that element are renamed too (next.Count), and so on through every literal named that way. Only where another element of that literal has the new name already is the old name written out ((item.count, Count: 2) becomes (count: item.Count, Count: 2)), and stays. A literal converted to a tuple type (return (true, item.Count); from a method returning (bool Found, int Total)) takes that type's names anyway. An anonymous object isn't a tuple: new { item.count } gets the old name written out (new { count = item.Count }), so it and its own uses keep working unchanged.

The names of one tuple type are renamed all or none: when one of them has to keep its name (see below), the others in the same tuple types keep theirs too, rather than leaving (int Total, int max). bro format lists the element that keeps its name with its reason, and each of the others as kept with it. Deconstruction (var (count, name) = item;) declares variables, not element names, and isn't touched.

Not reported

  • Where IDE1006 is suppressed: after #pragma warning disable IDE1006 (up to its restore), or with [SuppressMessage("Style", "IDE1006")] on the declaration, its member or type, or a global one targeting them. Code that opts out of the SDK's naming rules (a file copied from another project) keeps its names.
  • Where ReSharper's InconsistentNaming is suppressed: below // ReSharper disable once InconsistentNaming (the declaration, statement or member right under it: a method's covers its parameters), or after // ReSharper disable InconsistentNaming up to its restore. The author already said the name is on purpose.
  • The name appears in code excluded by #if anywhere in the project: disabled code isn't compiled, so the rename can't follow it.
  • Public API (unless stylebro_rename_public_api = true): Tuple element names in the types of a member's signature (return, parameter, property, field or event types) that other assemblies see, that is public, protected or protected internal in types that are visible too. Renaming them breaks code outside the solution. Internal code is renamed, also with InternalsVisibleTo.
  • _ and names that don't start with a letter.
  • Names a member has to take from a library member it overrides or implements: the library can't be renamed. An override or implementation of a member in the solution is reported, and renamed together with it.
  • Names a member has to take from public API in the solution it overrides or implements (an internal class implementing a public interface), unless stylebro_rename_public_api = true: the public member keeps its names, so the implementation has to keep them too.
  • A name in a tuple literal that follows a declared tuple type's name ((int count, int Total) best = (count: 3, Total: 4);): the declared element is what's reported, and its fix renames the literal too. Where the declaration keeps its name (public API, a library's member, a suppression), the literal has to keep it as well. The same for a literal compared with such a tuple (best == (count: 3, Total: 4)).
  • A name in a tuple literal when another element of the literal has the new name, written or inferred ((count: 1, Count: 2), (count: 1, other.Count)), or the new name is a member of every tuple (rest, toString, item2 for another element than the second).
  • Names a literal infers ((count, name) with variables of those names): the variable's name is another rule's.
  • A new name that would be a keyword (Var in camelCase).

Reported but not fixed

If dotnet format leaves a finding, StyleBro kept it on purpose. The fix looks at the whole solution, other projects included, which the analyzer can't see, and leaves a change that would break something there: the name in a string or a nameof (reflection, serialization), a log key (an interpolated string hole whose handler records the hole's text with [CallerArgumentExpression]), a member that would hide it, a use in generated code. It offers no code action for such a finding, and bro format lists each one with its reason (Findings kept on purpose); its --verify-no-changes doesn't fail on them. Change it by hand only after checking those uses, or suppress it or record it in the baseline.

A rename that would add compile errors (or CS8123 or CS8383 warnings: a tuple literal's name the compiler ignores) anywhere in the solution is skipped, and the warning stays: for example a tuple that already has the new name ((int count, int Count)), or an override of a library member whose own signature names the elements. When such an element is in another project, each project's elements with that name are judged on their own: one project's tuple that can't be renamed doesn't keep an unrelated one elsewhere. So is a rename that would let IDE0042 (deconstruct variable declaration) report a variable where your settings make IDE0042 a warning or an error: IDE0042 leaves var result = Get(); alone while an element shares its name with something in scope (a local pointer next to result.pointer), and the new name would end that.