Skip to content

Commit 763d098

Browse files
authored
Merge pull request #3158 from dymanoid/sa1008-tuple-parameter-attribute
Fix SA1008 for attributed tuple parameters
2 parents 2123c39 + 93f9dce commit 763d098

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1008CSharp7UnitTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,5 +978,65 @@ public void TestMethod(params (string name, string value)[] options)
978978

979979
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
980980
}
981+
982+
[Fact]
983+
[WorkItem(3117, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3117")]
984+
public async Task TestTupleParameterAttributesAsync()
985+
{
986+
var testCode = @"
987+
namespace TestNamespace
988+
{
989+
class NotNullAttribute : System.Attribute { }
990+
class CustomAttribute : System.Attribute { }
991+
992+
class TestClass
993+
{
994+
void TestMethod1([NotNull] (string, string) tuple) { }
995+
996+
void TestMethod2([NotNull, Custom] (string, string) tuple) { }
997+
998+
void TestMethod3([NotNull][Custom] (string, string) tuple) { }
999+
1000+
void TestMethod4([NotNull]{|#0:(|}string, string) tuple) { }
1001+
1002+
void TestMethod5([NotNull, Custom]{|#1:(|}string, string) tuple) { }
1003+
1004+
void TestMethod6([NotNull][Custom]{|#2:(|}string, string) tuple) { }
1005+
}
1006+
}
1007+
";
1008+
1009+
var fixedCode = @"
1010+
namespace TestNamespace
1011+
{
1012+
class NotNullAttribute : System.Attribute { }
1013+
class CustomAttribute : System.Attribute { }
1014+
1015+
class TestClass
1016+
{
1017+
void TestMethod1([NotNull] (string, string) tuple) { }
1018+
1019+
void TestMethod2([NotNull, Custom] (string, string) tuple) { }
1020+
1021+
void TestMethod3([NotNull][Custom] (string, string) tuple) { }
1022+
1023+
void TestMethod4([NotNull] (string, string) tuple) { }
1024+
1025+
void TestMethod5([NotNull, Custom] (string, string) tuple) { }
1026+
1027+
void TestMethod6([NotNull][Custom] (string, string) tuple) { }
1028+
}
1029+
}
1030+
";
1031+
1032+
DiagnosticResult[] expectedDiagnostics =
1033+
{
1034+
Diagnostic(DescriptorPreceded).WithLocation(0),
1035+
Diagnostic(DescriptorPreceded).WithLocation(1),
1036+
Diagnostic(DescriptorPreceded).WithLocation(2),
1037+
};
1038+
1039+
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
1040+
}
9811041
}
9821042
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,11 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
233233
case SyntaxKindEx.TupleType:
234234
// Comma covers tuple types in parameters and nested within other tuple types.
235235
// 'out', 'ref', 'in', 'params' parameters are covered by IsKeywordKind.
236+
// Attributes of parameters are covered by checking the previous token's parent.
236237
// Return types are handled by a helper.
237238
haveLeadingSpace = prevToken.IsKind(SyntaxKind.CommaToken)
238239
|| SyntaxFacts.IsKeywordKind(prevToken.Kind())
240+
|| prevToken.Parent.IsKind(SyntaxKind.AttributeList)
239241
|| ((TypeSyntax)token.Parent).GetContainingNotEnclosingType().IsReturnType();
240242
break;
241243
}

0 commit comments

Comments
 (0)