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

Replaces the single-iteration foreach loops #48

Merged
merged 4 commits into from
Sep 28, 2020
Merged

Replaces the single-iteration foreach loops #48

merged 4 commits into from
Sep 28, 2020

Conversation

scottdorman
Copy link
Contributor

@scottdorman scottdorman commented Sep 27, 2020

This will close #47.

@codecov-commenter
Copy link

codecov-commenter commented Sep 27, 2020

Codecov Report

Merging #48 into master will increase coverage by 2.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #48      +/-   ##
==========================================
+ Coverage   88.88%   90.90%   +2.02%     
==========================================
  Files           9        9              
  Lines         279      275       -4     
==========================================
+ Hits          248      250       +2     
+ Misses         31       25       -6     
Impacted Files Coverage Δ
src/Validation/Requires.cs 96.29% <100.00%> (+5.22%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a6a5350...aa3d15a. Read the comment docs.

@scottdorman
Copy link
Contributor Author

Not sure how you want to handle the codecov issue. As far as I can tell, it's saying that the coverage went down because there were lines of code removed. When I look at the diff, it looks like the new lines are being fully covered by tests.

Copy link
Owner

@AArnott AArnott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Just one change and we can merge.

}

if (!hasElements)
if (!values.GetEnumerator().MoveNext())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code would try casting the IEnumerator to IDisposable and dispose it if it did. It would do this in a try/finally to ensure it happened. We lost that in this method. Can you add it back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make this change, but I double-checked the code and didn't see anywhere that had it wrapped.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean. The try finally behavior I'm referring to comes automatically from the compiler by virtue of the foreach loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL. Oh! I thought you mean that it was explicitly wrapped in a try..finally. 🤦

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think to wrap it initially because IEnumerator doesn't implement IDisposable, although IEnumerator<T> does.

@scottdorman
Copy link
Contributor Author

Ok. Definitely not sure what you want to do about the codecov/patch failure here. It looks like it's complaining because the new call to dispose of the enumerator isn't being covered by a unit test. I can try and fabricate a unit test that will hit if you want me to though.

@scottdorman
Copy link
Contributor Author

I didn't mean to push that last commit (87e25c5) into this PR. :( Should I revert it?

@AArnott
Copy link
Owner

AArnott commented Sep 28, 2020

I didn't mean to push that last commit (87e25c5) into this PR. :( Should I revert it?

I wouldn't push a revert commit, but rather force-push the rollback so it's as if it never happened.

Comment on lines 201 to 207
finally
{
throw new ArgumentException(Format(Strings.Argument_EmptyArray, parameterName), parameterName);
if (enumerator is IDisposable disposable)
{
disposable.Dispose();
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tip: a more succinct syntax for try/finally based disposal in this case can be achieved like this:

IEnumerator enumerator = values.GetEnumerator();
using (enumerator as IDisposable)
{
    if (!values.GetEnumerator().MoveNext())
    {
        throw new ArgumentException(Format(Strings.Argument_EmptyArray, parameterName), parameterName);
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That gets the enumerator twice though. Wouldn't we want to avoid that?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops. Well, that bug was already in your code. :) I just copied it in. Yes, let's avoid calling GetEnumerator() twice by actually using your enumerator local.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoot! You're right. It was. ☹️ I missed that when I changed it to the try...finally.

Copy link
Owner

@AArnott AArnott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AArnott AArnott merged commit bb6a8e9 into AArnott:master Sep 28, 2020
AArnott added a commit that referenced this pull request May 2, 2023
Add -y switch to choco install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace the single-iteration foreach loops
3 participants