Skip to content

c# 14 extension members not understood properly for XmlDoc #1752

@dodexahedron

Description

@dodexahedron

The following C# file, which is used as part of a suite of extensions for System.CommandLine, is perfectly valid and the XmlDoc is understood by Roslyn and Visual Studio 2026 without issue:

MIT License notice from file header
// Copyright 2025 Brandon Thetford
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// See https://opensource.org/license/MIT/

using System.CommandLine.Completions;

namespace SnapsInAZfs.CommandLine.Completions;

/// <summary>
///   Extensions for <see cref="CompletionItem" />.
/// </summary>
public static class CompletionItemExtensions
{
  /// <param name="enumMember">
  ///   The <see langword="enum" /> member of type <typeparamref name="TEnum" /> from which to create the <see cref="CompletionItem" />.
  /// </param>
  /// <typeparam name="TEnum">An <see langword="enum" /> type.</typeparam>
  extension<TEnum> ( TEnum enumMember )
    where TEnum : unmanaged, Enum
  {
    /// <summary>
    ///   Creates a <see cref="CompletionItem" /> instance from an <see langword="enum" /> member where the
    ///   <see cref="CompletionItem.Label" /> is the name of the <see langword="enum" /> member and the
    ///   <see cref="CompletionItem.SortText" /> is the numeric value of the <see langword="enum" /> member, so that they are sorted by
    ///   value, rather than label.
    /// </summary>
    /// <returns>
    ///   A <see cref="CompletionItem" /> with <see cref="CompletionItem.Label" /> set to the name of the <see langword="enum" /> member
    ///   and <see cref="CompletionItem.SortText" /> set to the value of the <see langword="enum" /> member.
    /// </returns>
    public CompletionItem ToOrderableCompletionItem ( )
    {
      return new ( $"{enumMember:G}", sortText: $"{enumMember:D}" );
    }
  }
}

Roslynator takes issue with several things about it:

  1. It doesn't think the type parameter exists (RCS1263).
  2. It doesn't think the parameter for the extension block exists (Also RCS12631).
  3. It thinks the extension block needs a summary element (RCS1139).

It is wrong on all three counts, because both 1 and 2 do exist and are correct, and 3 would be invalid syntax for XmlDoc on an extension block. Summary is not allowed/understood there, by Roslyn.

It feels similar to the issues that several other analyzers out there have with C# 14 extension blocks: The extension block seems to get caught by whatever state the parser is in when it is looking for kinds of members that existed prior to C# 14 (though it would have been invalid syntax anyway, so it's a bug for pre-14 as well, but from a different angle).

Roslyn clearly understands the syntax, as it compiles without warnings (other than the ones from Roslynator) and intellisense works as expected, for all documented elements.

Footnotes

  1. Having the same ID for both type parameter not found and parameter not found seems suboptimal since they are distinct, albeit similar, situations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions