roslyn: 'Microsoft.CodeAnalysis.CSharp.Diagnostics.AddBraces.CSharpAddBracesDiagnosticAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Unexpected true'

Version Used:

VS 2019 preview 3

Warning	AD0001	Analyzer 'Microsoft.CodeAnalysis.CSharp.Diagnostics.AddBraces.CSharpAddBracesDiagnosticAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Unexpected true'.	

Microsoft.CodeAnalysis.CSharp.Diagnostics.AddBraces.CSharpAddBracesDiagnosticAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: SwitchToSource.CLI
SyntaxTree: C:\Users\Documents\Sources\Command.List.cs
SyntaxNode: else System.Console.WriteLine($" ... [ElseClauseSyntax]@[2781..2931) (54,40)-(55,144)

System.InvalidOperationException: Unexpected true
   at Roslyn.Utilities.Contract.ThrowIfTrue(Boolean condition, String message)
   at Microsoft.CodeAnalysis.Shared.Utilities.CommonFormattingHelpers.AppendTextBetween(SyntaxToken token1, SyntaxToken token2, StringBuilder builder)
   at Microsoft.CodeAnalysis.CSharp.Utilities.FormattingRangeHelper.AreTwoTokensOnSameLine(SyntaxToken token1, SyntaxToken token2)
   at Microsoft.CodeAnalysis.CSharp.Diagnostics.AddBraces.CSharpAddBracesDiagnosticAnalyzer.IsConsideredMultiLine(SyntaxNode statement, SyntaxNode embeddedStatement)
   at Microsoft.CodeAnalysis.CSharp.Diagnostics.AddBraces.CSharpAddBracesDiagnosticAnalyzer.AnalyzeNode(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__50`1.<ExecuteSyntaxNodeAction>b__50_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.

The code around the error (The line comments are not in the original code)

          packageToDisplayList.ForEach(listedPackage => // line 48
          {
              System.Console.WriteLine($"- {listedPackage.PackageId}");
              foreach (var versionInfo in listedPackage.Versions)
              {
                  if (versionInfo.Reason == NonSwitchableReason.None)
                      System.Console.WriteLine($"   * {versionInfo.Version} [{versionInfo.State}]"); // line 54
                  else
                      System.Console.WriteLine($"   * {versionInfo.Version} [{versionInfo.State}: {versionInfo.Reason}]");
              }
          });

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for the insight all. So my proposal is that I will create a PR that includes the fix plus a new unit test that covers the AreTwoTokensOnSameLine helper method using a mocked SyntaxNode/Tree that simulates the currently failing case.