From 22dfdf56803cba92b1de49476f6f16750cb0add9 Mon Sep 17 00:00:00 2001 From: Daniil Zaycev Date: Mon, 19 Feb 2024 08:56:12 +0300 Subject: [PATCH] fix(analyzer release tracking): fix RS2003 https://github.com/dotnet/roslyn-analyzers/issues/5828 --- .../AnalyzerReleases.Shipped.md | 8 ++-- .../Analyzers/BaseImmutableAnalyzer.cs | 39 +------------------ .../ImmutablePropertyTypeAnalyzer.cs | 26 +++++++++---- .../Analyzers/ImmutableSetAccessorAnalyzer.cs | 25 ++++++++---- .../ImmutableAnalyzer.csproj | 5 +++ 5 files changed, 47 insertions(+), 56 deletions(-) diff --git a/ImmutableAnalyzer/ImmutableAnalyzer/AnalyzerReleases.Shipped.md b/ImmutableAnalyzer/ImmutableAnalyzer/AnalyzerReleases.Shipped.md index 9737322..eb2f8ac 100644 --- a/ImmutableAnalyzer/ImmutableAnalyzer/AnalyzerReleases.Shipped.md +++ b/ImmutableAnalyzer/ImmutableAnalyzer/AnalyzerReleases.Shipped.md @@ -2,7 +2,7 @@ ### New Rules -Rule ID | Category | Severity | Notes ---------|----------|----------|------------------------------------------------ -IM0001 | Design | Error | Immutable classes should not contain mutable members. -IM0002 | Design | Error | Members of immutable classes shoud not have public setters. \ No newline at end of file +Rule ID | Category | Severity | Notes +--------|----------|----------|------------------------------------------------------------- +IM0001 | Design | Error | Immutable classes should not contain mutable members. +IM0002 | Design | Error | Members of immutable classes shoud not have public setters. \ No newline at end of file diff --git a/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/BaseImmutableAnalyzer.cs b/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/BaseImmutableAnalyzer.cs index 0d997ab..e182188 100644 --- a/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/BaseImmutableAnalyzer.cs +++ b/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/BaseImmutableAnalyzer.cs @@ -1,7 +1,4 @@ -using System.Collections.Immutable; -using System.Linq; -using ImmutableAnalyzer.Extensions; -using Microsoft.CodeAnalysis; +using ImmutableAnalyzer.Extensions; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; @@ -10,40 +7,6 @@ namespace ImmutableAnalyzer.Analyzers; internal abstract class BaseImmutableAnalyzer : DiagnosticAnalyzer { - /// - /// Diagnostic Identifier. - /// - protected abstract string DiagnosticId { get; } - - /// - /// Diagnostic title. - /// - protected abstract string Title { get; } - - /// - /// Diagnostic description. - /// - protected abstract string Description { get; } - - /// - /// Diagnostic message format. - /// - protected abstract string MessageFormat { get; } - - /// - /// Diagnostic category. - /// - protected abstract string Category { get; } - - /// - public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); - - /// - /// Diagnostic descriptor. - /// - protected DiagnosticDescriptor Rule => new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, - isEnabledByDefault: true, description: Description); - /// public override void Initialize(AnalysisContext context) { diff --git a/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/ImmutablePropertyTypeAnalyzer.cs b/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/ImmutablePropertyTypeAnalyzer.cs index 76beb20..66f0f5a 100644 --- a/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/ImmutablePropertyTypeAnalyzer.cs +++ b/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/ImmutablePropertyTypeAnalyzer.cs @@ -1,4 +1,5 @@ -using ImmutableAnalyzer.Extensions; +using System.Collections.Immutable; +using ImmutableAnalyzer.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -10,13 +11,24 @@ namespace ImmutableAnalyzer.Analyzers; /// Analyzer to check property types of immutable classes. /// [DiagnosticAnalyzer(LanguageNames.CSharp)] -internal class ImmutablePropertyTypeAnalyzer : BaseImmutableAnalyzer +internal sealed class ImmutablePropertyTypeAnalyzer : BaseImmutableAnalyzer { - protected override string DiagnosticId => "IM0001"; - protected override string Title => "Mutable member in immutable class"; - protected override string MessageFormat => "Immutable class can't have property of type '{0}'"; - protected override string Description => "Class member must have immutable type"; - protected override string Category => "Design"; + private const string DiagnosticId = "IM0001"; + private const string Title = "Mutable member in immutable class"; + private const string MessageFormat = "Immutable class can't have property of type '{0}'"; + private const string Description = "Class member must have immutable type."; + private const string Category = "Design"; + + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); + + /// + /// Diagnostic descriptor. + /// + private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( + DiagnosticId, Title, MessageFormat, + Category, DiagnosticSeverity.Error, true, Description + ); /// protected override void AnalyzeSyntax( diff --git a/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/ImmutableSetAccessorAnalyzer.cs b/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/ImmutableSetAccessorAnalyzer.cs index 057e05e..ef3ea0d 100644 --- a/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/ImmutableSetAccessorAnalyzer.cs +++ b/ImmutableAnalyzer/ImmutableAnalyzer/Analyzers/ImmutableSetAccessorAnalyzer.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Immutable; +using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; @@ -9,14 +10,24 @@ namespace ImmutableAnalyzer.Analyzers; /// Analyzer to check set accessor of properties of immutable classes. /// [DiagnosticAnalyzer(LanguageNames.CSharp)] -internal class ImmutableSetAccessorAnalyzer : BaseImmutableAnalyzer +internal sealed class ImmutableSetAccessorAnalyzer : BaseImmutableAnalyzer { - protected override string DiagnosticId => "IM0002"; - protected override string Title => "Public setter violates class immutability"; - protected override string MessageFormat => "Member of immutable class can't have '{0}' accessor"; - protected override string Description => "Setter can't be public, because it's give possibility to change member from the outer."; + private const string DiagnosticId = "IM0002"; + private const string Title = "Public setter violates class immutability"; + private const string MessageFormat = "Member of immutable class can't have '{0}' accessor"; + private const string Description = "Setter can't be public, because it's give possibility to change member from the outer."; + private const string Category = "Design"; + + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); - protected override string Category => "Design"; + /// + /// Diagnostic descriptor. + /// + private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( + DiagnosticId, Title, MessageFormat, + Category, DiagnosticSeverity.Error, true, Description + ); protected override void AnalyzeSyntax(ClassDeclarationSyntax classDeclarationNode, SyntaxNodeAnalysisContext context) { diff --git a/ImmutableAnalyzer/ImmutableAnalyzer/ImmutableAnalyzer.csproj b/ImmutableAnalyzer/ImmutableAnalyzer/ImmutableAnalyzer.csproj index 3471903..35ceb84 100644 --- a/ImmutableAnalyzer/ImmutableAnalyzer/ImmutableAnalyzer.csproj +++ b/ImmutableAnalyzer/ImmutableAnalyzer/ImmutableAnalyzer.csproj @@ -22,4 +22,9 @@ + + + + +