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

fix CA2021 false positives #7183

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ static bool CastWillAlwaysFail(ITypeSymbol castFrom, ITypeSymbol castTo)
return false;
}

castFrom = castFrom.OriginalDefinition;
castTo = castTo.OriginalDefinition;

if (castFrom.SpecialType == SpecialType.System_Object
|| castTo.SpecialType == SpecialType.System_Object)
{
Expand All @@ -220,14 +223,14 @@ static bool IsUnconstrainedTypeParameter(ITypeParameterSymbol typeParameterSymbo
// UnmanagedTypeConstraint
// Nullability annotations

switch (castFrom.OriginalDefinition.TypeKind, castTo.OriginalDefinition.TypeKind)
switch (castFrom.TypeKind, castTo.TypeKind)
{
case (TypeKind.Dynamic, _):
case (_, TypeKind.Dynamic):
return false;

case (TypeKind.TypeParameter, _):
var castFromTypeParam = (ITypeParameterSymbol)castFrom.OriginalDefinition;
var castFromTypeParam = (ITypeParameterSymbol)castFrom;
if (IsUnconstrainedTypeParameter(castFromTypeParam))
{
return false;
Expand All @@ -246,7 +249,7 @@ static bool IsUnconstrainedTypeParameter(ITypeParameterSymbol typeParameterSymbo

return false;
case (_, TypeKind.TypeParameter):
var castToTypeParam = (ITypeParameterSymbol)castTo.OriginalDefinition;
var castToTypeParam = (ITypeParameterSymbol)castTo;
if (IsUnconstrainedTypeParameter(castToTypeParam))
{
return false;
Expand All @@ -270,32 +273,32 @@ static bool IsUnconstrainedTypeParameter(ITypeParameterSymbol typeParameterSymbo
&& !castTo.DerivesFrom(castFrom);

case (TypeKind.Interface, TypeKind.Class):
return castTo.IsSealed && !castTo.AllInterfaces.Contains(castFrom);
return castTo.IsSealed && !castTo.DerivesFrom(castFrom);
case (TypeKind.Class, TypeKind.Interface):
return castFrom.IsSealed && !castFrom.AllInterfaces.Contains(castTo);
return castFrom.IsSealed && !castFrom.DerivesFrom(castTo);

case (TypeKind.Interface, TypeKind.Struct):
return !castTo.AllInterfaces.Contains(castFrom);
return !castTo.DerivesFrom(castFrom);
case (TypeKind.Struct, TypeKind.Interface):
return !castFrom.AllInterfaces.Contains(castTo);
return !castFrom.DerivesFrom(castTo);

case (TypeKind.Class, TypeKind.Enum):
return castFrom.OriginalDefinition.SpecialType is not SpecialType.System_Enum
and not SpecialType.System_ValueType;
return castFrom.SpecialType is not SpecialType.System_Enum
and not SpecialType.System_ValueType;
case (TypeKind.Enum, TypeKind.Class):
return castTo.OriginalDefinition.SpecialType is not SpecialType.System_Enum
and not SpecialType.System_ValueType;
return castTo.SpecialType is not SpecialType.System_Enum
and not SpecialType.System_ValueType;

case (TypeKind.Struct, TypeKind.Enum)
when castTo.OriginalDefinition is INamedTypeSymbol toEnum:
when castTo is INamedTypeSymbol toEnum:
return !castFrom.Equals(toEnum.EnumUnderlyingType);
case (TypeKind.Enum, TypeKind.Struct)
when castFrom.OriginalDefinition is INamedTypeSymbol fromEnum:
when castFrom is INamedTypeSymbol fromEnum:
return !fromEnum.EnumUnderlyingType!.Equals(castTo);

case (TypeKind.Enum, TypeKind.Enum)
when castFrom.OriginalDefinition is INamedTypeSymbol fromEnum
&& castTo.OriginalDefinition is INamedTypeSymbol toEnum:
when castFrom is INamedTypeSymbol fromEnum
&& castTo is INamedTypeSymbol toEnum:
return !fromEnum.EnumUnderlyingType!.Equals(toEnum.EnumUnderlyingType);

// this is too conservative
Expand All @@ -310,14 +313,14 @@ when castFrom is IArrayTypeSymbol fromArray
|| CastWillAlwaysFail(fromArray.ElementType, toArray.ElementType);

case (TypeKind.Array, TypeKind.Class):
return castTo.OriginalDefinition.SpecialType != SpecialType.System_Array;
return castTo.SpecialType != SpecialType.System_Array;
case (TypeKind.Class, TypeKind.Array):
return castFrom.OriginalDefinition.SpecialType != SpecialType.System_Array;
return castFrom.SpecialType != SpecialType.System_Array;

case (TypeKind.Class, TypeKind.Struct):
return castFrom.OriginalDefinition.SpecialType != SpecialType.System_ValueType;
return castFrom.SpecialType != SpecialType.System_ValueType;
case (TypeKind.Struct, TypeKind.Class):
return castTo.OriginalDefinition.SpecialType != SpecialType.System_ValueType;
return castTo.SpecialType != SpecialType.System_ValueType;

case (_, TypeKind.Enum):
case (TypeKind.Enum, _):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
Expand All @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.NetCore.Analyzers.Runtime.DoNotCallEnumerableCastOrOfTypeWithIncompatibleTypesAnalyzer,
Expand Down Expand Up @@ -825,6 +826,45 @@ void Mstruct<TStruct>() where TStruct : struct
}.RunAsync();
}

[Fact]
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
public async Task GenericRecordsAndInterfaces()
{
var test = new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net70,
LanguageVersion = LanguageVersion.CSharp10,
TestCode = @"
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

public static class Program
{
public static void Main()
{
var nodeChanges = new List<INodeUpdate<GraphNode>> { new DataNodeUpdate(new DataNode(0, 0)) };

//warning CA2021: This call will always result in an empty sequence because type 'INodeUpdate<GraphNode>' is incompatible with type 'DataNodeUpdate'
var nodeChangesFiltered = nodeChanges.OfType<DataNodeUpdate>();

Debug.Assert(nodeChangesFiltered.Count() == 1);
}
}

public abstract record class GraphNode(long Id);
public sealed record class DataNode(long Id, long Value) : GraphNode(Id);

public interface INodeUpdate<out T>
{
T Updated { get; }
}

public abstract record class NodeUpdate<T>(T Updated) : INodeUpdate<T> where T : GraphNode;
public sealed record class DataNodeUpdate(DataNode Updated) : NodeUpdate<DataNode>(Updated);"
};
await test.RunAsync();
}

[Fact]
public async Task NonGenericCasesVB()
{
Expand Down