SA1134: Attributes should not share line
📝 This rule is new for StyleCop Analyzers, and was not present in StyleCop Classic.
Cause
An attribute is placed on the same line of code as another attribute or element.
Rule description
A violation of this rule occurs when two more more attributes are placed on the same line of code, or an attribute is placed on the same line of code as another element.
For example, the following code would produce a violation of this rule:
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MyCodeFixProvider))][Shared]
public class MyCodeFixProvider : CodeFixProvider
{
}
The following code would not produce any violations:
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MyCodeFixProvider))]
[Shared]
public class MyCodeFixProvider : CodeFixProvider
{
}
Exceptions
A violation of this rule will not occur for attributes placed on parameters, type parameters, lambda expressions, or single-line auto-property accessors.
For example, the following code will not produce violations:
public class MyClass<[Foo][Bar] T>
{
public T MyMethod([In][MarshalAs(UnmanagedType.LPWStr)] string value)
{
...
}
}
Attributes on single-line auto-property accessors will also not produce violations:
public class MyClass
{
public bool DisableCaching { get; [UsedImplicitly] set; }
}
How to fix violations
To fix a violation of this rule, place each attribute on its own line.
How to suppress violations
#pragma warning disable SA1134 // Attributes should not share line
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MyCodeFixProvider))][Shared]
#pragma warning restore SA1134 // Attributes should not share line
public class MyCodeFixProvider : CodeFixProvider
{
}