Skip to content

Commit 6cac171

Browse files
Do not offer 'use auto prop' with ref-fields (#76638)
2 parents 3638dd9 + e39bbe6 commit 6cac171

File tree

4 files changed

+38
-26
lines changed

4 files changed

+38
-26
lines changed

src/Analyzers/CSharp/Analyzers/UseAutoProperty/CSharpUseAutoPropertyAnalyzer.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@
1010
using Microsoft.CodeAnalysis.CSharp.Extensions;
1111
using Microsoft.CodeAnalysis.CSharp.Syntax;
1212
using Microsoft.CodeAnalysis.Diagnostics;
13-
using Microsoft.CodeAnalysis.LanguageService;
1413
using Microsoft.CodeAnalysis.Shared.Extensions;
1514
using Microsoft.CodeAnalysis.UseAutoProperty;
1615
using Roslyn.Utilities;
1716

1817
namespace Microsoft.CodeAnalysis.CSharp.UseAutoProperty;
1918

2019
[DiagnosticAnalyzer(LanguageNames.CSharp)]
21-
internal sealed class CSharpUseAutoPropertyAnalyzer : AbstractUseAutoPropertyAnalyzer<
20+
internal sealed class CSharpUseAutoPropertyAnalyzer() : AbstractUseAutoPropertyAnalyzer<
2221
SyntaxKind,
2322
PropertyDeclarationSyntax,
2423
ConstructorDeclarationSyntax,
2524
FieldDeclarationSyntax,
2625
VariableDeclaratorSyntax,
2726
ExpressionSyntax,
28-
IdentifierNameSyntax>
27+
IdentifierNameSyntax>(CSharpSemanticFacts.Instance)
2928
{
3029
protected override SyntaxKind PropertyDeclarationKind
3130
=> SyntaxKind.PropertyDeclaration;
@@ -36,9 +35,6 @@ protected override bool CanExplicitInterfaceImplementationsBeFixed
3635
protected override bool SupportsFieldAttributesOnProperties
3736
=> true;
3837

39-
protected override ISemanticFacts SemanticFacts
40-
=> CSharpSemanticFacts.Instance;
41-
4238
protected override bool SupportsReadOnlyProperties(Compilation compilation)
4339
=> compilation.LanguageVersion() >= LanguageVersion.CSharp6;
4440

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Threading.Tasks;
66
using Microsoft.CodeAnalysis.CodeFixes;
77
using Microsoft.CodeAnalysis.CSharp;
8-
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
98
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
109
using Microsoft.CodeAnalysis.CSharp.UseAutoProperty;
1110
using Microsoft.CodeAnalysis.Diagnostics;
@@ -3115,4 +3114,24 @@ public required string Action
31153114
}
31163115
""");
31173116
}
3117+
3118+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76634")]
3119+
public async Task TestRefField()
3120+
{
3121+
await TestMissingInRegularAndScriptAsync(
3122+
"""
3123+
class Class
3124+
{
3125+
[|ref int i|];
3126+
3127+
int P
3128+
{
3129+
get
3130+
{
3131+
return i;
3132+
}
3133+
}
3134+
}
3135+
""");
3136+
}
31183137
}

src/Analyzers/Core/Analyzers/UseAutoProperty/AbstractUseAutoPropertyAnalyzer.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ internal abstract partial class AbstractUseAutoPropertyAnalyzer<
2828
TFieldDeclaration,
2929
TVariableDeclarator,
3030
TExpression,
31-
TIdentifierName> : AbstractBuiltInCodeStyleDiagnosticAnalyzer
31+
TIdentifierName>(ISemanticFacts semanticFacts)
32+
: AbstractBuiltInCodeStyleDiagnosticAnalyzer(IDEDiagnosticIds.UseAutoPropertyDiagnosticId,
33+
EnforceOnBuildValues.UseAutoProperty,
34+
CodeStyleOptions2.PreferAutoProperties,
35+
new LocalizableResourceString(nameof(AnalyzersResources.Use_auto_property), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)),
36+
new LocalizableResourceString(nameof(AnalyzersResources.Use_auto_property), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)))
3237
where TSyntaxKind : struct, Enum
3338
where TPropertyDeclaration : SyntaxNode
3439
where TConstructorDeclaration : SyntaxNode
@@ -48,17 +53,7 @@ internal abstract partial class AbstractUseAutoPropertyAnalyzer<
4853
/// <summary>
4954
/// Not static as this has different semantics around case sensitivity for C# and VB.
5055
/// </summary>
51-
private readonly ObjectPool<HashSet<string>> _fieldNamesPool;
52-
53-
protected AbstractUseAutoPropertyAnalyzer()
54-
: base(IDEDiagnosticIds.UseAutoPropertyDiagnosticId,
55-
EnforceOnBuildValues.UseAutoProperty,
56-
CodeStyleOptions2.PreferAutoProperties,
57-
new LocalizableResourceString(nameof(AnalyzersResources.Use_auto_property), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)),
58-
new LocalizableResourceString(nameof(AnalyzersResources.Use_auto_property), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)))
59-
{
60-
_fieldNamesPool = new(() => new(this.SyntaxFacts.StringComparer));
61-
}
56+
private readonly ObjectPool<HashSet<string>> _fieldNamesPool = new(() => new(semanticFacts.SyntaxFacts.StringComparer));
6257

6358
protected static void AddFieldUsage(ConcurrentDictionary<IFieldSymbol, ConcurrentSet<SyntaxNode>> fieldWrites, IFieldSymbol field, SyntaxNode location)
6459
=> fieldWrites.GetOrAdd(field, static _ => s_nodeSetPool.Allocate()).Add(location);
@@ -78,8 +73,8 @@ private static void ClearAndFree(ConcurrentDictionary<IFieldSymbol, ConcurrentSe
7873
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
7974
=> DiagnosticAnalyzerCategory.SemanticDocumentAnalysis;
8075

81-
protected abstract ISemanticFacts SemanticFacts { get; }
82-
protected ISyntaxFacts SyntaxFacts => this.SemanticFacts.SyntaxFacts;
76+
private ISemanticFacts SemanticFacts { get; } = semanticFacts;
77+
private ISyntaxFacts SyntaxFacts => SemanticFacts.SyntaxFacts;
8378

8479
protected abstract TSyntaxKind PropertyDeclarationKind { get; }
8580

@@ -139,6 +134,8 @@ protected sealed override void InitializeWorker(AnalysisContext context)
139134
IsConst: false,
140135
// Can't preserve volatile semantics on a property.
141136
IsVolatile: false,
137+
// Can't have an autoprop that returns by-ref.
138+
RefKind: RefKind.None,
142139
// To make processing later on easier, limit to well-behaved fields (versus having multiple
143140
// fields merged together in error recoery scenarios).
144141
DeclaringSyntaxReferences.Length: 1,

src/Analyzers/VisualBasic/Analyzers/UseAutoProperty/VisualBasicUseAutoPropertyAnalyzer.vb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
' See the LICENSE file in the project root for more information.
44

55
Imports System.Collections.Concurrent
6-
Imports System.Collections.Immutable
76
Imports System.Threading
87
Imports Microsoft.CodeAnalysis.Diagnostics
9-
Imports Microsoft.CodeAnalysis.LanguageService
108
Imports Microsoft.CodeAnalysis.UseAutoProperty
119
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
1210

1311
Namespace Microsoft.CodeAnalysis.VisualBasic.UseAutoProperty
1412
<DiagnosticAnalyzer(LanguageNames.VisualBasic)>
15-
Friend Class VisualBasicUseAutoPropertyAnalyzer
13+
Friend NotInheritable Class VisualBasicUseAutoPropertyAnalyzer
1614
Inherits AbstractUseAutoPropertyAnalyzer(Of
1715
SyntaxKind,
1816
PropertyBlockSyntax,
@@ -22,9 +20,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseAutoProperty
2220
ExpressionSyntax,
2321
IdentifierNameSyntax)
2422

25-
Protected Overrides ReadOnly Property PropertyDeclarationKind As SyntaxKind = SyntaxKind.PropertyBlock
23+
Public Sub New()
24+
MyBase.New(VisualBasicSemanticFacts.Instance)
25+
End Sub
2626

27-
Protected Overrides ReadOnly Property SemanticFacts As ISemanticFacts = VisualBasicSemanticFacts.Instance
27+
Protected Overrides ReadOnly Property PropertyDeclarationKind As SyntaxKind = SyntaxKind.PropertyBlock
2828

2929
Protected Overrides Function SupportsReadOnlyProperties(compilation As Compilation) As Boolean
3030
Return DirectCast(compilation, VisualBasicCompilation).LanguageVersion >= LanguageVersion.VisualBasic14

0 commit comments

Comments
 (0)