All rules
CA1422Portability and interoperability Enabled by default: As warning

Validate platform compatibility - obsoleted APIs

Validate platform compatibility - obsoleted APIs

Microsoft docs

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.

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.

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

#pragma warning disable CA1422
// The code that's violating the rule is on this line.
#pragma warning restore CA1422

When to suppress

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.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0