Skip to content

Commit

Permalink
Use Array.Exists, and Array.TrueForAll instead of LINQ.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Apr 8, 2024
1 parent ec6d19c commit 94fd159
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions .globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ dotnet_diagnostic.SA1623.severity = none
dotnet_diagnostic.SA1633.severity = none
dotnet_diagnostic.SA1642.severity = none
dotnet_diagnostic.SA1648.severity = none
dotnet_diagnostic.SA1649.severity = none # File name should match first type name.

dotnet_diagnostic.RCS1005.severity = silent
dotnet_diagnostic.RCS1006.severity = silent
Expand Down
4 changes: 2 additions & 2 deletions src/Buildalyzer/Construction/ProjectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ internal ProjectFile(string path)

/// <inheritdoc />
public bool RequiresNetFramework =>
_projectElement.GetDescendants(ProjectFileNames.Import).Any(x => ImportsThatRequireNetFramework.Any(i => x.GetAttributeValue(ProjectFileNames.Project)?.EndsWith(i, StringComparison.OrdinalIgnoreCase) ?? false))
|| _projectElement.GetDescendants(ProjectFileNames.LanguageTargets).Any(x => ImportsThatRequireNetFramework.Any(i => x.Value.EndsWith(i, StringComparison.OrdinalIgnoreCase)))
_projectElement.GetDescendants(ProjectFileNames.Import).Any(x => ImportsThatRequireNetFramework.Exists(i => x.GetAttributeValue(ProjectFileNames.Project)?.EndsWith(i, StringComparison.OrdinalIgnoreCase) ?? false))
|| _projectElement.GetDescendants(ProjectFileNames.LanguageTargets).Any(x => ImportsThatRequireNetFramework.Exists(i => x.Value.EndsWith(i, StringComparison.OrdinalIgnoreCase)))
|| ToolsVersion != null;

/// <inheritdoc />
Expand Down
6 changes: 4 additions & 2 deletions src/Buildalyzer/Environment/EnvironmentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ private bool GetFrameworkMsBuildExePath(out string msBuildExePath)
return !string.IsNullOrEmpty(msBuildExePath);
}

private bool OnlyTargetsFramework(string targetFramework) =>
targetFramework == null ? _projectFile.TargetFrameworks.All(x => IsFrameworkTargetFramework(x)) : IsFrameworkTargetFramework(targetFramework);
private bool OnlyTargetsFramework(string targetFramework)
=> targetFramework == null
? _projectFile.TargetFrameworks.TrueForAll(IsFrameworkTargetFramework)
: IsFrameworkTargetFramework(targetFramework);

// Internal for testing
// Because the .NET Core/.NET 5 TFMs are better defined, we just check if this is one of them and then negate
Expand Down
2 changes: 1 addition & 1 deletion src/Buildalyzer/Environment/MsBuildProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static class MsBuildProperties
{
[DesignTimeBuild] = "true",

// Supports Framework projects: https://github.com/dotnet/project-system/blob/main/docs/design-time-builds.md#determining-whether-a-target-is-running-in-a-design-time-build
// Supports Framework projects: https://github.com/dotnet/project-system/blob/main/docs/design-time-builds.md#determining-whether-a-target-is-running-in-a-design-time-build
[BuildingProject] = "false",
[BuildProjectReferences] = "false",
[SkipCompilerExecution] = "true",
Expand Down
14 changes: 14 additions & 0 deletions src/Buildalyzer/Extensions/System.Array.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace System;

internal static class BuildalyzerArrayExtensions
{
/// <inheritdoc cref="Array.Exists{T}(T[], Predicate{T})"/>>
[Pure]
public static bool Exists<T>(this T[] array, Predicate<T> match)
=> Array.Exists(array, match);

/// <inheritdoc cref="Array.TrueForAll{T}(T[], Predicate{T})"/>>
[Pure]
public static bool TrueForAll<T>(this T[] array, Predicate<T> match)
=> Array.TrueForAll(array, match);
}
2 changes: 1 addition & 1 deletion src/Buildalyzer/ProjectAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private static string FormatArgument(string argument)
argument = argument.Replace("\"", "\\\"");

// Also escape trailing slashes so they don't escape the closing quote
if (argument.EndsWith("\\"))
if (argument.EndsWith('\\'))
{
argument = $"{argument}\\";
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Buildalyzer.Tests/IO/IOPathFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public void Is_case_insensitive_on_windows()
#endif

[Test]
public void is_seperator_agnostic()
public void Is_seperator_agnostic()
=> IOPath.Parse(".\\root\\test\\somefile.txt").Should().Be(IOPath.Parse("./root/test/somefile.txt"));

[TestCase(@"c:\Program Files\Buildalyzer")]
public void supports_type_conversion(IOPath path)
public void Supports_type_conversion(IOPath path)
=> path.Should().Be(IOPath.Parse(@"c:\Program Files\Buildalyzer"));
}

0 comments on commit 94fd159

Please sign in to comment.