Skip to content

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

@Wazner

Description

@Wazner

Describe the bug

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

To Reproduce

uint[] a = [1, 2, 3];
var sequence = a.Cast<int>();

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions