Skip to content

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

Open
dkamburov wants to merge 19 commits into
dotnet:mainfrom
dkamburov:disposable-warning
Open

Warn on Blazor components with Dispose()/DisposeAsync() but no interface#67426
dkamburov wants to merge 19 commits into
dotnet:mainfrom
dkamburov:disposable-warning

Conversation

@dkamburov

@dkamburov dkamburov commented Jun 26, 2026

Copy link
Copy Markdown

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 BL0015 and BL0016 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.

Comment thread src/Components/Analyzers/src/ComponentDisposableSanityAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/ComponentDisposableSanityAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/ComponentDisposableSanityAnalyzer.cs
@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
Comment thread src/Components/Analyzers/src/ComponentDisposableSanityAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/ComponentDisposableSanityAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/ComponentDisposableSanityAnalyzer.cs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@dkamburov

Copy link
Copy Markdown
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 1 comment.

Comment thread src/Components/Analyzers/src/DiagnosticDescriptors.cs Outdated

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.

Comment thread src/Components/Analyzers/src/ComponentDisposableSanityAnalyzer.cs
Comment thread src/Components/Analyzers/src/ComponentDisposableSanityAnalyzer.cs Outdated
dkamburov and others added 2 commits July 8, 2026 14:54
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

3 participants