Skip to content

Commit cdd40e3

Browse files
committed
SA1133 now longer considers attributes on parameters and type parameters
1 parent 90708ec commit cdd40e3

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1133UnitTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,36 @@ internal class BarAttribute : Attribute
276276
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
277277
}
278278

279+
/// <summary>
280+
/// Verifies that attribute list with multiple attributes for (generic) parameters will not produce diagnostics.
281+
/// Regression test for #1882
282+
/// </summary>
283+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
284+
[Fact]
285+
public async Task VerifyAttributeListForParametersAsync()
286+
{
287+
var testCode = @"using System;
288+
289+
internal class TestClass
290+
{
291+
internal T TestMethod<[Foo, Bar]T>([Bar, Foo] int value)
292+
{
293+
return default(T);
294+
}
295+
}
296+
297+
internal class FooAttribute : Attribute
298+
{
299+
}
300+
301+
internal class BarAttribute : Attribute
302+
{
303+
}
304+
";
305+
306+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
307+
}
308+
279309
/// <inheritdoc/>
280310
protected override CodeFixProvider GetCSharpCodeFixProvider()
281311
{

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1133DoNotCombineAttributes.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ private static void HandleAttributeList(SyntaxNodeAnalysisContext context)
5050
{
5151
AttributeListSyntax attributeList = (AttributeListSyntax)context.Node;
5252

53+
if (attributeList.Parent.IsKind(SyntaxKind.Parameter) || attributeList.Parent.IsKind(SyntaxKind.TypeParameter))
54+
{
55+
// no analysis required for parameters or type (generic) parameters
56+
return;
57+
}
58+
5359
if (attributeList.Attributes.Count > 1)
5460
{
5561
context.ReportDiagnostic(Diagnostic.Create(Descriptor, attributeList.Attributes[1].Name.GetLocation()));

documentation/SA1133.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ public class MyCodeFixProvider : CodeFixProvider
4444
}
4545
```
4646

47+
### Exceptions
48+
49+
A violation of this rule will not occur for attributes placed on parameters or type parameters.
50+
51+
For example, the following code will not produce violations:
52+
53+
```csharp
54+
public class MyClass<[Foo, Bar] T>
55+
{
56+
public T MyMethod([In, MarshalAs(UnmanagedType.LPWStr)] string value)
57+
{
58+
...
59+
}
60+
}
61+
```
62+
4763
## How to fix violations
4864

4965
To fix a violation of this rule, place each attribute within its own set of square brackets.

documentation/SA1134.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ public class MyCodeFixProvider : CodeFixProvider
4444
}
4545
```
4646

47+
### Exceptions
48+
49+
A violation of this rule will not occur for attributes placed on parameters or type parameters.
50+
51+
For example, the following code will not produce violations:
52+
53+
```csharp
54+
public class MyClass<[Foo][Bar] T>
55+
{
56+
public T MyMethod([In][MarshalAs(UnmanagedType.LPWStr)] string value)
57+
{
58+
...
59+
}
60+
}
61+
```
62+
4763
## How to fix violations
4864

4965
To fix a violation of this rule, place each attribute on its own line.

0 commit comments

Comments
 (0)