From 06f781a0a08c1a5d379acb8935f91553c2d8e18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Wed, 29 Nov 2023 09:08:14 -0500 Subject: [PATCH] MA0144: Improve dection of System.OperatingSystem (#655) --- ...ratingSystemInsteadOfRuntimeInformationAnalyzer.cs | 4 ++-- ...gSystemInsteadOfRuntimeInformationAnalyzerTests.cs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Meziantou.Analyzer/Rules/UseOperatingSystemInsteadOfRuntimeInformationAnalyzer.cs b/src/Meziantou.Analyzer/Rules/UseOperatingSystemInsteadOfRuntimeInformationAnalyzer.cs index 40221178b..4223f658a 100644 --- a/src/Meziantou.Analyzer/Rules/UseOperatingSystemInsteadOfRuntimeInformationAnalyzer.cs +++ b/src/Meziantou.Analyzer/Rules/UseOperatingSystemInsteadOfRuntimeInformationAnalyzer.cs @@ -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); diff --git a/tests/Meziantou.Analyzer.Test/Rules/UseOperatingSystemInsteadOfRuntimeInformationAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/UseOperatingSystemInsteadOfRuntimeInformationAnalyzerTests.cs index eccadcfcd..2e47e1c5f 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/UseOperatingSystemInsteadOfRuntimeInformationAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/UseOperatingSystemInsteadOfRuntimeInformationAnalyzerTests.cs @@ -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() {