dotnet / dotnet/sdk

False positive CA2021 when calling Enumerable.Cast on types that have a primitive type conversion

Open
#52,704 3 comments 0 reactions 0 assignees View on GitHub
Area-Microsoft.CodeAnalysis.NetAnalyzers untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Describe the bug
I am getting false positives of rule CA2021 in my project on code that does work at runtime.

### To Reproduce
```csharp
uint[] a = [1, 2, 3];
var sequence = a.Cast();
```

Here the Cast triggers CA2021, even though generic `uint` can be casted to `int` no problem.

Runtime does fail for widening or narrowing conversions (int -> long, etc), so there the diagnostic is correct.
Just this specific path is a false positive.

### Exceptions (if any)
No exception

### Further technical details
Package 'Microsoft.CodeAnalysis.NetAnalyzers' version 10.0.102

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.