From aad8dd8d95f9deac123a5336f54730aeaa8df8a7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 14 Jun 2023 23:19:13 +0200 Subject: [PATCH] Remove some null suppressions, add [NotNullWhen] --- .../AutoConstructorGenerator.cs | 3 +-- .../D2DPixelShaderSourceGenerator.Execute.cs | 8 ++++---- .../InvalidAssemblyLevelCompileOptionsAnalyzer.cs | 2 +- .../ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs | 2 +- .../ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs | 2 +- .../ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs | 2 +- .../ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs | 8 ++++---- ...nerator.CreateLoadResourceTextureDescriptionsMethod.cs | 2 +- .../Extensions/ISymbolExtensions.cs | 3 ++- .../IShaderGenerator.CreateLoadBytecodeMethod.cs | 2 +- 10 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs index 3d6e8ab9..0da326cc 100644 --- a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs +++ b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs @@ -79,8 +79,7 @@ public sealed class AutoConstructorGenerator : IIncrementalGenerator // Check whether the field is annotated to have a special behavior if (fieldSymbol.Type.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.AutoConstructorBehaviorAttribute", out AttributeData? usageData) && - usageData!.ConstructorArguments is { Length: 1 } arguments && - arguments[0].Value is (int)AutoConstructorBehavior.IgnoreAndSetToDefault) + usageData.ConstructorArguments is [{ Value: (int)AutoConstructorBehavior.IgnoreAndSetToDefault }]) { defaulted.Add(fieldSymbol.Name); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index 004ee0ca..484d75dc 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -85,12 +85,12 @@ partial class D2DPixelShaderSourceGenerator { if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out AttributeData? attributeData)) { - return (D2D1ShaderProfile)attributeData!.ConstructorArguments[0].Value!; + return (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!; } if (methodSymbol.ContainingAssembly.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out attributeData)) { - return (D2D1ShaderProfile)attributeData!.ConstructorArguments[0].Value!; + return (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!; } diagnostics.Add( @@ -112,12 +112,12 @@ partial class D2DPixelShaderSourceGenerator { if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { - return (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; + return (D2D1CompileOptions)attributeData.ConstructorArguments[0].Value!; } if (methodSymbol.ContainingAssembly.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out attributeData)) { - return (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; + return (D2D1CompileOptions)attributeData.ConstructorArguments[0].Value!; } diagnostics.Add( diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidAssemblyLevelCompileOptionsAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidAssemblyLevelCompileOptionsAnalyzer.cs index e3dc2b6c..ac525a1a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidAssemblyLevelCompileOptionsAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidAssemblyLevelCompileOptionsAnalyzer.cs @@ -31,7 +31,7 @@ public sealed class InvalidAssemblyLevelCompileOptionsAnalyzer : DiagnosticAnaly // - The input D2D1CompileOptions value, which can be retrieved from the constructor arguments of the AttributeData object. if (assemblySymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { - D2D1CompileOptions options = (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; + D2D1CompileOptions options = (D2D1CompileOptions)attributeData.ConstructorArguments[0].Value!; if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index 331cddbe..41b86846 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -155,7 +155,7 @@ partial class ID2D1ShaderGenerator // by the logic handling the info gathering for LoadResourceTextureDescriptions. if (fieldSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DResourceTextureIndexAttribute", out AttributeData? attributeData)) { - _ = attributeData!.TryGetConstructorArgument(0, out index); + _ = attributeData.TryGetConstructorArgument(0, out index); } resourceTextures.Add((mapping ?? fieldSymbol.Name, typeName, index)); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs index c6d17dde..e2f0b848 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs @@ -32,7 +32,7 @@ partial class ID2D1ShaderGenerator } // Extract the buffer precision and channel depth, if available - if (attributeData!.ConstructorArguments.Length == 1) + if (attributeData.ConstructorArguments.Length == 1) { if (attributeData.AttributeConstructor is IMethodSymbol { Parameters.Length: 1 } constructorSymbol && constructorSymbol.Parameters[0].Type.HasFullyQualifiedMetadataName("ComputeSharp.D2D1.D2D1BufferPrecision")) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs index 7b805a78..428cbe61 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs @@ -20,7 +20,7 @@ partial class ID2D1ShaderGenerator { if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DPixelOptionsAttribute", out AttributeData? attributeData)) { - pixelOptions = (D2D1PixelOptions)attributeData!.ConstructorArguments[0].Value!; + pixelOptions = (D2D1PixelOptions)attributeData.ConstructorArguments[0].Value!; } else { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index 1854e959..cc9a22f1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -35,12 +35,12 @@ partial class ID2D1ShaderGenerator { if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out AttributeData? attributeData)) { - return (D2D1ShaderProfile)attributeData!.ConstructorArguments[0].Value!; + return (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!; } if (structDeclarationSymbol.ContainingAssembly.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out attributeData)) { - return (D2D1ShaderProfile)attributeData!.ConstructorArguments[0].Value!; + return (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!; } return null; @@ -56,7 +56,7 @@ partial class ID2D1ShaderGenerator { if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { - D2D1CompileOptions options = (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; + D2D1CompileOptions options = (D2D1CompileOptions)attributeData.ConstructorArguments[0].Value!; if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) { @@ -73,7 +73,7 @@ partial class ID2D1ShaderGenerator if (structDeclarationSymbol.ContainingAssembly.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out attributeData)) { // No need to validate against PackMatrixColumnMajor as that's checked separately - return (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value! | D2D1CompileOptions.PackMatrixRowMajor; + return (D2D1CompileOptions)attributeData.ConstructorArguments[0].Value! | D2D1CompileOptions.PackMatrixRowMajor; } return null; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs index b8ae3707..726f08b4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs @@ -56,7 +56,7 @@ partial class ID2D1ShaderGenerator { // If the constructor argument isn't available, it means the code is invalid. Just do nothing then, as the // user will have to fix that to get the code to compile anyway. This way the generator won't crash too. - if (attributeData!.TryGetConstructorArgument(0, out int resourceIndex)) + if (attributeData.TryGetConstructorArgument(0, out int resourceIndex)) { index = resourceIndex; } diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index fc24ec06..fa276e2b 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis; namespace ComputeSharp.SourceGeneration.Extensions; @@ -33,7 +34,7 @@ internal static class ISymbolExtensions /// The attribute name to look for. /// The resulting attribute data, if found. /// Whether or not has an attribute with the specified name. - public static bool TryGetAttributeWithFullyQualifiedMetadataName(this ISymbol symbol, string name, out AttributeData? attributeData) + public static bool TryGetAttributeWithFullyQualifiedMetadataName(this ISymbol symbol, string name, [NotNullWhen(true)] out AttributeData? attributeData) { foreach (AttributeData attribute in symbol.GetAttributes()) { diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs index 7f529115..8d988930 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs @@ -63,7 +63,7 @@ partial class IShaderGenerator int threadsZ; // Check for a dispatch axis argument first - if (attribute!.ConstructorArguments.Length == 1) + if (attribute.ConstructorArguments.Length == 1) { int dispatchAxis = (int)attribute.ConstructorArguments[0].Value!;