Skip to content

Commit

Permalink
Use more [NotNullWhen(..)]
Browse files Browse the repository at this point in the history
  • Loading branch information
j3parker committed Aug 7, 2024
1 parent 9270442 commit 7640157
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.CodeAnalysis;

Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/D2L.CodeStyle.TestAnalyzers/NUnit/CategoryAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ INamedTypeSymbol attributeType
.GetSymbolInfo( attribute, context.CancellationToken )
.Symbol;

if( symbol == null || symbol.Kind == SymbolKind.ErrorType ) {
if( symbol.IsNullOrErrorType() ) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CompilationStartAnalysisContext context
INamedTypeSymbol? factoryType = context.Compilation
.GetTypeByMetadataName( TestServiceLocatorFactoryType );

if( factoryType == null || factoryType.IsErrorType() ) {
if( factoryType.IsNullOrErrorType() ) {
return;
}

Expand Down

0 comments on commit 7640157

Please sign in to comment.