Skip to content

Commit a81a66a

Browse files
authored
Merge pull request #2758 from jnm2/permit_double_underscore_lambda_param
Allow double underscore as lambda parameter name
2 parents b5c4faa + a99043a commit a81a66a

4 files changed

Lines changed: 66 additions & 9 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1313UnitTests.cs

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ public void MethodName()
401401
}
402402

403403
[Fact]
404-
[WorkItem(1343, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1343")]
405-
public async Task TestLambdaParameterMultipleUnderscoresAsync()
404+
[WorkItem(1606, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1606")]
405+
public async Task TestLambdaParameterNamedDoubleUnderscoreAsync()
406406
{
407407
var testCode = @"public class TypeName
408408
{
@@ -414,11 +414,49 @@ public void MethodName()
414414
}
415415
}";
416416

417+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
418+
}
419+
420+
/// <summary>
421+
/// Verifies this diagnostic does not check whether or not a parameter named <c>__</c> is being used.
422+
/// </summary>
423+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
424+
[Fact]
425+
[WorkItem(1606, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1606")]
426+
public async Task TestLambdaParameterNamedDoubleUnderscoreUsageAsync()
427+
{
428+
var testCode = @"public class TypeName
429+
{
430+
public void MethodName()
431+
{
432+
System.Func<int, int> function1 = __ => __;
433+
System.Func<int, int> function2 = (__) => __;
434+
System.Func<int, int> function3 = delegate(int __) { return __; };
435+
}
436+
}";
437+
438+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
439+
}
440+
441+
[Fact]
442+
[WorkItem(1343, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1343")]
443+
public async Task TestLambdaParameterWithThreeUnderscoresAsync()
444+
{
445+
var testCode = @"public class TypeName
446+
{
447+
public void MethodName()
448+
{
449+
System.Action<int> action1 = ___ => { };
450+
System.Action<int> action2 = (___) => { };
451+
System.Action<int> action3 = delegate(int ___) { };
452+
}
453+
}";
454+
417455
DiagnosticResult[] expected =
418456
{
419-
this.CSharpDiagnostic().WithArguments("__").WithLocation(5, 38),
420-
this.CSharpDiagnostic().WithArguments("__").WithLocation(6, 39),
421-
this.CSharpDiagnostic().WithArguments("__").WithLocation(7, 51),
457+
this.CSharpDiagnostic().WithArguments("___").WithLocation(5, 38),
458+
this.CSharpDiagnostic().WithArguments("___").WithLocation(6, 39),
459+
this.CSharpDiagnostic().WithArguments("___").WithLocation(7, 51),
422460
};
423461
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
424462
await this.VerifyCSharpFixAsync(testCode, testCode).ConfigureAwait(false);
@@ -439,6 +477,21 @@ public void MethodName(int _)
439477
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
440478
}
441479

480+
[Fact]
481+
[WorkItem(1606, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1606")]
482+
public async Task TestMethodParameterNamedDoubleUnderscoreAsync()
483+
{
484+
var testCode = @"public class TypeName
485+
{
486+
public void MethodName(int __)
487+
{
488+
}
489+
}";
490+
491+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("__").WithLocation(3, 32);
492+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
493+
}
494+
442495
[Fact]
443496
[WorkItem(1529, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1529")]
444497
public async Task TestInheritedInterfacesWithOverloadedMembersAsync()

StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1313ParameterNamesMustBeginWithLowerCaseLetter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ namespace StyleCop.Analyzers.NamingRules
1717
/// <remarks>
1818
/// <para>A violation of this rule occurs when the name of a parameter does not begin with a lower-case letter.</para>
1919
///
20-
/// <para>An exception to this rule is made for lambda parameters named <c>_</c>. These parameters are often used to
21-
/// designate a placeholder parameter which is not actually used in the body of the lambda expression.</para>
20+
/// <para>An exception to this rule is made for lambda parameters named <c>_</c> and <c>__</c>. These parameters are
21+
/// often used to designate a placeholder parameter which is not actually used in the body of the lambda expression.</para>
2222
///
2323
/// <para>If the parameter name is intended to match the name of an item associated with Win32 or COM, and thus
2424
/// needs to begin with an upper-case letter, place the parameter within a special <c>NativeMethods</c> class. A
@@ -76,7 +76,7 @@ private static void HandleParameter(SyntaxNodeAnalysisContext context)
7676
return;
7777
}
7878

79-
if (name == "_" && IsInLambda(syntax))
79+
if ((name == "_" || name == "__") && IsInLambda(syntax))
8080
{
8181
return;
8282
}

documentation/KnownChanges.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ This rule is disabled by default in StyleCop Analyzers, but can be enabled by us
199199
:warning: StyleCop Analyzers does not report SA1305 for parameters in overriding methods and methods which implement an
200200
interface. StyleCop Classic reported SA1305 for all methods.
201201

202+
### SA1313
203+
204+
StyleCop Analyzers allows the single and double underscore (`_` and `__`) as lambda parameter names.
205+
202206
## Maintainability Rules
203207

204208
There are no known changes at this time.

documentation/SA1313.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The name of a parameter in C# does not begin with a lower-case letter.
2525

2626
A violation of this rule occurs when the name of a parameter does not begin with a lower-case letter.
2727

28-
An exception to this rule is made for lambda parameters named `_`. These parameters are often used to designate a
28+
An exception to this rule is made for lambda parameters named `_` and `__`. These parameters are often used to designate a
2929
placeholder parameter which is not actually used in the body of the lambda expression.
3030

3131
If the parameter name is intended to match the name of an item associated with Win32 or COM, and thus needs to begin

0 commit comments

Comments
 (0)