/// <summary> /// The DataFlowAnalysis API is fairly limited. It just provides a StartStatement and EndStatement. /// However, an "if" statement includes the entire Block that comes after the if clause. This is /// insufficient granularity. Consider the following example: /// y = x as yType; /// if (blah) /// { /// -optional code here- /// y.ToString(); /// We need to pass to DataFlowAnalysis a statement that is before y.ToString() for the endStatement. /// But if the firstStatement is the if clause, it includes the entire if block, of which endStatement is inside of. /// This causes DataFlowAnalysis to throw an exception. /// Stated differently (and more problematically), DataFlowAnalysis only works on statements in the same statementlist, ie the same block. /// https://github.com/kislyuk/roslyn/blob/89169a3380e48fc834c72e38355867881d030e94/Src/Compilers/CSharp/Source/Compilation/SyntaxTreeSemanticModel.cs#L1902 /// /// Fortunately, from a practical perspective, it is likely a common scenario that in this case, the reason it is in a nested block is precisely because /// it checked for null. /// </summary> /// <param name="blockOfInterest"></param> /// <param name="node"></param> /// <returns></returns> private bool SameBlock(BlockSyntax blockOfInterest, SyntaxNode node) { BlockSyntax ourSymbolsBlock = node.Ancestors().OfType <BlockSyntax>().First(); return(ourSymbolsBlock.IsEquivalentTo(blockOfInterest)); }