Skip to content

Commit e7cd0d3

Browse files
committed
Updated after CR
1 parent 21e815f commit e7cd0d3

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1134UnitTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,42 @@ public async Task VerifyMultipleAttributesOnSameLineForAssemblyAsync()
138138
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
139139
}
140140

141+
/// <summary>
142+
/// Verifies that multiple attributes on the same line, directly in front of a namespace declaration will produce the expected diagnostic.
143+
/// </summary>
144+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
145+
[Fact]
146+
public async Task VerifyMultipleAttributesBeforeNamespaceAsync()
147+
{
148+
var testCode = @"using System;
149+
using System.Runtime.InteropServices;
150+
151+
[assembly:CLSCompliant(false)][assembly:ComVisible(true)]
152+
namespace TestNamespace
153+
{
154+
}
155+
";
156+
157+
var fixedTestCode = @"using System;
158+
using System.Runtime.InteropServices;
159+
160+
[assembly:CLSCompliant(false)]
161+
[assembly:ComVisible(true)]
162+
namespace TestNamespace
163+
{
164+
}
165+
";
166+
167+
DiagnosticResult[] expected =
168+
{
169+
this.CSharpDiagnostic().WithLocation(4, 31)
170+
};
171+
172+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
173+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
174+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
175+
}
176+
141177
/// <summary>
142178
/// Verifies that multiple attributes on the same line for module level will produce the expected diagnostic.
143179
/// </summary>
@@ -366,6 +402,33 @@ public bool TestMethod2([In][MarshalAs(UnmanagedType.I4)] int value)
366402
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
367403
}
368404

405+
/// <summary>
406+
/// Verifies that multiple attributes on the same line for a generic parameter will produce the expected diagnostic.
407+
/// </summary>
408+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
409+
[Fact]
410+
public async Task VerifyMultipleAttributesOnSameLineForGenericParameterAsync()
411+
{
412+
var testCode = @"using System;
413+
414+
namespace TestNamespace
415+
{
416+
[AttributeUsage(System.AttributeTargets.All, AllowMultiple = true)]
417+
public sealed class TestAttribute : Attribute
418+
{
419+
public string Param { get; private set; }
420+
public TestAttribute(string param) { Param = param; }
421+
}
422+
423+
public class TestClass<[Test(""Test1"")][Test(""Test2"")]T>
424+
{
425+
}
426+
}
427+
";
428+
429+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
430+
}
431+
369432
/// <inheritdoc/>
370433
protected override CodeFixProvider GetCSharpCodeFixProvider()
371434
{

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1134AttributesMustNotShareLine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ private static void HandleAttributeList(SyntaxNodeAnalysisContext context)
6363
AttributeListSyntax attributeList = (AttributeListSyntax)context.Node;
6464
bool violation = false;
6565

66-
if (attributeList.Parent.IsKind(SyntaxKind.Parameter))
66+
if (attributeList.Parent.IsKind(SyntaxKind.Parameter) || attributeList.Parent.IsKind(SyntaxKind.TypeParameter))
6767
{
68-
// no analysis required for parameters
68+
// no analysis required for parameters or type (generic) parameters
6969
return;
7070
}
7171

0 commit comments

Comments
 (0)