Fix CS0642 - Possible mistaken empty statement #31740
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
About the PR
This took me a minute to disambiguate so I'll recount the tale here.
This is the original statement:
This statement throws a warning on the ending semicolon:
CS0642 Possible mistaken empty statement
. What it's trying to tell us is that the above statement actually becomes:What the code is actually trying to do is use a variant of the using statement that creates an implied
using
scope that is equal to the (rest of) the function scope (i.e. theDispose()
is called just before the function returns). That variant of theusing
statement must explicitly assign to a variable or else the using statement is actually treated like ausing
statement with a single-line code block following it (i.e. the example in the code example above).So we need to explicitly assign to something, but we don't actually need the result of
PushGroup
so we can just assign to the discard value.This properly sets the scope of the
using
statement to the whole function and clears the warning.I think C# probably has a bit too much syntax these days, but maybe I'm just old.
Requirements