Skip to content

Commit

Permalink
Ensure that PlatformCompatibilityAnalyzer better handles various TFMs
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Jan 21, 2025
1 parent 45caa45 commit 3fc3836
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public sealed partial class PlatformCompatibilityAnalyzer : DiagnosticAnalyzer
private const string IsOSPlatform = nameof(IsOSPlatform);
private const string IsPrefix = "Is";
private const string OptionalSuffix = "VersionAtLeast";
private const string Net = "net";
private const string NetCoreAppIdentifier = ".NETCoreApp";
private const string macOS = nameof(macOS);
private const string OSX = nameof(OSX);
private const string MacSlashOSX = "macOS/OSX";
Expand Down Expand Up @@ -281,19 +281,34 @@ private static bool TryExtractPlatformName(string methodName, [NotNullWhen(true)

private static bool PlatformAnalysisAllowed(AnalyzerOptions options, Compilation compilation)
{
var tfmString = options.GetMSBuildPropertyValue(MSBuildPropertyOptionNames.TargetFramework, compilation);
string tfmIdentifer = options.GetMSBuildPropertyValue(MSBuildPropertyOptionNames.TargetFrameworkIdentifier, compilation) ?? "";

Check failure on line 284 in src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI (Ubuntu Debug)

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs#L284

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs(284,20): error IDE0059: (NETCORE_ENGINEERING_TELEMETRY=Build) Unnecessary assignment of a value to 'tfmIdentifer' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0059)

Check failure on line 284 in src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI (Ubuntu Release)

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs#L284

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs(284,20): error IDE0059: (NETCORE_ENGINEERING_TELEMETRY=Build) Unnecessary assignment of a value to 'tfmIdentifer' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0059)
string tfmVersion = options.GetMSBuildPropertyValue(MSBuildPropertyOptionNames.TargetFrameworkVersion, compilation) ?? "";

if (tfmString?.Length >= 4 &&
tfmString.StartsWith(Net, StringComparison.OrdinalIgnoreCase) &&
int.TryParse(tfmString[3].ToString(), out var major) &&
major >= 5)
if (tfmIdentifier.Equals(NetCoreAppIdentifier, StringComparison.OrdinalIgnoreCase) && tfmVersion.StartsWith("v", StringComparison.OrdinalIgnoreCase))

Check failure on line 287 in src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI (Ubuntu Debug)

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs#L287

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs(287,17): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'tfmIdentifier' does not exist in the current context

Check failure on line 287 in src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs

View check run for this annotation

Azure Pipelines / roslyn-analyzers-CI (Ubuntu Release)

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs#L287

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs(287,17): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'tfmIdentifier' does not exist in the current context
{
return true;
}
else
{
return LowerTargetsEnabled(options, compilation);
tfmVersion = tfmVersion[1..];

int majorVersion;

if (Version.TryParse(tfmVersion, out var version))
{
// The default scenario will always have at least two parts, such as v9.0 or v10.0
majorVersion = version.Major;
}
else
{
// Custom scenarios may only specify one part such as v9 or v10
_ = int.TryParse(tfmVersion, out majorVersion);
}

if (majorVersion >= 5)
{
return true;
}
}

// We want to fallback to allowing force enablement regardless of recognizing the Identifier/Version
return LowerTargetsEnabled(options, compilation);
}

private static bool LowerTargetsEnabled(AnalyzerOptions options, Compilation compilation) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public static IEnumerable<object[]> Create_DifferentTfms()
yield return new object[] { "build_property.TargetFramework = net5.0-ios14.0", true };
yield return new object[] { "build_property.TargetFramework = Net99", true };
yield return new object[] { "build_property.TargetFramework = netcoreapp5", false };
yield return new object[] { "build_property.TargetFramework = net10.0", true };
yield return new object[] { "build_property.TargetFramework = net10", true };
yield return new object[] { "build_property.TargetFramework = nonesense\nbuild_property.TargetFrameworkIdentifier=.NETCoreApp\nbuild_property.TargetFrameworkVersion=v11", true };
yield return new object[] { "build_property.TargetFramework = nonesense\nbuild_property.TargetFrameworkIdentifier=.NETCoreApp\nbuild_property.TargetFrameworkVersion=v11.0", true };
}

[Theory]
Expand Down
2 changes: 2 additions & 0 deletions src/Utilities/Compiler/Options/MSBuildPropertyOptionNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Analyzer.Utilities
internal static class MSBuildPropertyOptionNames
{
public const string TargetFramework = nameof(TargetFramework);
public const string TargetFrameworkIdentifier = nameof(TargetFrameworkIdentifier);
public const string TargetFrameworkVersion = nameof(TargetFrameworkVersion);
public const string TargetPlatformMinVersion = nameof(TargetPlatformMinVersion);
public const string UsingMicrosoftNETSdkWeb = nameof(UsingMicrosoftNETSdkWeb);
public const string ProjectTypeGuids = nameof(ProjectTypeGuids);
Expand Down

0 comments on commit 3fc3836

Please sign in to comment.