Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.UseAutoProperty;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.UseAutoProperty;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CSharpUseAutoPropertyAnalyzer : AbstractUseAutoPropertyAnalyzer<
internal sealed class CSharpUseAutoPropertyAnalyzer() : AbstractUseAutoPropertyAnalyzer<
SyntaxKind,
PropertyDeclarationSyntax,
ConstructorDeclarationSyntax,
FieldDeclarationSyntax,
VariableDeclaratorSyntax,
ExpressionSyntax,
IdentifierNameSyntax>
IdentifierNameSyntax>(CSharpSemanticFacts.Instance)
{
protected override SyntaxKind PropertyDeclarationKind
=> SyntaxKind.PropertyDeclaration;
Expand All @@ -36,9 +35,6 @@ protected override bool CanExplicitInterfaceImplementationsBeFixed
protected override bool SupportsFieldAttributesOnProperties
=> true;

protected override ISemanticFacts SemanticFacts
=> CSharpSemanticFacts.Instance;

protected override bool SupportsReadOnlyProperties(Compilation compilation)
=> compilation.LanguageVersion() >= LanguageVersion.CSharp6;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.CSharp.UseAutoProperty;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -3115,4 +3114,24 @@ public required string Action
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76634")]
public async Task TestRefField()
{
await TestMissingInRegularAndScriptAsync(
"""
class Class
{
[|ref int i|];

int P
{
get
{
return i;
}
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ internal abstract partial class AbstractUseAutoPropertyAnalyzer<
TFieldDeclaration,
TVariableDeclarator,
TExpression,
TIdentifierName> : AbstractBuiltInCodeStyleDiagnosticAnalyzer
TIdentifierName>(ISemanticFacts semanticFacts)
: AbstractBuiltInCodeStyleDiagnosticAnalyzer(IDEDiagnosticIds.UseAutoPropertyDiagnosticId,
EnforceOnBuildValues.UseAutoProperty,
CodeStyleOptions2.PreferAutoProperties,
new LocalizableResourceString(nameof(AnalyzersResources.Use_auto_property), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)),
new LocalizableResourceString(nameof(AnalyzersResources.Use_auto_property), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)))
where TSyntaxKind : struct, Enum
where TPropertyDeclaration : SyntaxNode
where TConstructorDeclaration : SyntaxNode
Expand All @@ -48,17 +53,7 @@ internal abstract partial class AbstractUseAutoPropertyAnalyzer<
/// <summary>
/// Not static as this has different semantics around case sensitivity for C# and VB.
/// </summary>
private readonly ObjectPool<HashSet<string>> _fieldNamesPool;

protected AbstractUseAutoPropertyAnalyzer()
: base(IDEDiagnosticIds.UseAutoPropertyDiagnosticId,
EnforceOnBuildValues.UseAutoProperty,
CodeStyleOptions2.PreferAutoProperties,
new LocalizableResourceString(nameof(AnalyzersResources.Use_auto_property), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)),
new LocalizableResourceString(nameof(AnalyzersResources.Use_auto_property), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)))
{
_fieldNamesPool = new(() => new(this.SyntaxFacts.StringComparer));
}
private readonly ObjectPool<HashSet<string>> _fieldNamesPool = new(() => new(semanticFacts.SyntaxFacts.StringComparer));

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

protected abstract ISemanticFacts SemanticFacts { get; }
protected ISyntaxFacts SyntaxFacts => this.SemanticFacts.SyntaxFacts;
private ISemanticFacts SemanticFacts { get; } = semanticFacts;
private ISyntaxFacts SyntaxFacts => SemanticFacts.SyntaxFacts;

protected abstract TSyntaxKind PropertyDeclarationKind { get; }

Expand Down Expand Up @@ -139,6 +134,8 @@ protected sealed override void InitializeWorker(AnalysisContext context)
IsConst: false,
// Can't preserve volatile semantics on a property.
IsVolatile: false,
// Can't have an autoprop that returns by-ref.
RefKind: RefKind.None,
// To make processing later on easier, limit to well-behaved fields (versus having multiple
// fields merged together in error recoery scenarios).
DeclaringSyntaxReferences.Length: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
' See the LICENSE file in the project root for more information.

Imports System.Collections.Concurrent
Imports System.Collections.Immutable
Imports System.Threading
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.LanguageService
Imports Microsoft.CodeAnalysis.UseAutoProperty
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax

Namespace Microsoft.CodeAnalysis.VisualBasic.UseAutoProperty
<DiagnosticAnalyzer(LanguageNames.VisualBasic)>
Friend Class VisualBasicUseAutoPropertyAnalyzer
Friend NotInheritable Class VisualBasicUseAutoPropertyAnalyzer
Inherits AbstractUseAutoPropertyAnalyzer(Of
SyntaxKind,
PropertyBlockSyntax,
Expand All @@ -22,9 +20,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseAutoProperty
ExpressionSyntax,
IdentifierNameSyntax)

Protected Overrides ReadOnly Property PropertyDeclarationKind As SyntaxKind = SyntaxKind.PropertyBlock
Public Sub New()
MyBase.New(VisualBasicSemanticFacts.Instance)
End Sub

Protected Overrides ReadOnly Property SemanticFacts As ISemanticFacts = VisualBasicSemanticFacts.Instance
Protected Overrides ReadOnly Property PropertyDeclarationKind As SyntaxKind = SyntaxKind.PropertyBlock

Protected Overrides Function SupportsReadOnlyProperties(compilation As Compilation) As Boolean
Return DirectCast(compilation, VisualBasicCompilation).LanguageVersion >= LanguageVersion.VisualBasic14
Expand Down
Loading