Skip to content

SimplifyNullCheckAnalyzer throws NullReferenceException #1005

@doxxx

Description

@doxxx

Product and Version Used:

Visual Studio 2022 v17.4.1
Roslynator 4.2.0

Steps to Reproduce:

.NET Framework 4.7.2 library project. Subset of original class, which reproduces problem:

public static class SomeClass
{
    public static T DoStuff<T>(string propertyName, string result)
    {
        return default(T) == null ? (T)Convert.ChangeType(string.Empty, typeof(T)) : default(T);
    }
}

SimplifyNullCheckAnalyzer throws an exception for the return statement in the above function.

Analyzer 'Roslynator.CSharp.Analysis.SimplifyNullCheckAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.
Exception occurred with following context:
Compilation: Synaptive.Drive.Optics.Camera.Common
SyntaxTree: C:\Users\gordon.tyler\dev\Synaptive\ModusV\Drive\Source\Optics\Camera\Common\CameraCommands.cs
SyntaxNode: default(T) == null ? (T)Convert ... [ConditionalExpressionSyntax]@[10158..10238) (210,19)-(210,99)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Roslynator.CSharp.Syntax.NullCheckExpressionInfo.IsNullOrDefault(ExpressionSyntax left, ExpressionSyntax right, SemanticModel semanticModel, CancellationToken cancellationToken)
   at Roslynator.CSharp.Syntax.NullCheckExpressionInfo.Create(BinaryExpressionSyntax binaryExpression, SyntaxKind binaryExpressionKind, ExpressionSyntax expression1, ExpressionSyntax expression2, NullCheckStyles allowedStyles, Boolean allowMissing, SemanticModel semanticModel, CancellationToken cancellationToken)
   at Roslynator.CSharp.Syntax.NullCheckExpressionInfo.CreateImpl(SyntaxNode node, SemanticModel semanticModel, NullCheckStyles allowedStyles, Boolean walkDownParentheses, Boolean allowMissing, CancellationToken cancellationToken)
   at Roslynator.CSharp.Syntax.NullCheckExpressionInfo.Create(SyntaxNode node, SemanticModel semanticModel, NullCheckStyles allowedStyles, Boolean walkDownParentheses, Boolean allowMissing, CancellationToken cancellationToken)
   at Roslynator.CSharp.Analysis.SimplifyNullCheckAnalyzer.AnalyzeConditionalExpression(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__62`1.<ExecuteSyntaxNodeAction>b__62_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions