Skip to content
New issue

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

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

Already on GitHub? # to your account

.NET7.0: Suppress CA1416 when wrapped in UIDevice.CheckSystemVersion #6955

Open
rolfbjarne opened this issue Aug 31, 2023 · 5 comments
Open
Assignees
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design
Milestone

Comments

@rolfbjarne
Copy link
Member

From @projectgoav on Tue, 29 Aug 2023 08:25:43 GMT

Steps to Reproduce

  1. Create a new blank iOS project
  2. Change SupportedOSPlatform in csproj to 12.0
  3. Add the following code somewhere
public void CA1416()
{
    if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
    {
        UIImage? foo = UIImage.GetSystemImage("foo");
    }
}

Expected Behavior

There should be no build warnings

Actual Behavior

Warning CA1416: This call site is reachable on: 'iOS' 12.0 and later, 'maccatalyst' 13.0 and later. 'UIImage.GetSystemImage(string)' is only supported on: 'ios' 13.0 and later. (CA1416)

I'd be nice if this warning was suppressed when the calls are wrapped in UIDevice.CheckSystemVersion. It'd be great if there was some check up the callstack to see if this method was suitably protected, but at the very least it'd be great if it were to check in the same scope before generating the warning.

It appears that adding the SupportedOSPlatform attribute to a method already supports this.

public void CA1416()
{
    if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
    {
        UIImage? foo = UIImage.GetSystemImage("foo");  // CA1416
	DoSomethingWithFoo();                          // No CA1416
    }
}

[SupportedOSPlatform("ios13.0")]
private void DoSomethingWithFoo()
{
    UIImage? foo = UIImage.GetSystemImage("foo");
}

Environment

Version information
Visual Studio Community 2022 for Mac
Version 17.6.3 (build 421)
Installation UUID: 0c6258bc-cd55-4cb9-bca6-aa2bc5ae3e73

Runtime
.NET 7.0.3 (64-bit)
Architecture: X64
Microsoft.macOS.Sdk 13.1.1007; git-rev-head:8afca776a0a96613dfb7200e0917bb57f9ed5583; git-branch:release/7.0.1xx-xcode14.2

Roslyn (Language Service)
4.6.0-3.23180.6+99e956e42697a6dd886d1e12478ea2b27cceacfa

NuGet
Version: 6.4.0.117

.NET SDK (x64)
SDK: /usr/local/share/dotnet/sdk/7.0.307/Sdks
SDK Versions:
	7.0.307
	7.0.302
	7.0.203
	7.0.102
	6.0.413
	6.0.408
	6.0.405
	6.0.401
	5.0.408
	5.0.400
	5.0.302
	5.0.301
	5.0.203
	5.0.202
	5.0.201
	5.0.103
	5.0.101
	5.0.100
	3.1.426
	3.1.423
	3.1.412
	3.1.411
	3.1.410
	3.1.409
	3.1.408
	3.1.407
	3.1.406
	3.1.404
	3.1.402
	3.1.401
	3.1.302
	3.1.301
	3.1.200
	3.1.102
	3.0.101
	3.0.100
	2.1.701
	2.1.700
	2.1.505
	2.1.504
	2.1.503
	2.1.302
	2.1.301
	2.1.4
	2.0.0
MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks

.NET Runtime (x64)
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	7.0.10
	7.0.5
	7.0.2
	6.0.21
	6.0.16
	6.0.13
	6.0.9
	5.0.17
	5.0.9
	5.0.8
	5.0.7
	5.0.6
	5.0.5
	5.0.4
	5.0.3
	5.0.1
	5.0.0
	3.1.32
	3.1.29
	3.1.18
	3.1.17
	3.1.16
	3.1.15
	3.1.14
	3.1.13
	3.1.12
	3.1.10
	3.1.8
	3.1.7
	3.1.6
	3.1.5
	3.1.2
	3.0.1
	3.0.0
	2.1.23
	2.1.22
	2.1.21
	2.1.20
	2.1.19
	2.1.16
	2.1.15
	2.1.14
	2.1.13
	2.1.12
	2.1.11
	2.1.9
	2.1.8
	2.1.7
	2.1.2
	2.1.1
	2.0.5
	2.0.0

Xamarin.Profiler
Version: 1.8.0.49
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater
Version: 11

Apple Developer Tools
Xcode: 14.3.1 21815
Build: 14E300c

Xamarin.Mac
Version: 9.3.0.6 Visual Studio Community
Hash: 97731c92c
Branch: xcode14.3
Build date: 2023-04-11 22:38:35-0400

Xamarin.iOS
Version: 16.4.0.6 Visual Studio Community
Hash: 97731c92c
Branch: xcode14.3
Build date: 2023-04-11 22:38:36-0400

Xamarin Designer
Version: 17.6.3.9
Hash: 2648399ae8
Branch: remotes/origin/d17-6
Build date: 2023-08-02 19:04:23 UTC

Xamarin.Android
Version: 13.2.1.2 (Visual Studio Community)
Commit: xamarin-android/d17-5/a8a26c7
Android SDK: /Users/ewan/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		9.0 (API level 28)

SDK Command-line Tools Version: 2.1
SDK Platform Tools Version: 30.0.4
SDK Build Tools Version: 29.0.2

Build Information: 
Mono: d9a6e87
Java.Interop: xamarin/java.interop/d17-5@149d70fe
SQLite: xamarin/sqlite/3.40.1@68c69d8
Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-5@ca1552d

Microsoft Build of OpenJDK
Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk
11.0.16.1
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Eclipse Temurin JDK
Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk
1.8.0.302
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager
Version: 17.6.0.50
Hash: a715dca
Branch: HEAD
Build date: 2023-08-02 19:04:28 UTC

Android Device Manager
Version: 0.0.0.1309
Hash: 06e3e77
Branch: HEAD
Build date: 2023-08-02 19:04:28 UTC

Build Information
Release ID: 1706030421
Git revision: c90350679f1b9295edc584519320b13d06489853
Build date: 2023-08-02 19:02:50+00
Build branch: release-17.6
Build lane: release-17.6

Operating System
Mac OS X 13.5.0
Darwin 22.6.0 Darwin Kernel Version 22.6.0
    Wed Jul  5 22:21:56 PDT 2023
    root:xnu-8796.141.3~6/RELEASE_X86_64 x86_64

Build Logs

Example Project (If Possible)

CA1416.zip

Copied from original issue dotnet/macios#18856

@rolfbjarne
Copy link
Member Author

From @rolfbjarne on Tue, 29 Aug 2023 17:22:29 GMT

The UIImage.GetSystemImage(string) method has these attributes:

[Export("systemImageNamed:")]
[SupportedOSPlatform("tvos13.0")]
[SupportedOSPlatform("ios13.0")]
[SupportedOSPlatform("maccatalyst")]
[BindingImpl((BindingImplOptions)3)]
public static UIImage? GetSystemImage(string name)

and UIDevice.CheckSystemVersion is supposed to handle this correctly, due to the SupportedOSPlatformGuard attributes:

https://github.com/xamarin/xamarin-macios/blob/30bf6db902fa7876bc308a2502e0c6d96a184536/src/UIKit/UIDevice.cs#L15-L20

It seems to be related to Mac Catalyst, because if I add a [SupportedOSPlatform ("maccatalyst")] attributed to the DoSomethingWithFoo method, then the caller gets a warning:

	public void CA1416()
	{
		if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
		{
			UIImage? foo = UIImage.GetSystemImage("foo");

			DoSomethingWithFoo(); // This reports: warning CA1416: This call site is reachable on: 'iOS' 12.0 and later, 'maccatalyst' 13.0 and later. 'AppDelegate.DoSomethingWithFoo()' is only supported on: 'ios' 13.0 and later.
		}
    }

	[SupportedOSPlatform("ios13.0")]
	[SupportedOSPlatform("maccatalyst")]
	private void DoSomethingWithFoo()
	{
		UIImage? foo = UIImage.GetSystemImage("foo");
	}

@buyaa-n any idea what might be going wrong here?

@ghost
Copy link

ghost commented Sep 1, 2023

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

Issue Details

From @projectgoav on Tue, 29 Aug 2023 08:25:43 GMT

Steps to Reproduce

  1. Create a new blank iOS project
  2. Change SupportedOSPlatform in csproj to 12.0
  3. Add the following code somewhere
public void CA1416()
{
    if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
    {
        UIImage? foo = UIImage.GetSystemImage("foo");
    }
}

Expected Behavior

There should be no build warnings

Actual Behavior

Warning CA1416: This call site is reachable on: 'iOS' 12.0 and later, 'maccatalyst' 13.0 and later. 'UIImage.GetSystemImage(string)' is only supported on: 'ios' 13.0 and later. (CA1416)

I'd be nice if this warning was suppressed when the calls are wrapped in UIDevice.CheckSystemVersion. It'd be great if there was some check up the callstack to see if this method was suitably protected, but at the very least it'd be great if it were to check in the same scope before generating the warning.

It appears that adding the SupportedOSPlatform attribute to a method already supports this.

public void CA1416()
{
    if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
    {
        UIImage? foo = UIImage.GetSystemImage("foo");  // CA1416
	DoSomethingWithFoo();                          // No CA1416
    }
}

[SupportedOSPlatform("ios13.0")]
private void DoSomethingWithFoo()
{
    UIImage? foo = UIImage.GetSystemImage("foo");
}

Environment

Version information
Visual Studio Community 2022 for Mac
Version 17.6.3 (build 421)
Installation UUID: 0c6258bc-cd55-4cb9-bca6-aa2bc5ae3e73

Runtime
.NET 7.0.3 (64-bit)
Architecture: X64
Microsoft.macOS.Sdk 13.1.1007; git-rev-head:8afca776a0a96613dfb7200e0917bb57f9ed5583; git-branch:release/7.0.1xx-xcode14.2

Roslyn (Language Service)
4.6.0-3.23180.6+99e956e42697a6dd886d1e12478ea2b27cceacfa

NuGet
Version: 6.4.0.117

.NET SDK (x64)
SDK: /usr/local/share/dotnet/sdk/7.0.307/Sdks
SDK Versions:
	7.0.307
	7.0.302
	7.0.203
	7.0.102
	6.0.413
	6.0.408
	6.0.405
	6.0.401
	5.0.408
	5.0.400
	5.0.302
	5.0.301
	5.0.203
	5.0.202
	5.0.201
	5.0.103
	5.0.101
	5.0.100
	3.1.426
	3.1.423
	3.1.412
	3.1.411
	3.1.410
	3.1.409
	3.1.408
	3.1.407
	3.1.406
	3.1.404
	3.1.402
	3.1.401
	3.1.302
	3.1.301
	3.1.200
	3.1.102
	3.0.101
	3.0.100
	2.1.701
	2.1.700
	2.1.505
	2.1.504
	2.1.503
	2.1.302
	2.1.301
	2.1.4
	2.0.0
MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks

.NET Runtime (x64)
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	7.0.10
	7.0.5
	7.0.2
	6.0.21
	6.0.16
	6.0.13
	6.0.9
	5.0.17
	5.0.9
	5.0.8
	5.0.7
	5.0.6
	5.0.5
	5.0.4
	5.0.3
	5.0.1
	5.0.0
	3.1.32
	3.1.29
	3.1.18
	3.1.17
	3.1.16
	3.1.15
	3.1.14
	3.1.13
	3.1.12
	3.1.10
	3.1.8
	3.1.7
	3.1.6
	3.1.5
	3.1.2
	3.0.1
	3.0.0
	2.1.23
	2.1.22
	2.1.21
	2.1.20
	2.1.19
	2.1.16
	2.1.15
	2.1.14
	2.1.13
	2.1.12
	2.1.11
	2.1.9
	2.1.8
	2.1.7
	2.1.2
	2.1.1
	2.0.5
	2.0.0

Xamarin.Profiler
Version: 1.8.0.49
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater
Version: 11

Apple Developer Tools
Xcode: 14.3.1 21815
Build: 14E300c

Xamarin.Mac
Version: 9.3.0.6 Visual Studio Community
Hash: 97731c92c
Branch: xcode14.3
Build date: 2023-04-11 22:38:35-0400

Xamarin.iOS
Version: 16.4.0.6 Visual Studio Community
Hash: 97731c92c
Branch: xcode14.3
Build date: 2023-04-11 22:38:36-0400

Xamarin Designer
Version: 17.6.3.9
Hash: 2648399ae8
Branch: remotes/origin/d17-6
Build date: 2023-08-02 19:04:23 UTC

Xamarin.Android
Version: 13.2.1.2 (Visual Studio Community)
Commit: xamarin-android/d17-5/a8a26c7
Android SDK: /Users/ewan/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		9.0 (API level 28)

SDK Command-line Tools Version: 2.1
SDK Platform Tools Version: 30.0.4
SDK Build Tools Version: 29.0.2

Build Information: 
Mono: d9a6e87
Java.Interop: xamarin/java.interop/d17-5@149d70fe
SQLite: xamarin/sqlite/3.40.1@68c69d8
Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-5@ca1552d

Microsoft Build of OpenJDK
Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk
11.0.16.1
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Eclipse Temurin JDK
Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk
1.8.0.302
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager
Version: 17.6.0.50
Hash: a715dca
Branch: HEAD
Build date: 2023-08-02 19:04:28 UTC

Android Device Manager
Version: 0.0.0.1309
Hash: 06e3e77
Branch: HEAD
Build date: 2023-08-02 19:04:28 UTC

Build Information
Release ID: 1706030421
Git revision: c90350679f1b9295edc584519320b13d06489853
Build date: 2023-08-02 19:02:50+00
Build branch: release-17.6
Build lane: release-17.6

Operating System
Mac OS X 13.5.0
Darwin 22.6.0 Darwin Kernel Version 22.6.0
    Wed Jul  5 22:21:56 PDT 2023
    root:xnu-8796.141.3~6/RELEASE_X86_64 x86_64

Build Logs

Example Project (If Possible)

CA1416.zip

Copied from original issue dotnet/macios#18856

Author: rolfbjarne
Assignees: -
Labels:

area-Meta, untriaged, needs-area-label

Milestone: -

@buyaa-n
Copy link
Contributor

buyaa-n commented Sep 1, 2023

and UIDevice.CheckSystemVersion is supposed to handle this correctly, due to the SupportedOSPlatformGuard attributes:

What SupportedOSPlatformGuard attributes it has?

  [SupportedOSPlatform("ios13.0")]
  [SupportedOSPlatform("maccatalyst")]
  private void DoSomethingWithFoo()

As you know [SupportedOSPlatform("ios13.0")] means supported on ios 13.0+ and maccatalyst 13.0+, for what the [SupportedOSPlatform("maccatalyst")] is added? Is the API supported ios 13.0+ but maccatalyst all versions?

@rolfbjarne
Copy link
Member Author

Is the API supported ios 13.0+ but maccatalyst all versions?

Correct.

@buyaa-n buyaa-n added the Bug The product is not behaving according to its current intended design label Sep 5, 2023
@ghost ghost removed the untriaged label Sep 5, 2023
@buyaa-n buyaa-n transferred this issue from dotnet/runtime Sep 19, 2023
@mavasani mavasani added this to the .NET vNext milestone Oct 11, 2023
@buyaa-n
Copy link
Contributor

buyaa-n commented Feb 10, 2025

This looks has same root cause as #7239

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design
Projects
None yet
Development

No branches or pull requests

3 participants