{"action":"closed","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/18776","repository_url":"https://api.github.com/repos/dotnet/roslyn","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/18776/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/18776/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/18776/events","html_url":"https://github.com/dotnet/roslyn/issues/18776","id":222351128,"number":18776,"title":"IDE0018 false positive *causes crash*","user":{"login":"ljw1004","id":3316258,"avatar_url":"https://avatars1.githubusercontent.com/u/3316258?v=3","gravatar_id":"","url":"https://api.github.com/users/ljw1004","html_url":"https://github.com/ljw1004","followers_url":"https://api.github.com/users/ljw1004/followers","following_url":"https://api.github.com/users/ljw1004/following{/other_user}","gists_url":"https://api.github.com/users/ljw1004/gists{/gist_id}","starred_url":"https://api.github.com/users/ljw1004/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ljw1004/subscriptions","organizations_url":"https://api.github.com/users/ljw1004/orgs","repos_url":"https://api.github.com/users/ljw1004/repos","events_url":"https://api.github.com/users/ljw1004/events{/privacy}","received_events_url":"https://api.github.com/users/ljw1004/received_events","type":"User","site_admin":false},"labels":[{"id":166377917,"url":"https://api.github.com/repos/dotnet/roslyn/labels/Area-IDE","name":"Area-IDE","color":"5319e7","default":false},{"id":165001069,"url":"https://api.github.com/repos/dotnet/roslyn/labels/Bug","name":"Bug","color":"fc2929","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-04-18T09:15:05Z","updated_at":"2017-04-18T16:02:55Z","closed_at":"2017-04-18T16:02:55Z","body":"[Note: I see there are several bugs relating to IDE0018, some resolved as already-fixed, some marked as feature-requests. None of them report an actual crash, so I think this is a new issue.]\r\n\r\n[More generally, if feels like I've encountered lots of MS-authored analyzers crashing because of incorrect casts. These crashes are very user-visible, and probably the most common crashes that a user of Visual Studio will see. Why are there so many casts? And why so many crashes because of them? Maybe every single cast has to be treated with substantially more skepticism during code review? Or accompanied with a proof that it's valid? Or accompanied with fuzz-testing from an automatic syntax tree generator?]\r\n\r\n**Version Used**: 15.1 (26403.03 Release)\r\n\r\n**Steps to Reproduce**:\r\n\r\nFollowing console app:\r\n\r\n```\r\nclass Program {\r\n  static void Main() {\r\n    int count = 0;\r\n    foreach (var x in Gen(out count)) System.Console.WriteLine(x);\r\n    System.Console.WriteLine(count);\r\n  }\r\n\r\n  static int[] Gen(out int count) {\r\n    count = 2;\r\n    return new[] { 0, 1 };\r\n  }\r\n}\r\n```\r\n\r\n**Expected Behavior**:\r\n\r\nIt should not offer IDE0018 \"variable declaration of count can be inlined\". (That's because inlining it would limit its scope too much).\r\n\r\n**Actual Behavior**:\r\n\r\nIt offers IDE0018. Applying the fix results in a crash inside CSharpInlineDeclarationCodeFixProvider:\r\n\r\n```\r\nSystem.InvalidCastException : Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.BoundBlock' to type 'Microsoft.CodeAnalysis.CSharp.BoundForEachStatement'.\r\n   at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetForEachStatementInfo(CommonForEachStatementSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetForEachStatementInfo(CommonForEachStatementSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetForEachStatementInfo(SemanticModel semanticModel,CommonForEachStatementSyntax forEachStatement)\r\n   at Microsoft.CodeAnalysis.CSharp.Utilities.SpeculationAnalyzer.GetForEachSymbols(SemanticModel model,CommonForEachStatementSyntax forEach,IMethodSymbol& getEnumeratorMethod,ITypeSymbol& elementType)\r\n   at Microsoft.CodeAnalysis.Shared.Utilities.AbstractSpeculationAnalyzer`9.ReplacementBreaksForEachStatement(TForEachStatementSyntax forEachStatement,TForEachStatementSyntax newForEachStatement)\r\n   at Microsoft.CodeAnalysis.Shared.Utilities.AbstractSpeculationAnalyzer`9.ReplacementChangesSemanticsForNode(TSyntaxNode currentOriginalNode,TSyntaxNode currentReplacedNode,TSyntaxNode previousOriginalNode,TSyntaxNode previousReplacedNode)\r\n   at Microsoft.CodeAnalysis.Shared.Utilities.AbstractSpeculationAnalyzer`9.ReplacementChangesSemantics(TSyntaxNode currentOriginalNode,TSyntaxNode currentReplacedNode,TSyntaxNode originalRoot,Boolean skipVerificationForCurrentNode)\r\n   at Microsoft.CodeAnalysis.Shared.Utilities.AbstractSpeculationAnalyzer`9.ReplacementChangesSemantics()\r\n   at Microsoft.CodeAnalysis.CSharp.Extensions.ExpressionSyntaxExtensions.CanReplaceWithReducedName(NameSyntax name,TypeSyntax reducedName,SemanticModel semanticModel,CancellationToken cancellationToken)\r\n   at Microsoft.CodeAnalysis.CSharp.Extensions.ExpressionSyntaxExtensions.TryReduce(NameSyntax name,SemanticModel semanticModel,TypeSyntax& replacementNode,TextSpan& issueSpan,OptionSet optionSet,CancellationToken cancellationToken)\r\n   at Microsoft.CodeAnalysis.CSharp.Extensions.ExpressionSyntaxExtensions.TryReduceExplicitName(ExpressionSyntax expression,SemanticModel semanticModel,TypeSyntax& replacementNode,TextSpan& issueSpan,OptionSet optionSet,CancellationToken cancellationToken)\r\n   at Microsoft.CodeAnalysis.CSharp.Extensions.ExpressionSyntaxExtensions.TryReduceOrSimplifyExplicitName(ExpressionSyntax expression,SemanticModel semanticModel,ExpressionSyntax& replacementNode,TextSpan& issueSpan,OptionSet optionSet,CancellationToken cancellationToken)\r\n   at Microsoft.CodeAnalysis.CSharp.Simplification.CSharpNameReducer.SimplifyName(SyntaxNode node,SemanticModel semanticModel,OptionSet optionSet,CancellationToken cancellationToken)\r\n   at Microsoft.CodeAnalysis.CSharp.Simplification.AbstractCSharpReducer.AbstractExpressionRewriter.SimplifyNode[TNode](TNode node,SyntaxNode newNode,SyntaxNode parentNode,Func`5 simplifier)\r\n   at Microsoft.CodeAnalysis.CSharp.Simplification.AbstractCSharpReducer.AbstractExpressionRewriter.SimplifyExpression[TExpression](TExpression expression,SyntaxNode newNode,Func`5 simplifier)\r\n   at Microsoft.CodeAnalysis.CSharp.Simplification.CSharpNameReducer.Rewriter.VisitAliasQualifiedName(AliasQualifiedNameSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.Syntax.AliasQualifiedNameSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitQualifiedName(QualifiedNameSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.Simplification.CSharpNameReducer.Rewriter.VisitQualifiedName(QualifiedNameSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.Syntax.QualifiedNameSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitDeclarationExpression(DeclarationExpressionSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.Syntax.DeclarationExpressionSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitArgument(ArgumentSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.Syntax.ArgumentSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitListElement[TNode](TNode node)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitList[TNode](SeparatedSyntaxList`1 list)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitArgumentList(ArgumentListSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.Syntax.ArgumentListSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)\r\n   at Microsoft.CodeAnalysis.CSharp.Simplification.AbstractCSharpReducer.AbstractExpressionRewriter.VisitInvocationExpression(InvocationExpressionSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.Syntax.InvocationExpressionSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitForEachStatement(ForEachStatementSyntax node)\r\n   at Microsoft.CodeAnalysis.CSharp.Syntax.ForEachStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node)\r\n   at Microsoft.CodeAnalysis.CSharp.Simplification.AbstractCSharpReducer.AbstractExpressionRewriter.VisitNodeOrToken(SyntaxNodeOrToken nodeOrToken,SemanticModel semanticModel,Boolean simplifyAllDescendants)\r\n   at async Microsoft.CodeAnalysis.Simplification.AbstractSimplificationService`3.<>c__DisplayClass10_0.<ReduceAsync>b__0[TExpressionSyntax,TStatementSyntax,TCrefSyntax](<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.Simplification.AbstractSimplificationService`3.ReduceAsyncInternal[TExpressionSyntax,TStatementSyntax,TCrefSyntax](<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.Simplification.AbstractSimplificationService`3.ReduceAsync[TExpressionSyntax,TStatementSyntax,TCrefSyntax](<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.CleanupDocumentAsync(<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.PostProcessChangesAsync(<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.PostProcessAsync(<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)\r\n   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)\r\n   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)\r\n```"}}
{"actor":{"display_login":"Pilchie"}}