Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove always true "if" and unreachable code in System.Xml.Linq.XObject.SkipNotify method. #13863

Closed
andi9310 opened this issue Nov 12, 2014 · 5 comments · Fixed by dotnet/corefx#1606
Assignees
Milestone

Comments

@andi9310
Copy link
Contributor

This: if (o.Annotations() != null)
is always true because the above while can finish only in two conditions: when o != null or when o.annotations == null. The first condition will be catched by " if (o == null)" and if the second one is true, "o.Annotations() != null" will also always be true i think.

@davkean
Copy link
Member

davkean commented Nov 22, 2014

As mentioned on this pull request dotnet/corefx#55, we'd like to see some test coverage proving the bug, before we fix the bug.

@carlos-sarmiento
Copy link

I want to fix this issue. Regarding @davkean comment in dotnet/corefx#55 I found out the following:

If we check the C# 5.0 Language Specification, section 10.14.4 states (in part):
"When a function member returning an enumerator interface type is implemented using an iterator block, invoking the function member does not immediately execute the code in the iterator block. Instead, an enumerator object is created and returned. "

Then, by design, a call to a function defining an iterator block (such as yield) returns an enumerator object whose MoveNext method implements the function that uses the yield statement. As such, we have a guarantee that it is impossible for o.Annotations() to be null, since in ALL cases, we will receive an object.

So it is impossible to create a test that will use that part of the code, since it is impossible for the method to return null. At the same time, we know for a fact that the method will always return an object and the check is redundant.

@IDisposable
Copy link
Contributor

Carlos's analysis is correct, you cannot write a test to "expose a bug" that can never occur. Rather, this is simple code-path analysis that calls out dead code.

@davkean
Copy link
Member

davkean commented Jan 26, 2015

Sorry, I should clarify - I agree with the assessment of that the code is dead. However, I do not agree to the removal of said dead code, I would rather see the code changed to follow the spirit of which it was written (which appears to be to raise some Changing/Changed events).

ie My statement is, write some tests that show that these Changing/Changed events do not fire when they are supposed to, and then change the code to make them fire.

@sharwell
Copy link
Member

While pull request dotnet/corefx#55 is a correct interpretation of the original code, I believe the original code did not behave as intended. I believe the original code was meant to read as follows:

if (o.Annotations<XObjectChangeAnnotation>().Any()) return false;

If the condition was written like this, the meaning of the method would be:

Skip notify unless the current object or any of its parents includes the XObjectChangeAnnotation annotation.

As it was actually written, and as dotnet/corefx#55 preserved, the meaning of the method is:

Skip notify unless the current object or any of its parents includes any annotation.

alexandrnikitin referenced this issue in alexandrnikitin/corefx Feb 2, 2015
#54 (Remove always true "if" and unreachable code in System.Xml.Linq.XObject.SkipNotify method.)
Tests fails because XObject.Annotations<T>() returns an enumerator object which is always not null.
alexandrnikitin referenced this issue in alexandrnikitin/corefx Feb 2, 2015
#54 (Remove always true "if" and unreachable code in System.Xml.Linq.XObject.SkipNotify method.)
Uses XObject.Annotation<T>() method to check XObjectChangeAnnotation is present in XObject.annotations list
alexandrnikitin referenced this issue in alexandrnikitin/corefx May 1, 2015
#54 (Remove always true "if" and unreachable code in System.Xml.Linq.XObject.SkipNotify method.)
Uses XObject.Annotation<T>() method to check XObjectChangeAnnotation is present in XObject.annotations list
stephentoub referenced this issue in dotnet/corefx May 7, 2015
…tify-Fix

Fixes #54 (Remove always true "if" and unreachable code in System.Xml.Linq.XObject.SkipNotify method.)
@karelz karelz assigned karelz and alexandrnikitin and unassigned krwq and karelz Oct 3, 2016
luhenry referenced this issue in luhenry/corefx May 24, 2018
Restore `HttpConnectionResponseContent` from `release/2.1`.
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 1.0.0-rtm milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.