Skip to content

Commit f10e7f2

Browse files
authored
Fix bugs found in MAUI repo (#6361)
1 parent 8a04420 commit f10e7f2

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,13 @@ static bool IsKnownValueGuarded(
564564
}
565565
}
566566

567-
if (attribute.SupportedFirst != null &&
568-
info.Version.IsGreaterThanOrEqualTo(attribute.SupportedFirst))
567+
var checkVersion = attribute.SupportedSecond ?? attribute.SupportedFirst;
568+
569+
if (checkVersion != null &&
570+
info.Version.IsGreaterThanOrEqualTo(checkVersion))
569571
{
570572
attribute.SupportedFirst = null;
573+
attribute.SupportedSecond = null;
571574
RemoveUnsupportedWithLessVersion(info.Version, attribute);
572575
RemoveOtherSupportsOnDifferentPlatforms(attributes, info.PlatformName);
573576
}
@@ -820,8 +823,11 @@ static void ReportSupportedDiagnostic(IOperation operation, OperationBlockAnalys
820823
csPlatformNames = string.Join(CommaSeparator, csPlatformNames, PlatformCompatibilityAllPlatforms);
821824
}
822825

823-
var rule = supportedRule ? SwitchSupportedRule(callsite) : SwitchRule(callsite, true);
824-
context.ReportDiagnostic(operation.CreateDiagnostic(rule, operationName, JoinNames(platformNames), csPlatformNames));
826+
if (!platformNames.IsEmpty)
827+
{
828+
var rule = supportedRule ? SwitchSupportedRule(callsite) : SwitchRule(callsite, true);
829+
context.ReportDiagnostic(operation.CreateDiagnostic(rule, operationName, JoinNames(platformNames), csPlatformNames));
830+
}
825831

826832
if (!obsoletedPlatforms.IsEmpty)
827833
{

src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.ObsoletedOSPlatformTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,37 @@ public void ObsoletedOnLinuxAndWindows10() { }
453453
await VerifyAnalyzerCSAsync(csSource, s_msBuildPlatforms);
454454
}
455455

456+
[Fact]
457+
public async Task CalledApiHasSupportedAndObsoletedAttributes_CallsiteSupressesSupportedAttributeWarnsForObsoletedOnly()
458+
{
459+
var source = @"
460+
using System;
461+
using System.Runtime.Versioning;
462+
463+
class Program
464+
{
465+
[Mock.ObsoletedOSPlatform(""ios7.0"", ""Use 'NSString.GetBoundingRect (CGSize, NSStringDrawingOptions, UIStringAttributes, NSStringDrawingContext)' instead."")]
466+
[Mock.ObsoletedOSPlatform(""maccatalyst7.0"", ""Use 'NSString.GetBoundingRect (CGSize, NSStringDrawingOptions, UIStringAttributes, NSStringDrawingContext)' instead."")]
467+
[SupportedOSPlatform(""ios"")]
468+
[SupportedOSPlatform(""maccatalyst"")]
469+
public static void M3() { }
470+
471+
[SupportedOSPlatform(""ios"")]
472+
public static void M1()
473+
{
474+
{|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.).
475+
}
476+
477+
[SupportedOSPlatform(""ios10.0"")]
478+
public static void M2()
479+
{
480+
{|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.).
481+
}
482+
}" + MockObsoletedAttributeCS;
483+
484+
await VerifyAnalyzerCSAsync(source);
485+
}
486+
456487
private readonly string MockObsoletedAttributeCS = @"
457488
namespace Mock
458489
{

src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzerTests.GuardedCallsTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4968,6 +4968,39 @@ void M1()
49684968
await VerifyAnalyzerCSAsync(source);
49694969
}
49704970

4971+
[Fact, WorkItem(4372, "https://github.com/dotnet/roslyn-analyzers/issues/6158")]
4972+
public async Task ChildApiNarrowedParentSupport_GuardingVersionShouldBeComparedWithChildVersion()
4973+
{
4974+
var source = @"
4975+
using System;
4976+
using System.Runtime.Versioning;
4977+
4978+
[SupportedOSPlatform(""ios"")]
4979+
[SupportedOSPlatform(""tvos"")]
4980+
[SupportedOSPlatform(""maccatalyst"")]
4981+
class Program
4982+
{
4983+
[SupportedOSPlatform(""tvos10.2"")]
4984+
[SupportedOSPlatform(""ios10.3"")]
4985+
[SupportedOSPlatform(""maccatalyst10.3"")]
4986+
public static int P1 => 1;
4987+
}
4988+
class Test
4989+
{
4990+
[SupportedOSPlatform(""ios10.0"")]
4991+
public void M1()
4992+
{
4993+
var rate = (OperatingSystem.IsIOSVersionAtLeast(10, 3) || OperatingSystem.IsMacCatalystVersionAtLeast(10, 3) || OperatingSystem.IsTvOSVersionAtLeast(10, 3))
4994+
? Program.P1 : 0; // guarded
4995+
4996+
if (OperatingSystem.IsIOSVersionAtLeast(10, 3) || OperatingSystem.IsMacCatalystVersionAtLeast(10, 3) || OperatingSystem.IsTvOSVersionAtLeast(10))
4997+
rate = [|Program.P1|]; // version of TvOS is not guarded
4998+
}
4999+
}";
5000+
5001+
await VerifyAnalyzerCSAsync(source);
5002+
}
5003+
49715004
[Fact]
49725005
public async Task ApiAndGuardAttributeBothHasVersions_AttributeVersionWins()
49735006
{

0 commit comments

Comments
 (0)