From 2a14b12cdaeef94d9353412bf294e3ea8a757341 Mon Sep 17 00:00:00 2001 From: Corniel Nobel Date: Wed, 28 Aug 2024 09:06:59 +0200 Subject: [PATCH] Fix some issues. --- .../Qowaiv.CodeAnalysis.Specs.csproj | 3 --- .../Qowaiv.CodeAnalysis.CSharp.csproj | 6 ++---- .../Rules/DecoratePureFunctions.cs | 2 +- .../Rules/DefinePropertiesAsNotNullable.cs | 4 ++-- .../Rules/PreferRegularOverPositionalProperties.cs | 12 ++++-------- .../Rules/UseSystemDateOnly.cs | 8 ++++---- 6 files changed, 13 insertions(+), 22 deletions(-) diff --git a/specs/Qowaiv.CodeAnalysis.Specs/Qowaiv.CodeAnalysis.Specs.csproj b/specs/Qowaiv.CodeAnalysis.Specs/Qowaiv.CodeAnalysis.Specs.csproj index 18f0a27..578b6db 100644 --- a/specs/Qowaiv.CodeAnalysis.Specs/Qowaiv.CodeAnalysis.Specs.csproj +++ b/specs/Qowaiv.CodeAnalysis.Specs/Qowaiv.CodeAnalysis.Specs.csproj @@ -35,9 +35,6 @@ - - - diff --git a/src/Qowaiv.CodeAnalysis.CSharp/Qowaiv.CodeAnalysis.CSharp.csproj b/src/Qowaiv.CodeAnalysis.CSharp/Qowaiv.CodeAnalysis.CSharp.csproj index 3101229..1b911ba 100644 --- a/src/Qowaiv.CodeAnalysis.CSharp/Qowaiv.CodeAnalysis.CSharp.csproj +++ b/src/Qowaiv.CodeAnalysis.CSharp/Qowaiv.CodeAnalysis.CSharp.csproj @@ -19,12 +19,12 @@ true - 2.0.1 + 2.0.2 package-icon.png https://github.com/Qowaiv/qowaiv-analyzers/blob/main/design/package-icon.png README.md -ToBeReleased +v2.0.2 - QW0011, QW0012: Improve messages. #50 v2.0.1 - QW0016: Only take public records into account (FP). #48 @@ -113,8 +113,6 @@ v0.0.1 - - diff --git a/src/Qowaiv.CodeAnalysis.CSharp/Rules/DecoratePureFunctions.cs b/src/Qowaiv.CodeAnalysis.CSharp/Rules/DecoratePureFunctions.cs index 16f2c2b..827474e 100644 --- a/src/Qowaiv.CodeAnalysis.CSharp/Rules/DecoratePureFunctions.cs +++ b/src/Qowaiv.CodeAnalysis.CSharp/Rules/DecoratePureFunctions.cs @@ -6,7 +6,7 @@ public sealed class DecoratePureFunctions() : CodingRule(Rule.DecoratePureFuncti protected override void Register(AnalysisContext context) => context.RegisterSyntaxNodeAction(Report, SyntaxKind.MethodDeclaration); - private void Report(SyntaxNodeAnalysisContext context) + private static void Report(SyntaxNodeAnalysisContext context) { var declaration = context.Node; var symbol = context.SemanticModel.GetDeclaredSymbol(declaration); diff --git a/src/Qowaiv.CodeAnalysis.CSharp/Rules/DefinePropertiesAsNotNullable.cs b/src/Qowaiv.CodeAnalysis.CSharp/Rules/DefinePropertiesAsNotNullable.cs index 9ac5b63..b565373 100644 --- a/src/Qowaiv.CodeAnalysis.CSharp/Rules/DefinePropertiesAsNotNullable.cs +++ b/src/Qowaiv.CodeAnalysis.CSharp/Rules/DefinePropertiesAsNotNullable.cs @@ -11,10 +11,10 @@ protected override void Register(AnalysisContext context) context.RegisterSyntaxNodeAction(ReportRecord, SyntaxKind.RecordDeclaration); } - private void ReportProperty(SyntaxNodeAnalysisContext context) + private static void ReportProperty(SyntaxNodeAnalysisContext context) => Report(context.Node.Cast().Type, context); - private void ReportRecord(SyntaxNodeAnalysisContext context) + private static void ReportRecord(SyntaxNodeAnalysisContext context) { foreach (var type in context.Node.Cast().ParameterTypes()) { diff --git a/src/Qowaiv.CodeAnalysis.CSharp/Rules/PreferRegularOverPositionalProperties.cs b/src/Qowaiv.CodeAnalysis.CSharp/Rules/PreferRegularOverPositionalProperties.cs index 029043a..f542c37 100644 --- a/src/Qowaiv.CodeAnalysis.CSharp/Rules/PreferRegularOverPositionalProperties.cs +++ b/src/Qowaiv.CodeAnalysis.CSharp/Rules/PreferRegularOverPositionalProperties.cs @@ -1,7 +1,4 @@ - -using System.Linq.Expressions; - -namespace Qowaiv.CodeAnalysis.Rules; +namespace Qowaiv.CodeAnalysis.Rules; [DiagnosticAnalyzer(LanguageNames.CSharp)] public sealed class PreferRegularOverPositionalProperties() : CodingRule(Rule.PreferRegularOverPositionalProperties) @@ -11,9 +8,8 @@ protected override void Register(AnalysisContext context) private void Report(SyntaxNodeAnalysisContext context) { - if (context.Compilation.LanguageVersion() < LanguageVersionExt.CSharp11) { return; } - - if (context.Node is RecordDeclarationSyntax declaration + if (context.Compilation.LanguageVersion() >= LanguageVersionExt.CSharp11 + && context.Node is RecordDeclarationSyntax declaration && declaration.ParameterList is { } list && context.Node.TypeDeclaration(context.SemanticModel) is { IsPublic: true, IsObsolete: false }) { @@ -27,7 +23,7 @@ private void Report(SyntaxNodeAnalysisContext context) } } - private static bool Contains(BaseListSyntax? @base, ParameterSyntax parameter) + private static bool Contains(BaseListSyntax? @base, ParameterSyntax parameter) => @base?.Types is { Count: 1 } types && types[0] is PrimaryConstructorBaseTypeSyntax primary && primary.ArgumentList?.Arguments is { } arguments diff --git a/src/Qowaiv.CodeAnalysis.CSharp/Rules/UseSystemDateOnly.cs b/src/Qowaiv.CodeAnalysis.CSharp/Rules/UseSystemDateOnly.cs index 0ccd83e..356c358 100644 --- a/src/Qowaiv.CodeAnalysis.CSharp/Rules/UseSystemDateOnly.cs +++ b/src/Qowaiv.CodeAnalysis.CSharp/Rules/UseSystemDateOnly.cs @@ -13,13 +13,13 @@ protected override void Register(AnalysisContext context) context.RegisterSyntaxNodeAction(ReportProperty, SyntaxKind.PropertyDeclaration); } - private void ReportField(SyntaxNodeAnalysisContext context) + private static void ReportField(SyntaxNodeAnalysisContext context) => Report(context.Node.Cast().Declaration?.Type, context); - private void ReportMethod(SyntaxNodeAnalysisContext context) + private static void ReportMethod(SyntaxNodeAnalysisContext context) => Report(context.Node.Cast().ReturnType, context); - private void ReportParameterList(SyntaxNodeAnalysisContext context) + private static void ReportParameterList(SyntaxNodeAnalysisContext context) { foreach (var type in context.Node.Cast().Parameters.Select(p => p.Type)) { @@ -27,7 +27,7 @@ private void ReportParameterList(SyntaxNodeAnalysisContext context) } } - private void ReportProperty(SyntaxNodeAnalysisContext context) + private static void ReportProperty(SyntaxNodeAnalysisContext context) => Report(context.Node.Cast().Type, context); private static void Report(TypeSyntax? syntax, SyntaxNodeAnalysisContext context)