From f46b9cee2cac4c3c63143f367eae4e5e68b59a33 Mon Sep 17 00:00:00 2001 From: Per Kops Date: Wed, 13 Nov 2024 10:34:27 +0100 Subject: [PATCH 1/5] style: fix warnings based on new coding-rules --- .../Models/AnalyzerProviderBaseRuleData.cs | 2 +- ...edProjectTargetTypeDescriptionAttribute.cs | 2 +- .../Commands/RunCommand.cs | 11 +++----- .../Settings/ProjectBaseCommandSettings.cs | 13 ++++----- .../Models/Options/OptionsFolderMappings.cs | 2 +- .../ProjectHelper.cs | 27 +++++++------------ .../EditorConfigHelper.cs | 2 +- .../Extensions/StringExtensions.cs | 4 +-- src/Atc.CodingRules.Updater/FileHelper.cs | 10 ++----- 9 files changed, 27 insertions(+), 46 deletions(-) diff --git a/src/Atc.CodingRules.AnalyzerProviders/Models/AnalyzerProviderBaseRuleData.cs b/src/Atc.CodingRules.AnalyzerProviders/Models/AnalyzerProviderBaseRuleData.cs index a173e8c..15072bf 100644 --- a/src/Atc.CodingRules.AnalyzerProviders/Models/AnalyzerProviderBaseRuleData.cs +++ b/src/Atc.CodingRules.AnalyzerProviders/Models/AnalyzerProviderBaseRuleData.cs @@ -14,7 +14,7 @@ public AnalyzerProviderBaseRuleData(string name) public string Name { get; set; } = string.Empty; [SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "OK.")] - public ICollection Rules { get; set; } = new List(); + public ICollection Rules { get; set; } = []; public string? ExceptionMessage { get; set; } diff --git a/src/Atc.CodingRules.Updater.CLI/Commands/DescriptionAttributes/SupportedProjectTargetTypeDescriptionAttribute.cs b/src/Atc.CodingRules.Updater.CLI/Commands/DescriptionAttributes/SupportedProjectTargetTypeDescriptionAttribute.cs index 884ea29..3a8764d 100644 --- a/src/Atc.CodingRules.Updater.CLI/Commands/DescriptionAttributes/SupportedProjectTargetTypeDescriptionAttribute.cs +++ b/src/Atc.CodingRules.Updater.CLI/Commands/DescriptionAttributes/SupportedProjectTargetTypeDescriptionAttribute.cs @@ -8,7 +8,7 @@ public override string Description get { var defaultValue = new OptionsFile().ProjectTarget; - var values = Enum.GetNames(typeof(SupportedProjectTargetType)) + var values = Enum.GetNames() .Select(enumValue => enumValue.Equals(defaultValue.ToString(), StringComparison.Ordinal) ? $"{enumValue} (default)" : enumValue) diff --git a/src/Atc.CodingRules.Updater.CLI/Commands/RunCommand.cs b/src/Atc.CodingRules.Updater.CLI/Commands/RunCommand.cs index 8c37b13..3ca0fff 100644 --- a/src/Atc.CodingRules.Updater.CLI/Commands/RunCommand.cs +++ b/src/Atc.CodingRules.Updater.CLI/Commands/RunCommand.cs @@ -140,13 +140,10 @@ private static async Task GetOptionsFromFileAndUserArguments( buildFile = settings.BuildFile.Value; } - if (!string.IsNullOrEmpty(buildFile)) - { - return buildFile.Contains(':', StringComparison.Ordinal) + return !string.IsNullOrEmpty(buildFile) + ? buildFile.Contains(':', StringComparison.Ordinal) ? new FileInfo(buildFile) - : new FileInfo(Path.Combine(projectPath.FullName, buildFile)); - } - - return null; + : new FileInfo(Path.Combine(projectPath.FullName, buildFile)) + : null; } } \ No newline at end of file diff --git a/src/Atc.CodingRules.Updater.CLI/Commands/Settings/ProjectBaseCommandSettings.cs b/src/Atc.CodingRules.Updater.CLI/Commands/Settings/ProjectBaseCommandSettings.cs index 2deb8f6..15d1308 100644 --- a/src/Atc.CodingRules.Updater.CLI/Commands/Settings/ProjectBaseCommandSettings.cs +++ b/src/Atc.CodingRules.Updater.CLI/Commands/Settings/ProjectBaseCommandSettings.cs @@ -14,14 +14,11 @@ public class ProjectBaseCommandSettings : BaseCommandSettings public override ValidationResult Validate() { var validationResult = base.Validate(); - if (!validationResult.Successful) - { - return validationResult; - } - - return string.IsNullOrEmpty(ProjectPath) - ? ValidationResult.Error("ProjectPath is missing.") - : ValidationResult.Success(); + return !validationResult.Successful + ? validationResult + : string.IsNullOrEmpty(ProjectPath) + ? ValidationResult.Error("ProjectPath is missing.") + : ValidationResult.Success(); } internal string GetOptionsPath() diff --git a/src/Atc.CodingRules.Updater.CLI/Models/Options/OptionsFolderMappings.cs b/src/Atc.CodingRules.Updater.CLI/Models/Options/OptionsFolderMappings.cs index b5fc4f8..43f8d97 100644 --- a/src/Atc.CodingRules.Updater.CLI/Models/Options/OptionsFolderMappings.cs +++ b/src/Atc.CodingRules.Updater.CLI/Models/Options/OptionsFolderMappings.cs @@ -3,7 +3,7 @@ namespace Atc.CodingRules.Updater.CLI.Models.Options; public class OptionsFolderMappings { [SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "OK.")] - public IList Paths { get; set; } = new List(); + public IList Paths { get; set; } = []; public override string ToString() => $"{nameof(Paths)}.Count: {Paths.Count}"; diff --git a/src/Atc.CodingRules.Updater.CLI/ProjectHelper.cs b/src/Atc.CodingRules.Updater.CLI/ProjectHelper.cs index e61b1a7..0bafae4 100644 --- a/src/Atc.CodingRules.Updater.CLI/ProjectHelper.cs +++ b/src/Atc.CodingRules.Updater.CLI/ProjectHelper.cs @@ -140,24 +140,17 @@ private static ProjectFrameworkType DetermineProjectFrameworkType( Path.GetFileNameWithoutExtension(csProjFile.Name), StringComparison.OrdinalIgnoreCase)); - if (optionsProjectFrameworkMapping is not null) + projectFrameworkType = optionsProjectFrameworkMapping?.Type ?? projectType switch { - projectFrameworkType = optionsProjectFrameworkMapping.Type; - } - else - { - projectFrameworkType = projectType switch - { - DotnetProjectType.AzureFunctionApp => ProjectFrameworkType.AzureFunctions, - DotnetProjectType.BlazorServerApp or DotnetProjectType.BlazorWAsmApp => ProjectFrameworkType.Blazor, - DotnetProjectType.CliApp => ProjectFrameworkType.Cli, - DotnetProjectType.MauiApp => ProjectFrameworkType.Maui, - DotnetProjectType.WinFormApp => ProjectFrameworkType.WinForms, - DotnetProjectType.WpfApp or DotnetProjectType.WpfLibrary => ProjectFrameworkType.Wpf, - DotnetProjectType.WebApi => ProjectFrameworkType.WebApi, - _ => projectFrameworkType, - }; - } + DotnetProjectType.AzureFunctionApp => ProjectFrameworkType.AzureFunctions, + DotnetProjectType.BlazorServerApp or DotnetProjectType.BlazorWAsmApp => ProjectFrameworkType.Blazor, + DotnetProjectType.CliApp => ProjectFrameworkType.Cli, + DotnetProjectType.MauiApp => ProjectFrameworkType.Maui, + DotnetProjectType.WinFormApp => ProjectFrameworkType.WinForms, + DotnetProjectType.WpfApp or DotnetProjectType.WpfLibrary => ProjectFrameworkType.Wpf, + DotnetProjectType.WebApi => ProjectFrameworkType.WebApi, + _ => projectFrameworkType, + }; return projectFrameworkType; } diff --git a/src/Atc.CodingRules.Updater/EditorConfigHelper.cs b/src/Atc.CodingRules.Updater/EditorConfigHelper.cs index 79f7f00..1e2df2f 100644 --- a/src/Atc.CodingRules.Updater/EditorConfigHelper.cs +++ b/src/Atc.CodingRules.Updater/EditorConfigHelper.cs @@ -333,7 +333,7 @@ private static List>> ExtractContentCustomParts( } workingOnCustomHeader = nextLine.Substring(CustomSectionHeaderPrefix.Length).Trim(); - workingOnCustomLines = new List(); + workingOnCustomLines = []; } } diff --git a/src/Atc.CodingRules.Updater/Extensions/StringExtensions.cs b/src/Atc.CodingRules.Updater/Extensions/StringExtensions.cs index 2a0a218..e7b1329 100644 --- a/src/Atc.CodingRules.Updater/Extensions/StringExtensions.cs +++ b/src/Atc.CodingRules.Updater/Extensions/StringExtensions.cs @@ -4,7 +4,7 @@ namespace System; public static class StringExtensions { - private static readonly string[] LineBreaks = { "\r\n", "\r", "\n" }; + private static readonly string[] LineBreaks = ["\r\n", "\r", "\n"]; public static string TrimEndForEmptyLines( this string value) @@ -25,7 +25,7 @@ public static string TrimEndForEmptyLines( public static Collection GetKeyValues( this string value) => string.IsNullOrEmpty(value) - ? new Collection() + ? [] : value .Split(LineBreaks, StringSplitOptions.RemoveEmptyEntries) .GetKeyValues(); diff --git a/src/Atc.CodingRules.Updater/FileHelper.cs b/src/Atc.CodingRules.Updater/FileHelper.cs index fff6bcc..dbd5480 100644 --- a/src/Atc.CodingRules.Updater/FileHelper.cs +++ b/src/Atc.CodingRules.Updater/FileHelper.cs @@ -1,4 +1,3 @@ -// ReSharper disable ConvertIfStatementToReturnStatement namespace Atc.CodingRules.Updater; public static class FileHelper @@ -83,13 +82,8 @@ public static bool AreFilesEqual( return false; } - if (headerLinesA.Find(x => x.StartsWith("# Distribution", StringComparison.CurrentCultureIgnoreCase)) != - headerLinesB.Find(x => x.StartsWith("# Distribution", StringComparison.CurrentCultureIgnoreCase))) - { - return false; - } - - return true; + return headerLinesA.Find(x => x.StartsWith("# Distribution", StringComparison.CurrentCultureIgnoreCase)) == + headerLinesB.Find(x => x.StartsWith("# Distribution", StringComparison.CurrentCultureIgnoreCase)); } public static bool ContainsEditorConfigFile( From 365e630134456a19adeb64bc8b936b3a7cfbaee3 Mon Sep 17 00:00:00 2001 From: Per Kops Date: Wed, 13 Nov 2024 10:34:45 +0100 Subject: [PATCH 2/5] build: add NU1903 to nowarn in Directory.Build.props --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 2c20e6e..335b544 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -20,7 +20,7 @@ enable net8.0 true - 1573,1591,1712,CA1014 + 1573,1591,1712,CA1014,NU1903 full From 041dab773b9e550ab5c1abe5b6660507f3301ebc Mon Sep 17 00:00:00 2001 From: Per Kops Date: Wed, 13 Nov 2024 10:35:02 +0100 Subject: [PATCH 3/5] build: add new rules to root .editorconfig --- .editorconfig | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.editorconfig b/.editorconfig index 2ac6cdc..41acd8b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -516,11 +516,20 @@ dotnet_diagnostic.S1135.severity = suggestion # https://github.com/atc-net # Custom - Code Analyzers Rules ########################################## +dotnet_diagnostic.IDE0010.severity = suggestion # Populate switch +dotnet_diagnostic.IDE0021.severity = suggestion # Use expression body for constructor +dotnet_diagnostic.IDE0046.severity = suggestion # If statement can be simplified +dotnet_diagnostic.IDE0055.severity = none # Fix formatting dotnet_diagnostic.IDE0057.severity = none # Substring can be simplified +dotnet_diagnostic.IDE0072.severity = suggestion # Populate switch +dotnet_diagnostic.IDE0130.severity = suggestion # Namespace does not match folder structure +dotnet_diagnostic.IDE0290.severity = none # Use primary constructor +dotnet_diagnostic.IDE0305.severity = suggestion # Collection initialization can be simplified dotnet_diagnostic.SA1010.severity = none # dotnet_diagnostic.CA1054.severity = none # URI parameters should not be strings +dotnet_diagnostic.CA1515.severity = suggestion # Because an application's API isn't typically referenced from outside the assembly, types can be made internal (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1515) dotnet_diagnostic.CA1848.severity = none # For improved performance, use the LoggerMessage delegates instead of calling 'LoggerExtensions.LogTrace(ILogger, string, params object[])' dotnet_diagnostic.CA1859.severity = none # dotnet_diagnostic.CA1860.severity = none # @@ -536,6 +545,7 @@ dotnet_diagnostic.S1172.severity = none # False positive dotnet_diagnostic.S2589.severity = none # dotnet_diagnostic.S2629.severity = none # dotnet_diagnostic.S3267.severity = none # +dotnet_diagnostic.S3358.severity = none # dotnet_diagnostic.S3878.severity = none # dotnet_diagnostic.S4457.severity = none # Split this method into two, one handling parameters check and the other handling the asynchronous code dotnet_diagnostic.S6602.severity = none # From 521912c52bcfb74b130f567cdb2f6bf71930b90d Mon Sep 17 00:00:00 2001 From: Per Kops Date: Wed, 13 Nov 2024 10:35:14 +0100 Subject: [PATCH 4/5] feat: add dotnet9 support --- src/Atc.CodingRules.Updater.CLI/ProjectHelper.cs | 3 ++- src/Atc.CodingRules.Updater/ProjectSanityCheckHelper.cs | 1 + .../SupportedProjectTargetType.cs | 9 +++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Atc.CodingRules.Updater.CLI/ProjectHelper.cs b/src/Atc.CodingRules.Updater.CLI/ProjectHelper.cs index 0bafae4..6c27e9e 100644 --- a/src/Atc.CodingRules.Updater.CLI/ProjectHelper.cs +++ b/src/Atc.CodingRules.Updater.CLI/ProjectHelper.cs @@ -33,7 +33,8 @@ is SupportedProjectTargetType.DotNetCore or SupportedProjectTargetType.DotNet5 or SupportedProjectTargetType.DotNet6 or SupportedProjectTargetType.DotNet7 - or SupportedProjectTargetType.DotNet8) + or SupportedProjectTargetType.DotNet8 + or SupportedProjectTargetType.DotNet9) { HandleDirectoryBuildPropsFiles(logger, projectPath, options); diff --git a/src/Atc.CodingRules.Updater/ProjectSanityCheckHelper.cs b/src/Atc.CodingRules.Updater/ProjectSanityCheckHelper.cs index 35f222c..d2cbf5a 100644 --- a/src/Atc.CodingRules.Updater/ProjectSanityCheckHelper.cs +++ b/src/Atc.CodingRules.Updater/ProjectSanityCheckHelper.cs @@ -19,6 +19,7 @@ public static void CheckFiles( case SupportedProjectTargetType.DotNet6: case SupportedProjectTargetType.DotNet7: case SupportedProjectTargetType.DotNet8: + case SupportedProjectTargetType.DotNet9: HasTargetFrameworkAndImplicitUsings(throwIf, logger, projectPath, "netcoreapp3.1"); break; } diff --git a/src/Atc.CodingRules.Updater/SupportedProjectTargetType.cs b/src/Atc.CodingRules.Updater/SupportedProjectTargetType.cs index 5b0e9da..9e622d5 100644 --- a/src/Atc.CodingRules.Updater/SupportedProjectTargetType.cs +++ b/src/Atc.CodingRules.Updater/SupportedProjectTargetType.cs @@ -3,8 +3,9 @@ namespace Atc.CodingRules.Updater; public enum SupportedProjectTargetType { DotNetCore, - DotNet5, - DotNet6, - DotNet7, - DotNet8, + DotNet5, // STS + DotNet6, // LTS + DotNet7, // STS + DotNet8, // LTS + DotNet9, // STS } \ No newline at end of file From 111c58353a9c51b3648e91a3f9dd1c9d5c6f8d48 Mon Sep 17 00:00:00 2001 From: Per Kops Date: Wed, 13 Nov 2024 10:53:01 +0100 Subject: [PATCH 5/5] test: fix CollectBaseRules unit-test --- .../Providers/MicrosoftCompilerErrorsProviderTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Atc.CodingRules.AnalyzerProviders.Tests/Providers/MicrosoftCompilerErrorsProviderTests.cs b/test/Atc.CodingRules.AnalyzerProviders.Tests/Providers/MicrosoftCompilerErrorsProviderTests.cs index c4fb7c7..712937f 100644 --- a/test/Atc.CodingRules.AnalyzerProviders.Tests/Providers/MicrosoftCompilerErrorsProviderTests.cs +++ b/test/Atc.CodingRules.AnalyzerProviders.Tests/Providers/MicrosoftCompilerErrorsProviderTests.cs @@ -21,6 +21,6 @@ public async Task CollectBaseRules(ProviderCollectingMode providerCollectingMode Assert.NotNull(actual.Name); Assert.Equal(MicrosoftCompilerErrorsProvider.Name, actual.Name); Assert.NotNull(actual.Rules); - Assert.True(actual.Rules.Count >= 910); + Assert.True(actual.Rules.Count >= 850); } } \ No newline at end of file