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

Usage of _ in methods arguments raises SA1313 #2599

Closed
Onzi12 opened this issue Jan 22, 2018 · 8 comments
Closed

Usage of _ in methods arguments raises SA1313 #2599

Onzi12 opened this issue Jan 22, 2018 · 8 comments

Comments

@Onzi12
Copy link

Onzi12 commented Jan 22, 2018

Usage of _ in method arguments raises SA1313.

Example:

public override void Foo( int arg, int _ )
{
...
}

Can you please make an exception to SA1313, to support '_' in method arguments?

@Onzi12 Onzi12 changed the title SA1313 usage of _ in methods arguments Usage of _ in methods arguments raises SA1313 Jan 22, 2018
@brinko99
Copy link

I agree. As is, an exception is made for lambda expressions...

In fact, I would request that any number of ignored parameters be supported in this way. This supports the cases where multiple parameters are to be ignored.

e.g. MyProperty.PropertyChanged += (_, __) => ...

@sharwell
Copy link
Member

I'm not sure I agree with this one. Plus, it's going to conflict with #1949.

@brinko99 Your case is a duplicate of #1606.

@Onzi12
Copy link
Author

Onzi12 commented Jan 25, 2018

Why are you not sure you agree?
The purpose of the _ is to acknowledge the arg is not in use.
Without this enhancement, users can use this feature ONLY in lambda handlers.

About the conflict with #1949 I agree. Implementing this should include an exception to #1949.

@sharwell
Copy link
Member

Closing this one as won't fix. The recommended solution for method signatures when a parameter is intentionally not used is:

  1. Give the parameter a meaningful name. This should match the original definition when applicable (see Implement SA1315: ParametersShouldMatchInheritedNames #1949).
  2. Assign the parameter to a discard at the start of the method to clearly indicate to the reader that the parameter is intentionally unused.

For example:

public void Method(T value)
{
  _ = value;
}

@sharwell sharwell self-assigned this Nov 17, 2020
@tutike2000
Copy link

Why close this as won't fix if so many people want it fixed?

@sharwell
Copy link
Member

sharwell commented Feb 7, 2022

@tutike2000 There is no requirement that a project enable SA1313 or use it without modification. Options for resolving this externally include:

  • SA1313 can be disabled, and a new rule implemented separately that is used instead. The new rule can be based on SA1313 and even start with its code under the MIT license.
  • SA1313 can be left enabled, and a DiagnosticSuppressor implemented separately that prevents SA1313 from reporting in the location described by this issue. I faced a similar issue with VSTHRD200 and resolved it by writing RelaxTestNamingSuppressor.

@palenshus
Copy link

palenshus commented Nov 15, 2022

Currently IDE0060 suggests that we do in fact rename unused parameters to _:

IDE0060 Remove unused parameter 'sender' if it is not part of a shipped public
Avoid unused parameters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.

However, if we follow that advice, given by the IDE, then we run into this issue. That doesn't seem like a good thing to Won't Fix

@Lowlet
Copy link

Lowlet commented Oct 26, 2024

This should be fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants