Minor refactors to first-party Analyzers and Source Generators

This commit is contained in:
YoshiRulz 2024-05-30 05:39:57 +10:00
parent b8c600783a
commit ea15126fe1
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
4 changed files with 9 additions and 6 deletions

View File

@ -44,10 +44,11 @@ public sealed class FirstOrDefaultOnStructAnalyzer : DiagnosticAnalyzer
var calledSym = operation.TargetMethod.ConstructedFrom; var calledSym = operation.TargetMethod.ConstructedFrom;
if (!(firstOrDefaultWithPredSym!.Matches(calledSym) || firstOrDefaultNoPredSym!.Matches(calledSym))) return; if (!(firstOrDefaultWithPredSym!.Matches(calledSym) || firstOrDefaultNoPredSym!.Matches(calledSym))) return;
var receiverExprType = operation.SemanticModel!.GetTypeInfo((CSharpSyntaxNode) operation.Arguments[0].Syntax).ConvertedType!; var receiverExprType = operation.SemanticModel!.GetTypeInfo((CSharpSyntaxNode) operation.Arguments[0].Syntax).ConvertedType!;
var collectionElemType = receiverExprType switch { var collectionElemType = receiverExprType switch
{
INamedTypeSymbol nts => nts.TypeArguments[0], INamedTypeSymbol nts => nts.TypeArguments[0],
IArrayTypeSymbol ats => ats.ElementType, IArrayTypeSymbol ats => ats.ElementType,
_ => throw new InvalidOperationException($"receiver parameter's effective type was of an unexpected kind (neither class/struct nor array): {receiverExprType}") _ => throw new InvalidOperationException($"receiver parameter's effective type was of an unexpected kind (neither class/struct nor array): {receiverExprType.GetType().FullName}")
}; };
if (collectionElemType.IsValueType) oac.ReportDiagnostic(Diagnostic.Create(DiagUseFirstOrNull, operation.Syntax.GetLocation())); if (collectionElemType.IsValueType) oac.ReportDiagnostic(Diagnostic.Create(DiagUseFirstOrNull, operation.Syntax.GetLocation()));
}, },

View File

@ -77,7 +77,7 @@ namespace {nSpace}
{{ {{
private const string EMBED_PREFIX = ""{nSpace}.""; private const string EMBED_PREFIX = ""{nSpace}."";
private static Type[]? _types; private static Type[]? _types = null;
private static readonly Assembly Asm = typeof({nSpace}.ReflectionCache).Assembly; private static readonly Assembly Asm = typeof({nSpace}.ReflectionCache).Assembly;
@ -88,17 +88,19 @@ namespace {nSpace}
public static IEnumerable<string> EmbeddedResourceList(string extraPrefix) public static IEnumerable<string> EmbeddedResourceList(string extraPrefix)
{{ {{
var fullPrefix = EMBED_PREFIX + extraPrefix; var fullPrefix = EMBED_PREFIX + extraPrefix;
return Asm.GetManifestResourceNames().Where(s => s.StartsWith(fullPrefix, StringComparison.Ordinal)).Select(s => s.RemovePrefix(fullPrefix)); return Asm.GetManifestResourceNames().Where(s => s.StartsWithOrdinal(fullPrefix)) // seems redundant with `RemovePrefix`, but we only want these in the final list
.Select(s => s.RemovePrefix(fullPrefix));
}} }}
public static IEnumerable<string> EmbeddedResourceList() public static IEnumerable<string> EmbeddedResourceList()
=> EmbeddedResourceList(string.Empty); => EmbeddedResourceList(string.Empty); // can't be simplified to `Asm.GetManifestResourceNames` call
/// <exception cref=""ArgumentException"">not found</exception> /// <exception cref=""ArgumentException"">not found</exception>
public static Stream EmbeddedResourceStream(string embedPath) public static Stream EmbeddedResourceStream(string embedPath)
{{ {{
var fullPath = EMBED_PREFIX + embedPath; var fullPath = EMBED_PREFIX + embedPath;
return Asm.GetManifestResourceStream(fullPath) ?? throw new ArgumentException($""resource at {{fullPath}} not found"", nameof(embedPath)); return Asm.GetManifestResourceStream(fullPath)
?? throw new ArgumentException(paramName: nameof(embedPath), message: $""resource at {{fullPath}} not found"");
}} }}
}} }}
}} }}

Binary file not shown.