Skip to content

Warn on Blazor components with Dispose()/DisposeAsync() but no interface - #67426

Merged
kotlarmilos merged 34 commits into
dotnet:mainfrom
dkamburov:disposable-warning
Sep 3, 2026
Merged

kotlarmilos merged 34 commits into
dotnet:mainfrom
dkamburov:disposable-warning

Conversation

@dkamburov

@dkamburov dkamburov commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Add analyzer for Dispose()/DisposeAsync() interface sanity checks

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Adds two new analyzer diagnostics that catch a silent class of bugs where a Blazor component declares a dispose method but the runtime never calls it because the corresponding interface isn't implemented.

Description

Introduce ComponentDisposableSanityAnalyzer to warn when Blazor components declare Dispose() or DisposeAsync() without implementing IDisposable or IAsyncDisposable. Add diagnostics with resource strings, and provide thorough analyzer tests for all relevant scenarios.

Fixes #51714

Copilot AI review requested due to automatic review settings June 26, 2026 07:11
@dkamburov
dkamburov requested a review from a team as a code owner June 26, 2026 07:11
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @dkamburov. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dkamburov dkamburov changed the title Disposable warning Add analyzer for Dispose/DisposeAsync interface sanity checks Jun 26, 2026

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.

Pull request overview

This PR introduces a new Components analyzer that warns when a Blazor component declares Dispose() / DisposeAsync() but does not implement the corresponding disposal interface, helping prevent cleanup logic from silently never running.

Changes:

  • Add ComponentDisposableSanityAnalyzer to report new warnings BL0014 (Dispose() without IDisposable) and BL0015 (DisposeAsync() without IAsyncDisposable).
  • Add localized descriptor resources and wire them into DiagnosticDescriptors.
  • Add unit tests covering the core warning/no-warning scenarios.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/Components/Analyzers/src/ComponentDisposableSanityAnalyzer.cs New analyzer that scans component types for Dispose/DisposeAsync methods and reports BL0014/BL0015.
src/Components/Analyzers/src/DiagnosticDescriptors.cs Adds the two new DiagnosticDescriptors for BL0014/BL0015.
src/Components/Analyzers/src/Resources.resx Adds titles/formats/descriptions for the two new diagnostics.
src/Components/Analyzers/test/ComponentDisposableSanityAnalyzerTest.cs Adds tests validating BL0014/BL0015 reporting behavior.

@dkamburov dkamburov changed the title Add analyzer for Dispose/DisposeAsync interface sanity checks Warn on Blazor components with Dispose()/DisposeAsync() but no interface Jun 26, 2026
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@dkamburov

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree company="Infragistics, Inc"

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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

@Youssef1313 Youssef1313 added the area-blazor Includes: Blazor, Razor Components label Sep 2, 2026

@kotlarmilos kotlarmilos 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.

LGTM. The analyzer matches Blazor’s interface-based disposal behavior and covers the relevant method and interface combinations without introducing false positives.

@kotlarmilos
kotlarmilos merged commit 69ec3ca into dotnet:main Sep 3, 2026
38 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] IDisposable Sanity check on Blazor Component

4 participants