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

Proposed exception to SA1312 #2851

Closed
lcsondes opened this issue Jan 7, 2019 · 3 comments
Closed

Proposed exception to SA1312 #2851

lcsondes opened this issue Jan 7, 2019 · 3 comments

Comments

@lcsondes
Copy link

lcsondes commented Jan 7, 2019

Names such as _, __, etc. are commonly used to indicate that they're otherwise ignored, which is now "official" syntax in some instances such as out _.

There are cases where you want to evaluate an expression for its side effects, but are otherwise uninterested in the results, this would be written as var _ = expression;. It's also a relatively common convention to use (_, __) => blah for a lambda that's used as an event handler (sender, args) if you don't want to use the delegate{blah} syntax for ignoring arguments.

I'd like to propose names that are made up of only underscores to not trigger SA1312.

@sharwell
Copy link
Member

sharwell commented Jan 7, 2019

There are cases where you want to evaluate an expression for its side effects, but are otherwise uninterested in the results, this would be written as var _ = expression;.

The new discard form for these is:

_ = expression;

The discard syntax does not trigger SA1312, and can be used any number of times within the same method.

It's also a relatively common convention to use (_, __) => blah for a lambda that's used as an event handler (sender, args) if you don't want to use the delegate{blah} syntax for ignoring arguments.

This is SA1313, and covered by previous issue #1606.

@lcsondes
Copy link
Author

lcsondes commented Jan 7, 2019

I did not know that, suggestion withdrawn.

@lcsondes lcsondes closed this as completed Jan 7, 2019
@colgreen
Copy link

The exception doesn't seem to be working in this case:

await foreach (var _ in sut.GetListAsync()) { }

(seen in a unit test that was exhausting an IAsyncEnumerable without using the enumerated items.)

In this case I just changed it to use a normal variable name <shrugs> :)

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

No branches or pull requests

3 participants