Skip to content

Commit 96ddfb3

Browse files
Fix formatting in use-auto-prop when attributes are present (#79735)
2 parents ec9aca1 + 1072c37 commit 96ddfb3

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,4 +3058,33 @@ public C(string a)
30583058
}
30593059
""",
30603060
new(options: Option(CodeStyleOptions2.QualifyPropertyAccess, true, NotificationOption2.Error)));
3061+
3062+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79734")]
3063+
public Task TestAttributeOnAccessor()
3064+
=> TestInRegularAndScriptAsync(
3065+
"""
3066+
class AAttribute : Attribute;
3067+
3068+
class C
3069+
{
3070+
[|int _i;|]
3071+
int I
3072+
{
3073+
[A]
3074+
get => _i;
3075+
}
3076+
}
3077+
""",
3078+
"""
3079+
class AAttribute : Attribute;
3080+
3081+
class C
3082+
{
3083+
int I
3084+
{
3085+
[A]
3086+
get;
3087+
}
3088+
}
3089+
""");
30613090
}

src/Features/CSharp/Portable/UseAutoProperty/CSharpUseAutoPropertyCodeFixProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Collections.Generic;
76
using System.Collections.Immutable;
87
using System.Composition;
98
using System.Diagnostics.CodeAnalysis;
@@ -221,7 +220,7 @@ protected override ImmutableArray<AbstractFormattingRule> GetFormattingRules(
221220
{
222221
// If the final property is only simple `get;set;` accessors, then reformat the property to be on a single line.
223222
if (propertyDeclaration is PropertyDeclarationSyntax { AccessorList.Accessors: var accessors } &&
224-
accessors.All(a => a is { ExpressionBody: null, Body: null }))
223+
accessors.All(a => a is { ExpressionBody: null, Body: null, AttributeLists.Count: 0 }))
225224
{
226225
return [new SingleLinePropertyFormattingRule(), .. Formatter.GetDefaultFormattingRules(document)];
227226
}

0 commit comments

Comments
 (0)