dotnet / dotnet/command-line-api

`Command` class violates design rule CA1010

Open
#2,751 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

# Summary
The `System.CommandLine.Command` class implements non-generic `IEnumerable`, but it does not implement `IEnumerable`.

https://github.com/dotnet/command-line-api/blob/cf5fd8d696450a48d3cc75a7a1792d34b5303f88/src/System.CommandLine/Command.cs#L25

This causes classes inheriting from the `Command` class to violate [design rule CA1010](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1010).
For projects with `AnalysisMode` set to `Recommended` or `All`, warning CA1010 will occur.

```
Build succeeded.

/home/smdn/temp/cli/Lib.cs(5,14): warning CA1010: Type 'MyCommand' directly or indirectly inherits 'IEnumerable' without implementing 'IEnumerable'. Publicly-visible types should implement the generic version to broaden usability. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1010) [/home/smdn/temp/cli/cli.csproj]
1 Warning(s)
0 Error(s)
```

This simply triggers a warning and does not corrupt any functionality of `System.CommandLine`.
However, since the cause of the warning is not easy to find out and it may cause confusion, so I think this should be fixed.

# Steps to reproduce
Set `AnalysisMode` to `Recommended` or `All` in the csproj file.

```csproj


Exe
net10.0
enable
enable


Recommended



```

Create a class that inherits from the `Command` class.

```cs
using System.CommandLine;

namespace MyLibrary;

// warning CA1010: Type 'MyCommand' directly or indirectly inherits 'IEnumerable' without implementing 'IEnumerable'. Publicly-visible types should implement the generic version to broaden usability. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1010)
public class MyCommand(string name, string? description) : Command(name, description) { }
```

# Workarounds
The following workarounds are available to avoid this warning.

## Workaround 1: disable warning CA1010
```cs
#pragma warning disable CA1010
public class MyCommand(string name, string? description) : Command(name, description)
{
}
#pragma warning restore CA1010
```

## Workaround 2: implement `IEnumerator`
```cs
public class MyCommand(string name, string? description) : Command(name, description)
{
// Implement IEnumerator in order to suppress warning CA1010.
public IEnumerator GetEnumerator() => Children.GetEnumerator();
}
```

## Workaround 3: reduce `AnalysisMode`
```csproj

Minimum
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.