StyleCopAnalyzers: SA1313: Parameter '__' must begin with lower-case letter.

The following code snippet is OK in StyleCop 4.6, but not in StyleCopAnalyzers:

Action<object, object> b = (_, __) => { };

The warning output is warning SA1313: Parameter '__' must begin with lower-case letter.

StyleCopAnalyzers seems to be OK with a single underscore, but not two underscores. I assume this is due to https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1313ParameterNamesMustBeginWithLowerCaseLetter.cs#L86 explicitly checking for a single underscore.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 22 (11 by maintainers)

Most upvoted comments

To add my two cents to this.

WPF has a very common type RoutedEventHandler so seeing (_,__) => {...} when responding to events is rather normal.

While it would be better if C# supported _ as a syntax for ignored parameters (like other languages). Using multiple underscore to communicate parameters as being unused is still better than using comments to communicate unuse. It also falls into line with alot of the teaching out there for functional programming.

The stable 1.1.118 fixes this, and is making my code better in new ways.

The PR to fix was only released with 1.1.0-beta009.

Try using the latest 1.1.118 build.

I’m getting this again

SA1313 Parameter ‘__’ must begin with lower-case letter

in 1.0.2

@vweijsters Sure. And if discards aren’t supported by then, we can consider allowing a third underscore in June 2021.

@GregReddick The underscores aren’t completely horrible when used as intended - in other words, not used at all. I agree, we shouldn’t say how to use them - just let people use _ (or __) to mean “unused variable”.

I’d suggest they ought to be sequential (i.e. ___ can’t come after _ unless __ is between them), and only allowed if they’re unused, perhaps as separate rules. If you have a lambda with more than 3 or 4 unused arguments, I’d say you have bigger problems than just style.