From 764015774feaf922c1143000e766b7083cb75a9e Mon Sep 17 00:00:00 2001 From: Jacob Parker Date: Wed, 7 Aug 2024 11:54:37 -0400 Subject: [PATCH] Use more [NotNullWhen(..)] --- .../Extensions/Microsoft.CodeAnalysis.cs | 22 ++++++------------- .../NUnit/CategoryAnalyzer.cs | 2 +- .../NUnit/ConfigTestSetupStringsAnalyzer.cs | 4 ++-- .../CustomTestServiceLocatorAnalyzer.cs | 2 +- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/D2L.CodeStyle.TestAnalyzers/Extensions/Microsoft.CodeAnalysis.cs b/src/D2L.CodeStyle.TestAnalyzers/Extensions/Microsoft.CodeAnalysis.cs index 059370c2..404fa12e 100644 --- a/src/D2L.CodeStyle.TestAnalyzers/Extensions/Microsoft.CodeAnalysis.cs +++ b/src/D2L.CodeStyle.TestAnalyzers/Extensions/Microsoft.CodeAnalysis.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; using System.Text; using Microsoft.CodeAnalysis; @@ -9,7 +10,9 @@ public static class RoslynExtensions { // Copied from the non-test assembly because we do not reference it. - public static bool IsNullOrErrorType( this ITypeSymbol? symbol ) { + public static bool IsNullOrErrorType( + [NotNullWhen( false )] this ITypeSymbol? symbol + ) { if( symbol == null ) { return true; @@ -26,20 +29,9 @@ public static bool IsNullOrErrorType( this ITypeSymbol? symbol ) { return false; } - public static bool IsErrorType( this ITypeSymbol symbol ) { - - if( symbol.Kind == SymbolKind.ErrorType ) { - return true; - } - - if( symbol.TypeKind == TypeKind.Error ) { - return true; - } - - return false; - } - - public static bool IsNullOrErrorType( this ISymbol symbol ) { + public static bool IsNullOrErrorType( + [NotNullWhen( false )] this ISymbol? symbol + ) { if( symbol == null ) { return true; diff --git a/src/D2L.CodeStyle.TestAnalyzers/NUnit/CategoryAnalyzer.cs b/src/D2L.CodeStyle.TestAnalyzers/NUnit/CategoryAnalyzer.cs index b5c5582c..27626a56 100644 --- a/src/D2L.CodeStyle.TestAnalyzers/NUnit/CategoryAnalyzer.cs +++ b/src/D2L.CodeStyle.TestAnalyzers/NUnit/CategoryAnalyzer.cs @@ -54,7 +54,7 @@ private static void OnCompilationStart( CompilationStartAnalysisContext context context: ctx, bannedCategories: bannedCategories, types: types, - syntax: (ctx.Node as MethodDeclarationSyntax)! + syntax: (MethodDeclarationSyntax)ctx.Node ), SyntaxKind.MethodDeclaration ); diff --git a/src/D2L.CodeStyle.TestAnalyzers/NUnit/ConfigTestSetupStringsAnalyzer.cs b/src/D2L.CodeStyle.TestAnalyzers/NUnit/ConfigTestSetupStringsAnalyzer.cs index 6828a5c6..d0b45710 100644 --- a/src/D2L.CodeStyle.TestAnalyzers/NUnit/ConfigTestSetupStringsAnalyzer.cs +++ b/src/D2L.CodeStyle.TestAnalyzers/NUnit/ConfigTestSetupStringsAnalyzer.cs @@ -30,7 +30,7 @@ private static void Register( CompilationStartAnalysisContext context ) { INamedTypeSymbol? attributeType = context.Compilation.GetTypeByMetadataName( AttributeTypeName ); - if( attributeType == null || attributeType.IsErrorType() ) { + if( attributeType.IsNullOrErrorType() ) { return; } @@ -78,7 +78,7 @@ INamedTypeSymbol attributeType .GetSymbolInfo( attribute, context.CancellationToken ) .Symbol; - if( symbol == null || symbol.Kind == SymbolKind.ErrorType ) { + if( symbol.IsNullOrErrorType() ) { continue; } diff --git a/src/D2L.CodeStyle.TestAnalyzers/ServiceLocator/CustomTestServiceLocatorAnalyzer.cs b/src/D2L.CodeStyle.TestAnalyzers/ServiceLocator/CustomTestServiceLocatorAnalyzer.cs index c07cc0a5..61e6c8ca 100644 --- a/src/D2L.CodeStyle.TestAnalyzers/ServiceLocator/CustomTestServiceLocatorAnalyzer.cs +++ b/src/D2L.CodeStyle.TestAnalyzers/ServiceLocator/CustomTestServiceLocatorAnalyzer.cs @@ -31,7 +31,7 @@ CompilationStartAnalysisContext context INamedTypeSymbol? factoryType = context.Compilation .GetTypeByMetadataName( TestServiceLocatorFactoryType ); - if( factoryType == null || factoryType.IsErrorType() ) { + if( factoryType.IsNullOrErrorType() ) { return; }