Skip to content

Commit 06f095f

Browse files
Update 'use auto property' to respect user options around this. for property access. (#79405)
2 parents 24ab192 + b7159b0 commit 06f095f

File tree

17 files changed

+218
-146
lines changed

17 files changed

+218
-146
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Threading.Tasks;
66
using Microsoft.CodeAnalysis.CodeFixes;
7+
using Microsoft.CodeAnalysis.CodeStyle;
78
using Microsoft.CodeAnalysis.CSharp;
89
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
910
using Microsoft.CodeAnalysis.CSharp.UseAutoProperty;
@@ -2999,4 +3000,62 @@ class Class
29993000
readonly ref int P => ref i;
30003001
}
30013002
""");
3003+
3004+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/77011")]
3005+
public Task TestRemoveThisIfPreferredCodeStyle()
3006+
=> TestInRegularAndScriptAsync(
3007+
"""
3008+
class C
3009+
{
3010+
[|private readonly string a;|]
3011+
3012+
public C(string a)
3013+
{
3014+
this.a = a;
3015+
}
3016+
3017+
public string A => a;
3018+
}
3019+
""",
3020+
"""
3021+
class C
3022+
{
3023+
public C(string a)
3024+
{
3025+
A = a;
3026+
}
3027+
3028+
public string A { get; }
3029+
}
3030+
""",
3031+
options: Option(CodeStyleOptions2.QualifyPropertyAccess, false, NotificationOption2.Error));
3032+
3033+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/77011")]
3034+
public Task TestKeepThisIfPreferredCodeStyle()
3035+
=> TestInRegularAndScriptAsync(
3036+
"""
3037+
class C
3038+
{
3039+
[|private readonly string a;|]
3040+
3041+
public C(string a)
3042+
{
3043+
this.a = a;
3044+
}
3045+
3046+
public string A => a;
3047+
}
3048+
""",
3049+
"""
3050+
class C
3051+
{
3052+
public C(string a)
3053+
{
3054+
this.A = a;
3055+
}
3056+
3057+
public string A { get; }
3058+
}
3059+
""",
3060+
options: Option(CodeStyleOptions2.QualifyPropertyAccess, true, NotificationOption2.Error));
30023061
}

src/EditorFeatures/Core/InlineRename/AbstractEditorInlineRenameService.InlineRenameLocationSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task<IInlineRenameReplacementInfo> GetReplacementsAsync(
4444
CancellationToken cancellationToken)
4545
{
4646
var conflicts = await _renameLocationSet.ResolveConflictsAsync(
47-
_renameInfo.RenameSymbol, _renameInfo.GetFinalSymbolName(replacementText), nonConflictSymbolKeys: default, cancellationToken).ConfigureAwait(false);
47+
_renameInfo.RenameSymbol, _renameInfo.GetFinalSymbolName(replacementText), cancellationToken).ConfigureAwait(false);
4848

4949
return new InlineRenameReplacementInfo(conflicts);
5050
}

src/EditorFeatures/Test2/Diagnostics/UseAutoProperty/UseAutoPropertyTests.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ partial class C
131131

132132
public C()
133133
{
134-
this.P = 0;
134+
P = 0;
135135
}
136136
}".Trim()
137137

src/EditorFeatures/Test2/Rename/RenameEngineResult.vb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,13 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
136136
Dim locations = Renamer.FindRenameLocationsAsync(
137137
solution, symbol, renameOptions, CancellationToken.None).GetAwaiter().GetResult()
138138

139-
Return locations.ResolveConflictsAsync(symbol, renameTo, nonConflictSymbolKeys:=Nothing, CancellationToken.None).GetAwaiter().GetResult()
139+
Return locations.ResolveConflictsAsync(symbol, renameTo, CancellationToken.None).GetAwaiter().GetResult()
140140
Else
141141
' This tests that rename properly works when the entire call is remoted to OOP and the final result is
142142
' marshaled back.
143143

144144
Return Renamer.RenameSymbolAsync(
145-
solution, symbol, renameTo, renameOptions,
146-
nonConflictSymbolKeys:=Nothing, CancellationToken.None).GetAwaiter().GetResult()
145+
solution, symbol, renameTo, renameOptions, CancellationToken.None).GetAwaiter().GetResult()
147146
End If
148147
End Function
149148

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
using Microsoft.CodeAnalysis.CSharp.Extensions;
1515
using Microsoft.CodeAnalysis.CSharp.Syntax;
1616
using Microsoft.CodeAnalysis.Editing;
17+
using Microsoft.CodeAnalysis.FindSymbols;
1718
using Microsoft.CodeAnalysis.Formatting;
1819
using Microsoft.CodeAnalysis.Formatting.Rules;
1920
using Microsoft.CodeAnalysis.PooledObjects;
20-
using Microsoft.CodeAnalysis.Rename;
2121
using Microsoft.CodeAnalysis.Shared.Extensions;
2222
using Microsoft.CodeAnalysis.UseAutoProperty;
2323
using Roslyn.Utilities;
@@ -59,16 +59,15 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato
5959

6060
protected override PropertyDeclarationSyntax RewriteFieldReferencesInProperty(
6161
PropertyDeclarationSyntax property,
62-
LightweightRenameLocations fieldLocations,
62+
ImmutableArray<ReferencedSymbol> fieldLocations,
6363
CancellationToken cancellationToken)
6464
{
6565
// We're going to walk this property body, converting most reference of the field to use the `field` keyword
6666
// instead. However, not all reference can be updated. For example, reference through another instance. Those
6767
// we update to point at the property instead. So we grab that property name here to use in the rewriter.
6868
var propertyIdentifierName = IdentifierName(property.Identifier.WithoutTrivia());
6969

70-
var identifierNames = fieldLocations.Locations
71-
.Select(loc => loc.Location.FindNode(getInnermostNodeForTie: true, cancellationToken) as IdentifierNameSyntax)
70+
var identifierNames = fieldLocations.SelectMany(loc => loc.Locations.Select(loc => loc.Location.FindNode(getInnermostNodeForTie: true, cancellationToken) as IdentifierNameSyntax))
7271
.WhereNotNull()
7372
.ToSet();
7473

src/Features/Core/Portable/EncapsulateField/AbstractEncapsulateFieldService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ private static async Task<Solution> RenameAsync(
287287
// edit the files in the current project
288288
var resolution = await initialLocations
289289
.Filter((documentId, span) => !linkedProjectIds.Contains(documentId.ProjectId) && filter(documentId, span))
290-
.ResolveConflictsAsync(field, finalName, nonConflictSymbolKeys: default, cancellationToken).ConfigureAwait(false);
290+
.ResolveConflictsAsync(field, finalName, cancellationToken).ConfigureAwait(false);
291291

292292
Contract.ThrowIfFalse(resolution.IsSuccessful);
293293

0 commit comments

Comments
 (0)