Skip to content

Commit

Permalink
Solution improvements
Browse files Browse the repository at this point in the history
- Enable (almost) all built-in style analyzers.
- Fix new style warnings.
- Use global package references.
- Add reference trimmer analyzer.
  • Loading branch information
martincostello committed Mar 29, 2024
1 parent cf56e6b commit addf853
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 17 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ charset = utf-8-bom
# .NET Coding Conventions #
###############################
[*.{cs,vb}]

# Enable style analyzers
dotnet_analyzer_diagnostic.category-Style.severity = warning

dotnet_diagnostic.IDE0005.severity = silent
dotnet_diagnostic.IDE0045.severity = silent
dotnet_diagnostic.IDE0046.severity = silent
dotnet_diagnostic.IDE0058.severity = silent
dotnet_diagnostic.IDE0072.severity = silent
dotnet_diagnostic.IDE0079.severity = silent

# Organize usings
dotnet_sort_system_directives_first = true

Expand Down Expand Up @@ -81,6 +92,7 @@ csharp_style_expression_bodied_operators = false:none
csharp_style_expression_bodied_properties = true:none
csharp_style_expression_bodied_indexers = true:none
csharp_style_expression_bodied_accessors = true:none
csharp_style_expression_bodied_local_functions = when_on_single_line

# Pattern matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
Expand Down
8 changes: 6 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
</ItemGroup>
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisMode>All</AnalysisMode>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)ProjectEuler.snk</AssemblyOriginatorKeyFile>
<Authors>martin_costello</Authors>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)ProjectEuler.ruleset</CodeAnalysisRuleSet>
<Company>https://github.com/martincostello/project-euler</Company>
<Copyright>Martin Costello (c) 2015-$([System.DateTime]::UtcNow.ToString(yyyy))</Copyright>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<GenerateGitMetadata Condition=" '$(CI)' != '' and '$(GenerateGitMetadata)' == '' ">true</GenerateGitMetadata>
Expand All @@ -20,7 +21,6 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<NeutralLanguage>en-US</NeutralLanguage>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<NoWarn Condition=" '$(GenerateDocumentationFile)' != 'true' ">$(NoWarn);SA0001</NoWarn>
<Nullable>enable</Nullable>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/martincostello/project-euler</PackageProjectUrl>
Expand All @@ -39,6 +39,10 @@
<VersionPrefix>8.0.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
<PropertyGroup Condition=" '$(EnableReferenceTrimmer)' != 'false' and '$(GenerateDocumentationFile)' != 'true' ">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);419;1570;1573;1574;1584;1591;SA0001;SA1602</NoWarn>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Globalization" />
</ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project>
<ItemGroup>
<GlobalPackageReference Include="ReferenceTrimmer" Version="3.3.3" />
<GlobalPackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" />
<PackageVersion Include="BenchmarkDotNet.TestAdapter" Version="0.13.12" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.3.3" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
<PackageVersion Include="xunit" Version="2.7.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7" />
</ItemGroup>
<ItemGroup Condition=" '$(UseSharedCompilation)' != 'false' and '$(BuildInParallel)' != 'false' ">
<PackageReference Include="StyleCop.Analyzers" PrivateAssets="All" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion ProjectEuler.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{AD52E729-49AF-4F5C-9611-DD51EFED3727}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build.yml = .github\workflows\build.yml
.github\workflows\update-dotnet-sdk.yml = .github\workflows\update-dotnet-sdk.yml
.github\workflows\dependency-review.yml = .github\workflows\dependency-review.yml
.github\workflows\lint.yml = .github\workflows\lint.yml
EndProjectSection
EndProject
Global
Expand Down
2 changes: 1 addition & 1 deletion src/ProjectEuler/Puzzles/MaximumPathPuzzle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected int[][] LoadTriangle()
lines++;
}

return [..triangle];
return [.. triangle];
}

/// <inheritdoc />
Expand Down
10 changes: 5 additions & 5 deletions src/ProjectEuler/Puzzles/Puzzle054.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ internal static bool IsFirstPlayerTheWinner(string hand)
string[] player1 = cards[..5];
string[] player2 = cards[5..];

player1 = [..player1.OrderBy(GetCardValue)];
player2 = [..player2.OrderBy(GetCardValue)];
player1 = [.. player1.OrderBy(GetCardValue)];
player2 = [.. player2.OrderBy(GetCardValue)];

var predicates = new IsHandPredicate[]
{
IsHandPredicate[] predicates =
[
IsRoyalFlush,
IsStraightFlush,
IsFourOfAKind,
Expand All @@ -38,7 +38,7 @@ internal static bool IsFirstPlayerTheWinner(string hand)
IsThreeOfAKind,
IsTwoPair,
IsPair,
};
];

foreach (var predicate in predicates)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ProjectEuler/Puzzles/Puzzle092.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override int SolveCore(string[] args)
{
int value = i;

while (value != 1 && value != Target)
while (value is not 1 and not Target)
{
value = Maths.Digits(value).Sum((p) => p * p);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ProjectEuler.Tests/MathsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public static void Maths_Permutations_Returns_Correct_Value_2()
Assert.Equal(new[] { "a", "b" }, actual[0]);
Assert.Equal(new[] { "b", "a" }, actual[1]);

collection = new[] { "b", "a" };
collection = ["b", "a"];

// Act
actual = Maths.Permutations(collection)
Expand Down
2 changes: 1 addition & 1 deletion tests/ProjectEuler.Tests/ProjectEuler.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ProjectReference Include="..\ProjectEuler.Benchmarks\ProjectEuler.Benchmarks.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" />
<PackageReference Include="GitHubActionsTestLogger" NoWarn="RT0003" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
Expand Down
2 changes: 1 addition & 1 deletion tests/ProjectEuler.Tests/PuzzleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void Puzzles_Have_Questions()
var puzzleTypes = puzzleType.GetTypeInfo().Assembly
.GetTypes()
.Where((p) => !p.GetTypeInfo().IsAbstract)
.Where((p) => puzzleType.IsAssignableFrom(p))
.Where(puzzleType.IsAssignableFrom)
.ToList();

Assert.NotEmpty(puzzleTypes);
Expand Down

0 comments on commit addf853

Please sign in to comment.