Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs found in MAUI repo #6361

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -563,10 +563,13 @@ static bool IsKnownValueGuarded(
}
}

if (attribute.SupportedFirst != null &&
info.Version.IsGreaterThanOrEqualTo(attribute.SupportedFirst))
var checkVersion = attribute.SupportedSecond ?? attribute.SupportedFirst;

if (checkVersion != null &&
info.Version.IsGreaterThanOrEqualTo(checkVersion))
{
attribute.SupportedFirst = null;
attribute.SupportedSecond = null;
RemoveUnsupportedWithLessVersion(info.Version, attribute);
RemoveOtherSupportsOnDifferentPlatforms(attributes, info.PlatformName);
}
Expand Down Expand Up @@ -816,8 +819,11 @@ static void ReportSupportedDiagnostic(IOperation operation, OperationBlockAnalys
csPlatformNames = string.Join(CommaSeparator, csPlatformNames, PlatformCompatibilityAllPlatforms);
}

var rule = supportedRule ? SwitchSupportedRule(callsite) : SwitchRule(callsite, true);
context.ReportDiagnostic(operation.CreateDiagnostic(rule, operationName, JoinNames(platformNames), csPlatformNames));
if (!platformNames.IsEmpty)
{
var rule = supportedRule ? SwitchSupportedRule(callsite) : SwitchRule(callsite, true);
context.ReportDiagnostic(operation.CreateDiagnostic(rule, operationName, JoinNames(platformNames), csPlatformNames));
}

if (!obsoletedPlatforms.IsEmpty)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,37 @@ public void ObsoletedOnLinuxAndWindows10() { }
await VerifyAnalyzerCSAsync(csSource, s_msBuildPlatforms);
}

[Fact]
public async Task CalledApiHasSupportedAndObsoletedAttributes_CallsiteSupressesSupportedAttributeWarnsForObsoletedOnly()
{
var source = @"
using System;
using System.Runtime.Versioning;

class Program
{
[Mock.ObsoletedOSPlatform(""ios7.0"", ""Use 'NSString.GetBoundingRect (CGSize, NSStringDrawingOptions, UIStringAttributes, NSStringDrawingContext)' instead."")]
[Mock.ObsoletedOSPlatform(""maccatalyst7.0"", ""Use 'NSString.GetBoundingRect (CGSize, NSStringDrawingOptions, UIStringAttributes, NSStringDrawingContext)' instead."")]
[SupportedOSPlatform(""ios"")]
[SupportedOSPlatform(""maccatalyst"")]
public static void M3() { }

[SupportedOSPlatform(""ios"")]
public static void M1()
{
{|CA1422:M3()|}; // This call site is reachable on: 'ios', 'maccatalyst'. 'Program.M3()' is obsoleted on: 'ios' 7.0 and later (Use 'NSString.GetBoundingRect (CGSize, NSStringDrawingOptions, UIStringAttributes, NSStringDrawingContext)' instead.), 'maccatalyst' 7.0 and later (Use 'NSString.GetBoundingRect (CGSize, NSStringDrawingOptions, UIStringAttributes, NSStringDrawingContext)' instead.).
}

[SupportedOSPlatform(""ios10.0"")]
public static void M2()
{
{|CA1422:M3()|}; // This call site is reachable on: 'ios' 10.0 and later, 'maccatalyst' 10.0 and later. 'Program.M3()' is obsoleted on: 'ios' 7.0 and later (Use 'NSString.GetBoundingRect (CGSize, NSStringDrawingOptions, UIStringAttributes, NSStringDrawingContext)' instead.), 'maccatalyst' 7.0 and later (Use 'NSString.GetBoundingRect (CGSize, NSStringDrawingOptions, UIStringAttributes, NSStringDrawingContext)' instead.).
}
}" + MockObsoletedAttributeCS;

await VerifyAnalyzerCSAsync(source);
}

private readonly string MockObsoletedAttributeCS = @"
namespace Mock
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4968,6 +4968,39 @@ void M1()
await VerifyAnalyzerCSAsync(source);
}

[Fact, WorkItem(4372, "https://github.com/dotnet/roslyn-analyzers/issues/6158")]
public async Task ChildApiNarrowedParentSupport_GuardingVersionShouldBeComparedWithChildVersion()
{
var source = @"
using System;
using System.Runtime.Versioning;

[SupportedOSPlatform(""ios"")]
[SupportedOSPlatform(""tvos"")]
[SupportedOSPlatform(""maccatalyst"")]
class Program
{
[SupportedOSPlatform(""tvos10.2"")]
[SupportedOSPlatform(""ios10.3"")]
[SupportedOSPlatform(""maccatalyst10.3"")]
public static int P1 => 1;
}
class Test
{
[SupportedOSPlatform(""ios10.0"")]
public void M1()
{
var rate = (OperatingSystem.IsIOSVersionAtLeast(10, 3) || OperatingSystem.IsMacCatalystVersionAtLeast(10, 3) || OperatingSystem.IsTvOSVersionAtLeast(10, 3))
? Program.P1 : 0; // guarded

if (OperatingSystem.IsIOSVersionAtLeast(10, 3) || OperatingSystem.IsMacCatalystVersionAtLeast(10, 3) || OperatingSystem.IsTvOSVersionAtLeast(10))
rate = [|Program.P1|]; // version of TvOS is not guarded
}
}";

await VerifyAnalyzerCSAsync(source);
}

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