Search code examples
c#.netc#-4.0dblinq

How to fix unreachable code dedected warning


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


Solution

  • 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.