Skip to content

"InvalidOperationException: Object not initialized" when building .NET Framework project #1232

@KavenBreton-eaton

Description

@KavenBreton-eaton

Product and Version Used:
Roslynator.Analyzers 4.6.0, Roslynator.CodeAnalysis.Analyzers 4.6.0 and Roslynator.Formatting.Analyzers 4.6.0
.NET Framework 4.8
Visual Studio 2022

Steps to Reproduce:
We upgraded from 4.5.0 to 4.6.0 and this error started popping when building our project.

Actual Behavior:
This error is thrown when building the project.

Error AD0001	Analyzer 'Roslynator.CSharp.Analysis.InvocationExpressionAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Object is not initialized.'.
Exception occurred with following context:
SyntaxNode: sb.AppendLine() [InvocationExpressionSyntax]@[5364..5379) (134,16)-(134,31)

System.InvalidOperationException: Object is not initialized.
   at Roslynator.CSharp.Syntax.SimpleMemberInvocationExpressionInfo.get_Name()
   at Roslynator.CSharp.Syntax.SimpleMemberInvocationExpressionInfo.get_NameText()
   at Roslynator.CSharp.Analysis.OptimizeStringBuilderAppendCallAnalysis.Analyze(SyntaxNodeAnalysisContext context, SimpleMemberInvocationExpressionInfo& invocationInfo)
   at Roslynator.CSharp.Analysis.InvocationExpressionAnalyzer.AnalyzeInvocationExpression(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__49`1.<ExecuteSyntaxNodeAction>b__49_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)
-----

Suppress the following diagnostics to disable this analyzer: RCS1015, RCS1068, RCS1077, RCS1080, RCS1097, RCS1107, RCS1112, RCS1112FadeOut, RCS1128, RCS1151, RCS1155, RCS1186, RCS1197, RCS1198, RCS1200, RCS1201, RCS1202, RCS1235, RCS1246

From the following code.

StringBuilder sb = new();
if (bPrintWarnings && HasWarning) { <------ Line 134
    sb.AppendLine(); <------
    sb.AppendFormat(CultureInfo.InvariantCulture, "{0} configuration warning{1} found", m_warnings.Count, m_warnings.Count > 1 ? "s" : string.Empty).AppendLine();
    sb.AppendLine(ToStringSeparator);
    foreach (ConfigurationWarning warning in m_warnings) {
        sb.AppendLine(warning.ToString());
        sb.AppendLine(ToStringSeparator);
    }
}

Expected Behavior:
No error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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