How to fix unreachable code dedected warning

131 views Asked by At

dbLinq XMlMappingSource.cs contains code:

    public void ReadEmptyContent(XmlReader r, string name)
    {
        if (r.IsEmptyElement)
            r.ReadStartElement(name, DbmlNamespace);
        else
        {
            r.ReadStartElement(name, DbmlNamespace);
            for (r.MoveToContent(); r.NodeType != XmlNodeType.EndElement; r.MoveToContent())
            {
                if (r.NamespaceURI != DbmlNamespace)
                    r.Skip();
                throw UnexpectedItemError(r);
            }
            r.ReadEndElement();
        }
    }

This causes compile warning

Warning CS0162 Unreachable code detected

at line

            for (r.MoveToContent(); r.NodeType != XmlNodeType.EndElement; r.MoveToContent())

( https://github.com/DbLinq/dblinq2007/blob/d7a05bb452b98fd24bca5693da01ecfecd4f3d40/src/DbLinq/Data/Linq/Mapping/XmlMappingSource.cs#L176 )

in third part of for clause r.MoveToContent() It looks like normal node traversal code and third part of for is reached.

How to fix this ? Using .NET 4

2

There are 2 answers

0
Darrell On BEST ANSWER

As Rawling stated you are throwing the exception no matter what.

Try changing the loop to this:

for (r.MoveToContent(); r.NodeType != XmlNodeType.EndElement; r.MoveToContent())
            {
                if (r.NamespaceURI != DbmlNamespace)
                    r.Skip();
                else
                    throw UnexpectedItemError(r);
            }

This will fix your issue.

0
Rawling On

You never execute the increment step, because your first run through the loop always throws an exception.