Skip to content

Fix spurious CA1416 warnings in guard members and in nameof - #56042

Merged
jozkee merged 3 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/fix-ca1416-platform-guard-attribute
Sep 16, 2026
Merged

jozkee merged 3 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/fix-ca1416-platform-guard-attribute

Conversation

@Sergio0694

@Sergio0694 Sergio0694 commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #54066

Two things made CA1416 fire when it shouldn't.

Guard members lost their call site platform context. The analyzer computes the platform attributes of a symbol by walking up the containing chain and merging them, and it clears everything it found when the symbol carries a guard attribute. That is intentional, because referencing a guard member should never warn. The problem is that the same result is also used as the call site context for the symbol whose body is being analyzed, so as soon as a member was annotated with [SupportedOSPlatformGuard] it also lost the context coming from the assembly level [SupportedOSPlatform] that the SDK generates from SupportedOSPlatformVersion. Everything inside that member was then reported as reachable on all platforms.

The call site lookup now falls back to the assembly when the containing member is a guard. It deliberately stops there rather than using the containing type: a guard sheds its type's requirements so that callers on any platform can reference it, so its body cannot rely on them either. Assembly wide requirements are different, since every call site inside the assembly is bound by them regardless.

nameof was treated as a real API reference. A nameof operand is a compile time constant and never touches the member, and it is lowered out of the control flow graph, so platform guards around it did not suppress the diagnostic either. Operations under a nameof are now skipped.

The second part is a behavior change: nameof(SomePlatformSpecificProperty) used to report CA1416 and no longer does. That was already inconsistent, since the same expression did not warn when the attribute sat on the accessor rather than on the property, and GetPropertyAccessors already documents that ValueUsageInfo.Name only uses the name of the property. One existing test line was updated for this.

Both cases were reproduced as failing tests first, and each new test was confirmed to fail without the corresponding change. The full Microsoft.CodeAnalysis.NetAnalyzers.UnitTests suite passes.

Copilot AI lite review requested due to automatic review settings August 31, 2026 12:02
@Sergio0694
Sergio0694 requested a review from a team as a code owner August 31, 2026 12:02
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@Sergio0694

Copy link
Copy Markdown
Contributor Author

@sbomer could you help review this? Also, could we backport it to 10.0.4xx or 10.0.5xx? Thank you! 🙂

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Pull request overview

This PR updates the PlatformCompatibility analyzer (CA1416/related) to avoid two categories of spurious diagnostics: (1) operations inside members annotated with [SupportedOSPlatformGuard]/[UnsupportedOSPlatformGuard] no longer lose the platform context coming from the containing type/assembly, and (2) references that occur only as the operand of nameof/NameOf are no longer treated as real API accesses.

Changes:

  • Skip platform-compatibility analysis for operations that occur under an INameOfOperation (so nameof(SomePlatformSpecificMember) won’t warn).
  • When establishing the call-site platform context for an operation, fall back to the containing symbol’s container if the containing member is a guard.
  • Add/adjust unit tests to cover the guard-member-context preservation and the nameof behavior change (including an obsoleted-platform case).
File summaries
File Description
src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs Implements the analyzer behavior changes for guard-member call-site context and skipping operations under nameof.
src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzerTests.GuardedCallsTests.cs Adds regression tests ensuring guard-member bodies retain container/assembly platform context and still warn when appropriate.
src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzerTests.cs Updates an existing nameof-related expectation and adds new C#/VB tests ensuring nameof does not warn while real access still does.
src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.ObsoletedOSPlatformTests.cs Adds a regression test ensuring nameof does not trigger obsoleted-platform diagnostics.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Lite

@Evangelink Evangelink left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

This review comment was generated with AI assistance.

Found one analyzer-correctness issue in the guard-member context change.

Sergio0694 and others added 2 commits September 8, 2026 07:32
Members annotated with [SupportedOSPlatformGuard] lost the platform context
inherited from their containing type or assembly, so every platform specific
API used inside them was reported as reachable on all platforms.

Operations under a nameof expression were also treated as real API references,
even though nameof only captures the name at compile time.

Fixes dotnet#54066

Co-authored-by: Copilot App <[email protected]>
A guard member sheds the platform requirements of its containing type so
that it can be referenced from any call site, so its body cannot rely on
them either. Falling back to the immediate container meant neither the
reference nor the body was diagnosed. Fall back to the assembly instead,
whose requirements bind every call site within it.

Co-authored-by: Copilot App <[email protected]>
@Sergio0694
Sergio0694 force-pushed the user/sergiopedri/fix-ca1416-platform-guard-attribute branch from 7e51853 to dc220e3 Compare September 8, 2026 14:33
Cover both guard attributes in the assembly-context and neutral-call-site tests, add the nested VB NameOf case, and document the paired guard attribute handling.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: cfef23d8-6258-4113-8295-d666bd027fe7
@Sergio0694

Copy link
Copy Markdown
Contributor Author

@tannergooding thank you for the review! I don't have permissions to merge, could you help? 😄

@jozkee
jozkee merged commit 5141662 into dotnet:main Sep 16, 2026
41 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 17, 2026
@tannergooding

Copy link
Copy Markdown
Member

/backport to release/11.0.1xx

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0.1xx (link to workflow run)

This branch had an error being deployed

1 failed deployment
copilot-pat-pool — 978ff76d Deployed Sep 16, 2026 by jozkee via pre_activation #5288
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong CA1416 warning within members annotated with [SupportedOSPlatformGuard]

7 participants