Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception in Microsoft.CodeAnalysis.CSharp.UseCollectionInitializer.CSharpUseCollectionInitializerDiagnosticAnalyzer #75214

Closed
Akridian opened this issue Sep 24, 2024 · 0 comments · Fixed by #75242
Labels
Area-Analyzers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@Akridian
Copy link

Akridian commented Sep 24, 2024

Version Used:

SDK Version: 8.0.400

Visual Studio: 17.11.4

TargetFramework: net8.0

AnalysisLevel: latest-recommended

Steps to Reproduce:

We faced this issue in our work project and I reproduced it in test project to submit here.

AnalyzerErrorTest.zip

  1. Setup project with EnforceCodeStyleInBuild, TreatWarningsAsErrors and latest-recommended AnalysisLevel.
  2. Use code like:
List<int>? list1 = null;

foreach (var (value, sort) in (list1 ?? new List<int>()).Select((val, i) => (val, i)))
{

}
  1. Build project.
  2. Face error:
CSC : error AD0001: Analyzer 'Microsoft.CodeAnalysis.CSharp.UseCollectionInitializer.CSharpUseCollectionInitializerDiagnosticAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Operation is not valid due to the current state of the object.'.

If we to build project with dotnet build -v:d, we can see full error stake trace:

1:7>CSC : error AD0001: Analyzer 'Microsoft.CodeAnalysis.CSharp.UseCollectionInitializer.CSharpUseCollectionInitializerDiagnosticAnalyzer' threw an exception of type 'System.InvalidOperationException' with messa
       ge 'Operation is not valid due to the current state of the object.'. [C:\_work\AnalyzerErrorTest\AnalyzerErrorTest\AnalyzerErrorTest.csproj]
         Exception occurred with following context:
         Compilation: AnalyzerErrorTest
         SyntaxTree: C:\_work\AnalyzerErrorTest\AnalyzerErrorTest\Program.cs
         SyntaxNode: new List<int>() [ObjectCreationExpressionSyntax]@[68..83) (2,40)-(2,55)
         System.InvalidOperationException: Operation is not valid due to the current state of the object.
         at Microsoft.CodeAnalysis.Shared.Utilities.AbstractSpeculationAnalyzer`8.ReplacementBreaksForEachStatement(TForEachStatementSyntax forEachStatement, TForEachStatementSyntax newForEachStatement)
         at Microsoft.CodeAnalysis.Shared.Utilities.AbstractSpeculationAnalyzer`8.ReplacementChangesSemanticsForNode(SyntaxNode currentOriginalNode, SyntaxNode currentReplacedNode, SyntaxNode previousOriginalNode,
         SyntaxNode previousReplacedNode)
         at Microsoft.CodeAnalysis.Shared.Utilities.AbstractSpeculationAnalyzer`8.ReplacementChangesSemantics(SyntaxNode currentOriginalNode, SyntaxNode currentReplacedNode, SyntaxNode originalRoot, Boolean skipVer
         ificationForCurrentNode)
         at Microsoft.CodeAnalysis.Shared.Utilities.AbstractSpeculationAnalyzer`8.ReplacementChangesSemantics()
         at Microsoft.CodeAnalysis.CSharp.UseCollectionExpression.UseCollectionExpressionHelpers.CanReplaceWithCollectionExpression(SemanticModel semanticModel, ExpressionSyntax expression, CollectionExpressionSynt
         ax replacementExpression, INamedTypeSymbol expressionType, Boolean isSingletonInstance, Boolean allowSemanticsChange, Boolean skipVerificationForReplacedNode, CancellationToken cancellationToken, Boolean&
         changesSemantics)
         at Microsoft.CodeAnalysis.CSharp.UseCollectionInitializer.CSharpUseCollectionInitializerDiagnosticAnalyzer.CanUseCollectionExpression(SemanticModel semanticModel, BaseObjectCreationExpressionSyntax objectC
         reationExpression, INamedTypeSymbol expressionType, Boolean allowSemanticsChange, CancellationToken cancellationToken, Boolean& changesSemantics)
         at Microsoft.CodeAnalysis.UseCollectionInitializer.AbstractUseCollectionInitializerDiagnosticAnalyzer`10.<AnalyzeNode>g__GetCollectionExpressionMatches|12_2(<>c__DisplayClass12_0&)
         at Microsoft.CodeAnalysis.UseCollectionInitializer.AbstractUseCollectionInitializerDiagnosticAnalyzer`10.AnalyzeNode(SyntaxNodeAnalysisContext context, INamedTypeSymbol ienumerableType, INamedTypeSymbol ex
         pressionType)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__52`1.<ExecuteSyntaxNodeAction>b__52_0(ValueTuple`2 data)
         at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToke
         n)
         -----
         Suppress the following diagnostics to disable this analyzer: IDE0028
         CompilerServer: server - server processed compilation - AnalyzerErrorTest (net8.0)
       Done executing task "Csc" -- FAILED.

Error disappears if we change code to use collection expression:

List<int>? list1 = null;

foreach (var (value, sort) in (list1 ?? []).Select((val, i) => (val, i)))
{

}

Diagnostic Id:

Use collection initializers or expressions (IDE0028).

Expected Behavior:

Code style suggestion.

Actual Behavior:

Exception during build.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Analyzers untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Analyzers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant