You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
EnforceCodeStyleInBuild
,TreatWarningsAsErrors
andlatest-recommended
AnalysisLevel
.If we to build project with
dotnet build -v:d
, we can see full error stake trace:Error disappears if we change code to use collection expression:
Diagnostic Id:
Use collection initializers or expressions (IDE0028).
Expected Behavior:
Code style suggestion.
Actual Behavior:
Exception during build.
The text was updated successfully, but these errors were encountered: