Skip to content

Commit

Permalink
MA0144: Improve dection of System.OperatingSystem (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou authored Nov 29, 2023
1 parent 1724838 commit 06f781a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public override void Initialize(AnalysisContext context)
{
var isOSPlatformSymbol = DocumentationCommentId.GetFirstSymbolForDeclarationId("M:System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform)", context.Compilation) as IMethodSymbol;
var osPlatformSymbol = context.Compilation.GetBestTypeByMetadataName("System.Runtime.InteropServices.OSPlatform");
var operatingSystemSymbol = context.Compilation.GetBestTypeByMetadataName("System.OperatingSystem");
if (isOSPlatformSymbol is null || operatingSystemSymbol is null || osPlatformSymbol is null)
var operatingSystemSymbol = DocumentationCommentId.GetFirstSymbolForDeclarationId("M:System.OperatingSystem.IsWindows", context.Compilation);
if (isOSPlatformSymbol is null || operatingSystemSymbol is null || !context.Compilation.IsSymbolAccessibleWithin(operatingSystemSymbol, context.Compilation.Assembly) || osPlatformSymbol is null)
return;
context.RegisterOperationAction(context => AnalyzeInvocation(context, isOSPlatformSymbol, osPlatformSymbol), OperationKind.Invocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Fact]
public async Task ShouldNotReport_WhenOperatingSystemIsNotAvailable()
{
await CreateProjectBuilder()
.WithTargetFramework(TargetFramework.NetStandard2_0)
.WithSourceCode("""
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
""")
.ValidateAsync();
}

[Fact]
public async Task ShouldNotReport_WhenDynamic()
{
Expand Down

0 comments on commit 06f781a

Please sign in to comment.