-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTypeSymbolExtensions.cs
More file actions
53 lines (50 loc) · 2.16 KB
/
TypeSymbolExtensions.cs
File metadata and controls
53 lines (50 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Diagnostics.CodeAnalysis;
using System.ComponentModel;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
namespace ExhaustiveMatching.Analyzer.Enums.Semantics
{
public static class TypeSymbolExtensions
{
/// <summary>
/// Is this the <see cref="InvalidEnumArgumentException"/> type?
/// </summary>
/// <remarks>Checking this way avoids using <see cref="Compilation.GetTypeByMetadataName"/>
/// which can return <see langword="null"/> if multiple types match a metadata name.</remarks>
public static bool IsInvalidEnumArgumentException(this ITypeSymbol typeSymbol)
// TODO is there additional stuff that should be checked? (e.g. system assembly or correct base class)
=> typeSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)
== typeof(InvalidEnumArgumentException).FullName;
public static bool IsEnum(
this ITypeSymbol type,
SyntaxNodeAnalysisContext context,
[NotNullWhen(true)] out INamedTypeSymbol? enumType,
out bool nullable)
{
switch (type.TypeKind)
{
case TypeKind.Enum:
enumType = (INamedTypeSymbol)type;
nullable = false;
return true;
case TypeKind.Struct:
var nullableType = context.Compilation.GetTypeByMetadataName(TypeNames.Nullable);
if (SymbolEqualityComparer.IncludeNullability.Equals(type.OriginalDefinition, nullableType))
{
type = ((INamedTypeSymbol)type).TypeArguments.Single();
if (type.TypeKind == TypeKind.Enum)
{
enumType = (INamedTypeSymbol)type;
nullable = true;
return true;
}
}
break;
}
enumType = null;
nullable = false;
return false;
}
}
}