提交 b24605c8 编写于 作者: M Manish Vasani

Merge pull request #3271 from mavasani/Issue3254

Fix ITypeSymbolExtensions.IsOrDerivedFromExceptionType to handle type parameters constrained on Exception type or its subtype. Fixes #3254 
......@@ -3747,6 +3747,46 @@ class C
class D : C
{
}");
}
[WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public void DontRemoveCastToTypeParameterWithExceptionContraint()
{
TestMissing(
@"using System;
class Program
{
private static void RequiresCondition<TException>(bool condition, string messageOnFalseCondition)
where TException : Exception
{
if (!condition)
{
throw [|(TException)Activator.CreateInstance(typeof(TException), messageOnFalseCondition)|];
}
}
}");
}
[WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public void DontRemoveCastToTypeParameterWithExceptionSubTypeContraint()
{
TestMissing(
@"using System;
class Program
{
private static void RequiresCondition<TException>(bool condition, string messageOnFalseCondition)
where TException : ArgumentException
{
if (!condition)
{
throw [|(TException)Activator.CreateInstance(typeof(TException), messageOnFalseCondition)|];
}
}
}");
}
}
......
......@@ -2655,6 +2655,42 @@ End Class
Class Derived2 : Inherits Base
End Class
</File>
TestMissing(markup)
End Sub
<WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)>
Public Sub DontRemoveCastToTypeParameterWithExceptionContraint()
Dim markup =
<File>
Imports System
Class Program
Private Shared Sub RequiresCondition(Of TException As Exception)(condition As Boolean, messageOnFalseCondition As String)
If Not condition Then
Throw [|DirectCast(Activator.CreateInstance(GetType(TException), messageOnFalseCondition), TException)|]
End If
End Sub
End Class
</File>
TestMissing(markup)
End Sub
<WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)>
Public Sub DontRemoveCastToTypeParameterWithExceptionSubTypeContraint()
Dim markup =
<File>
Imports System
Class Program
Private Shared Sub RequiresCondition(Of TException As ArgumentException)(condition As Boolean, messageOnFalseCondition As String)
If Not condition Then
Throw [|DirectCast(Activator.CreateInstance(GetType(TException), messageOnFalseCondition), TException)|]
End If
End Sub
End Class
</File>
TestMissing(markup)
End Sub
......
......@@ -777,12 +777,29 @@ public static bool IsOrDerivesFromExceptionType(this ITypeSymbol type, Compilati
{
if (type != null)
{
foreach (var baseType in type.GetBaseTypesAndThis())
switch(type.Kind)
{
if (baseType.Equals(compilation.ExceptionType()))
{
return true;
}
case SymbolKind.NamedType:
foreach (var baseType in type.GetBaseTypesAndThis())
{
if (baseType.Equals(compilation.ExceptionType()))
{
return true;
}
}
break;
case SymbolKind.TypeParameter:
foreach (var constraint in ((ITypeParameterSymbol)type).ConstraintTypes)
{
if (constraint.IsOrDerivesFromExceptionType(compilation))
{
return true;
}
}
break;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册