Edit

Share via


CA1422: Validate platform compatibility - obsoleted APIs

Property Value
Rule ID CA1422
Title Validate platform compatibility - obsoleted APIs
Category Interoperability
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 9 As warning

Cause

An API that's marked with 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:

Example

The following code snippet shows a violation of CA1422:

[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()
{ }
<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

The following code snippet fixes the violation by adding to the call site an UnsupportedOSPlatformAttribute attribute that specifies the version the called method was obsoleted in.

[SupportedOSPlatform("Windows")]
[ObsoletedOSPlatform("Windows6.2")]
public void M1()
{
    ObsoletedOnWindows62();
}

[ObsoletedOSPlatform("Windows6.2")]
public void ObsoletedOnWindows62()
{ }
<SupportedOSPlatform("Windows")>
<ObsoletedOSPlatform("Windows6.2")>
Public Sub M1()
    ObsoletedOnWindows62()
End Sub

<ObsoletedOSPlatform("Windows6.2")>
Public Sub ObsoletedOnWindows62()
End Sub

When 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 CA1422

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1422.severity = none

To 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 = none

For more information, see How to suppress code analysis warnings.

See also