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

Fix CS0642 - Possible mistaken empty statement #31740

Merged
merged 1 commit into from
Sep 2, 2024

Conversation

Mervill
Copy link
Member

@Mervill Mervill commented Sep 2, 2024

About the PR

This took me a minute to disambiguate so I'll recount the tale here.

This is the original statement:

using(args.PushGroup(nameof(GasTankComponent)));
if (args.IsInDetailsRange)
   ...

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:

using(args.PushGroup(nameof(GasTankComponent)));
{
    // empty using block!
}
if (args.IsInDetailsRange)
   ...

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. the Dispose() is called just before the function returns). That variant of the using statement must explicitly assign to a variable or else the using statement is actually treated like a using 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

  • I have read and I am following the Pull Request Guidelines. I understand that not doing so may get my pr closed at maintainer’s discretion
  • I have added screenshots/videos to this PR showcasing its changes ingame, or this PR does not require an ingame showcase

@Mervill Mervill requested a review from Partmedia as a code owner September 2, 2024 05:53
@github-actions github-actions bot added the S: Needs Review Status: Requires additional reviews before being fully accepted label Sep 2, 2024
@PJB3005 PJB3005 merged commit 05d29c8 into space-wizards:master Sep 2, 2024
13 checks passed
@Mervill Mervill deleted the CS0642 branch September 2, 2024 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S: Needs Review Status: Requires additional reviews before being fully accepted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants