static bool HasConflictingNames(SyntaxNodeAnalysisContext nodeContext, IfStatementSyntax ifElseStatement)
        {
            var block = ifElseStatement.Else.Statement as BlockSyntax;
            if (block == null || block.Statements.Count == 0)
                return false;

            var member = ifElseStatement.Ancestors().FirstOrDefault(a => a is MemberDeclarationSyntax);

            var priorLocalDeclarations = new List<string>();
            foreach (var localDecl in member.DescendantNodes().Where(n => n.SpanStart < ifElseStatement.Else.SpanStart).OfType<LocalDeclarationStatementSyntax>()) {
                foreach (var v in localDecl.Declaration.Variables)
                    priorLocalDeclarations.Add(v.Identifier.ValueText);
            }

            foreach (var sym in block.Statements)
            {
                var decl = sym as LocalDeclarationStatementSyntax;
                if (decl == null)
                    continue;
                
                if (priorLocalDeclarations.Contains(s => decl.Declaration.Variables.Any(v => v.Identifier.ValueText == s)))
                    return true;
            }
            return false;
        }
예제 #2
0
        static bool HasConflictingNames(SyntaxNodeAnalysisContext nodeContext, IfStatementSyntax ifElseStatement)
        {
            var block = ifElseStatement.Else?.Statement as BlockSyntax;

            if (block == null || block.Statements.Count == 0)
            {
                return(false);
            }

            var member = ifElseStatement.Ancestors().FirstOrDefault(a => a is MemberDeclarationSyntax);

            var priorLocalDeclarations = new HashSet <string>();

            foreach (var localDecl in member.DescendantNodes().Where(n => n.SpanStart < ifElseStatement.Else.SpanStart).OfType <LocalDeclarationStatementSyntax>())
            {
                foreach (var v in localDecl.Declaration.Variables)
                {
                    priorLocalDeclarations.Add(v.Identifier.ValueText);
                }
            }

            foreach (var sym in block.Statements)
            {
                var decl = sym as LocalDeclarationStatementSyntax;
                if (decl == null)
                {
                    continue;
                }

                foreach (var variable in decl.Declaration.Variables)
                {
                    if (priorLocalDeclarations.Contains(variable.Identifier.ValueText))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }