Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA2021: false positive with ValueType and Enum #7031

Open
MrJul opened this issue Nov 15, 2023 · 1 comment · May be fixed by #7575
Open

CA2021: false positive with ValueType and Enum #7031

MrJul opened this issue Nov 15, 2023 · 1 comment · May be fixed by #7575

Comments

@MrJul
Copy link

MrJul commented Nov 15, 2023

Analyzer

Diagnostic ID: CA2021 (no documentation yet): Type 'U' is incompatible with type 'T' and cast attempts will throw InvalidCastException at runtime

Analyzer source

SDK: Built-in CA analyzers in .NET 8 SDK
Version: 8.0.100

Describe the bug

CA2021 incorrectly triggers when casting ValueType to a T : struct, and Enum to a T : struct, Enum.

Steps To Reproduce

public static IEnumerable<T> CastEnums<T>(IEnumerable<Enum> values) where T : struct, Enum
    => values.Cast<T>(); // CA2021

public static IEnumerable<T> CastValueTypes<T>(IEnumerable<ValueType> values) where T : struct
    => values.Cast<T>(); // CA2021

Expected behavior

No warnings.

Actual behavior

CA2021 warnings in both Cast<T> calls.

@colejohnson66
Copy link

colejohnson66 commented Nov 21, 2023

It seems the analyzer is complaining because of the struct constraint; Removing it removes the warning. Makes sense, as a struct type can't ever inherit from a class (except for when it can, here, and with ValueType).

n0099 added a commit to n0099/open-tbm that referenced this issue Mar 23, 2024
…ar to the nearest @ `RotateMatrix()`

* fix the violation of Roslyn analyzer rule `S1244: Floating point numbers should not be tested for equality`: SonarSource/sonar-dotnet#2326 @ `PreprocessTextBoxes()`
* using `RadiansToDegrees()` introduced in .NET 8: dotnet/runtime#86402 @ `GetRotationDegrees()`
@ TesseractRecognizer.cs

- the no longer effective suppression for Roslyn analyzer rule `SA1119:Statement should not use unnecessary parenthesis` due to DotNetAnalyzers/StyleCopAnalyzers#3730 @ `PaddleOcrRecognizerAndDetector.Initialize()`
* add some ad-hoc suppressions for ReSharper inspection `StringLiteralTypo`
@ imagePipeline

* remove the no longer effective suppression of Roslyn analyzer rule `SA1003 // Symbols should be spaced correctly` and fix the violation of ReSharper inspection `RedundantCast` @ `ThreadLateCrawlerAndSaver.CrawlThread()`
* suppress the violation of ReSharper inspection `InconsistentlySynchronizedField` @ `CrawlerLocks.AcquireFailed()`
* suppress the violations of Roslyn analyzer rule `CA2021:Do not call Enumerable.Cast<T> or Enumerable.OfType<T> with incompatible types` due to it's false positive: dotnet/roslyn-analyzers#7031 @ `ResumeSuspendPostContentsPushingWorker.DoWork()`
@ crawler

* suppress the violations of Roslyn analyzer rule `S6667: Logging in a catch clause should pass the caught exception as a parameter` @ `ErrorableWorker.DoWorkWithExceptionLogging()`
@ shared

* suppress the violation of Roslyn analyzer rule `S6674:Log message template should be syntactically correct` and ReSharper inspection `RedundantUsingDirective` @ GlobalSuppressions.cs
* fix all violations of Roslyn analyzer rule`CA1513: Use ObjectDisposedException throw helper`
* setting `csharp_style_prefer_primary_constructors` @ .editorconfig
* prevent `appsettings.*.json` get copied when publishing following https://weblog.west-wind.com/posts/2022/Aug/24/Keeping-Content-Out-of-the-Publish-Folder-for-WebDeploy#what-works-copytopublishdirectorynever @ `tbm.{Crawler,ImagePipeline}.csproj`
+ `tbm.sln.DotSettings` to store team-shared ReSharper options
@ c#
fowl2 pushed a commit to fowl2/roslyn-analyzers that referenced this issue Feb 14, 2025
fowl2 added a commit to fowl2/roslyn-analyzers that referenced this issue Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants