Skip to content

CMTK0005: report ImmutableArray<T>.Builder.Count set on a builder of value objects #269

Open

Description

@CaffeinatedCoder

Found in the weaving review of #229, beside CollectionsMarshal.SetCount.

ImmutableArray<T>.Builder.Count = n grows the builder with default items whenever n is larger than its count, as CollectionsMarshal.SetCount(list, n) does for a List<T>, and CMTK0005 does not report it:

var builder = ImmutableArray.CreateBuilder<OrderId>();
builder.Count = 3;                       // three default OrderIds, nothing reported
var ids = builder.MoveToImmutable();

After #229 the Analyzers README's row says "an array, span or list ... given a length", which a reader takes to include this.

Proposal

CMTK0005 reports an assignment to ImmutableArray<T>.Builder.Count (resolved by symbol) where T is a value object, Option, Result or [RequireCustomInitialization] struct, unless the value is a constant zero. A Warning reporting more needs no exception to decision 34. Check the other BCL builders and lists with a settable count while there.

Test

A CMTK0005 row for the setter, a zero control, across TypeArgumentKinds.

Activity

  1. added this to the 2.4.0 milestone on Oct 31, 2026
  2. added
    enhancement
    package/analyzers
    area/value-objects
    on Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/value-objectsenhancementpackage/analyzers

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions