Cause
An API that's marked with System.Runtime.Versioning.ObsoletedOSPlatformAttribute is called from a call site that's marked as supporting the obsoleted operating system (OS). This rule is similar to CA1416: Validate platform compatibility except that it warns about APIs that are obsolete on a given platform versus unsupported entirely.
Rule description
Calling an API that's obsolete in a given OS (version) from a call site that's reachable from that OS (version) is not recommended. Consider calling a non-obsolete API instead, or guarding against calling the obsolete API on affected operating systems.
How to fix violations
There are various ways to fix a violation of this rule:
- Restrict the call site to operating systems that don't include the obsoleted version by marking it with System.Runtime.Versioning.UnsupportedOSPlatformAttribute or System.Runtime.Versioning.ObsoletedOSPlatformAttribute.
- Guard the call using System.OperatingSystem APIs, for example,
if (!OperatingSystem.IsLinux()). - Guard the call using an API that's annotated with System.Runtime.Versioning.UnsupportedOSPlatformGuardAttribute or negated System.Runtime.Versioning.SupportedOSPlatformGuardAttribute.
Example
The following code snippet shows a violation of CA1422:
sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1422.csSnippet1
using System.Runtime.Versioning;
public class Violation
{
// <Snippet1>
[SupportedOSPlatform("Windows")]
public void M1()
{
// Violates rule CA1422.
// This call site is reachable on 'Windows',
// but 'ObsoletedOnWindows62()'
// is obsoleted on 'Windows 6.2' and later.
ObsoletedOnWindows62();
}
[ObsoletedOSPlatform("Windows6.2")]
public void ObsoletedOnWindows62()
{ }
// </Snippet1>
}
public class Fix
{
// <Snippet2>
[SupportedOSPlatform("Windows")]
[ObsoletedOSPlatform("Windows6.2")]
public void M1()
{
ObsoletedOnWindows62();
}
[ObsoletedOSPlatform("Windows6.2")]
public void ObsoletedOnWindows62()
{ }
// </Snippet2>
}sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1422.vbSnippet1
Imports System.Runtime.Versioning
Public Class Violation
' <Snippet1>
<SupportedOSPlatform("Windows")>
Public Sub M1()
' Violates rules CA1422.
' This call site is reachable on 'Windows',
' but 'ObsoletedOnWindows62()'
' is obsoleted on 'Windows 6.2' and later.
ObsoletedOnWindows62()
End Sub
<ObsoletedOSPlatform("Windows6.2")>
Public Sub ObsoletedOnWindows62()
End Sub
' </Snippet1>
End Class
Public Class Fix
' <Snippet2>
<SupportedOSPlatform("Windows")>
<ObsoletedOSPlatform("Windows6.2")>
Public Sub M1()
ObsoletedOnWindows62()
End Sub
<ObsoletedOSPlatform("Windows6.2")>
Public Sub ObsoletedOnWindows62()
End Sub
' </Snippet2>
End ClassThe following code snippet fixes the violation by adding to the call site an System.Runtime.Versioning.UnsupportedOSPlatformAttribute attribute that specifies the version the called method was obsoleted in.
sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1422.csSnippet2
using System.Runtime.Versioning;
public class Violation
{
// <Snippet1>
[SupportedOSPlatform("Windows")]
public void M1()
{
// Violates rule CA1422.
// This call site is reachable on 'Windows',
// but 'ObsoletedOnWindows62()'
// is obsoleted on 'Windows 6.2' and later.
ObsoletedOnWindows62();
}
[ObsoletedOSPlatform("Windows6.2")]
public void ObsoletedOnWindows62()
{ }
// </Snippet1>
}
public class Fix
{
// <Snippet2>
[SupportedOSPlatform("Windows")]
[ObsoletedOSPlatform("Windows6.2")]
public void M1()
{
ObsoletedOnWindows62();
}
[ObsoletedOSPlatform("Windows6.2")]
public void ObsoletedOnWindows62()
{ }
// </Snippet2>
}sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1422.vbSnippet2
Imports System.Runtime.Versioning
Public Class Violation
' <Snippet1>
<SupportedOSPlatform("Windows")>
Public Sub M1()
' Violates rules CA1422.
' This call site is reachable on 'Windows',
' but 'ObsoletedOnWindows62()'
' is obsoleted on 'Windows 6.2' and later.
ObsoletedOnWindows62()
End Sub
<ObsoletedOSPlatform("Windows6.2")>
Public Sub ObsoletedOnWindows62()
End Sub
' </Snippet1>
End Class
Public Class Fix
' <Snippet2>
<SupportedOSPlatform("Windows")>
<ObsoletedOSPlatform("Windows6.2")>
Public Sub M1()
ObsoletedOnWindows62()
End Sub
<ObsoletedOSPlatform("Windows6.2")>
Public Sub ObsoletedOnWindows62()
End Sub
' </Snippet2>
End ClassWhen to suppress warnings
It's safe to suppress a warning from this rule if you're not concerned about calling an obsolete API, or if you know that the obsolete API will never be called on the affected OS version.
Suppress a warning
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1422
// The code that's violating the rule is on this line.
#pragma warning restore CA1422To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1422.severity = noneTo disable this entire category of rules, set the severity for the category to none in the configuration file.
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Interoperability.severity = noneFor more information, see How to suppress code analysis warnings.