Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1407,14 +1407,18 @@ void CheckOperationAttributes(ISymbol symbol, bool checkParents)
platformSpecificOperations.TryAdd(new KeyValuePair<IOperation, ISymbol>(operation, symbol), (notSuppressedAttributes, callSiteAttributes.Platforms));
}
}
else if (TryCopyAttributesNotSuppressedByMsBuild(operationAttributes.Platforms!, msBuildPlatforms, out var copiedAttributes))
else if (!OperationHasOnlyAssemblyAttributesAndCalledFromSameAssembly(operationAttributes, symbol, containingSymbol) &&
TryCopyAttributesNotSuppressedByMsBuild(operationAttributes.Platforms!, msBuildPlatforms, out var copiedAttributes))
{
platformSpecificOperations.TryAdd(new KeyValuePair<IOperation, ISymbol>(operation, symbol), (copiedAttributes, null));
}
}
}
}

private static bool OperationHasOnlyAssemblyAttributesAndCalledFromSameAssembly(PlatformAttributes operationAttributes, ISymbol symbol, ISymbol containingSymbol) =>
operationAttributes.IsAssemblyAttribute && containingSymbol.ContainingAssembly == symbol.ContainingAssembly;

private static bool UsedInCreatingNotSupportedException(IArgumentOperation operation, ITypeSymbol? notSupportedExceptionType)
{
if (operation.Parent is IObjectCreationOperation creation &&
Expand Down Expand Up @@ -1799,11 +1803,7 @@ static void MergePlatformAttributes(ImmutableArray<AttributeData> immediateAttri

if (attribute.AttributeClass.Name is SupportedOSPlatformGuardAttribute or UnsupportedOSPlatformGuardAttribute)
{
if (!parentAttributes.IsAssemblyAttribute)
{
parentAttributes = new PlatformAttributes();
}

parentAttributes = new PlatformAttributes(); // The API is for guard, clear parent attributes
return;
}

Expand Down
2 changes: 0 additions & 2 deletions src/NetAnalyzers/RulesMissingDocumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

Rule ID | Missing Help Link | Title |
--------|-------------------|-------|
CA1871 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1871> | Do not pass a nullable struct to 'ArgumentNullException.ThrowIfNull' |
CA2022 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022> | Avoid inexact read with 'Stream.Read' |
CA2264 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2264> | Do not pass a non-nullable value to 'ArgumentNullException.ThrowIfNull' |
CA2265 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265> | Do not compare Span\<T> to 'null' or 'default' |
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -1216,6 +1217,76 @@ static void M1()
await VerifyAnalyzerCSAsync(csSource);
}

[Fact]
public async Task CallGuardFromPlatformSpecificAssembly()
{
string csDependencyCode = @"
public class Library
{
[System.Runtime.Versioning.SupportedOSPlatformGuard(""windows"")]
[System.Runtime.Versioning.SupportedOSPlatformGuard(""linux"")]
public static bool IsSupported => false;

public static void AMethod() { }
}";
csDependencyCode = @$"[assembly: System.Runtime.Versioning.SupportedOSPlatform(""windows"")]
[assembly: System.Runtime.Versioning.SupportedOSPlatform(""linux"")]
{csDependencyCode}";

string csCurrentAssemblyCode = @"
using System;

public class Program
{
public void ProgramMethod()
{
[|Library.AMethod()|]; // Not guarded, warns
if (Library.IsSupported)
{
Library.AMethod();
}
}
}";
var test = SetupDependencyAndTestCSWithOneSourceFile(csCurrentAssemblyCode, csDependencyCode);
test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", $@"root = true

[*]
build_property.TargetFramework = net5
"));
await test.RunAsync();
}

private static VerifyCS.Test SetupDependencyAndTestCSWithOneSourceFile(string csInput, string csDependencyCode)
{
return new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp10,
MarkupOptions = MarkupOptions.UseFirstDescriptor,
TestState =
{
Sources =
{
csInput
},
AdditionalProjects =
{
["PreviewAssembly"] =
{
Sources =
{
("/PreviewAssembly/AssemblyInfo.g.cs", csDependencyCode)
},
},
},
AdditionalProjectReferences =
{
"PreviewAssembly",
},
},
};
}

[Fact]
public async Task GuardedWith_RuntimeInformation_IsOSPlatform_SimpleIfElseAsync()
{
Expand Down Expand Up @@ -4181,7 +4252,7 @@ public async Task GuardCallingCachedValue_CallSiteHasAssemblyAttributeAsync()
[assembly: SupportedOSPlatform(""ios10.0"")]
class Test
{
static bool s_isiOS11OrNewer => false;
static bool s_isiOS11OrNewer = false;

[SupportedOSPlatformGuard(""ios11.0"")]
private bool IsIos11Supported() => s_isiOS11OrNewer; // should not warn
Expand Down