Add analyzer to warn when using required
or init
on Blazor component parameters
#63822
+132
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds a new analyzer (BL0010) that warns developers when using
required
orinit
modifiers on Blazor component parameter properties, addressing issues reported in #47824 and #44974.Problem
Customers have been attempting to use the
required
modifier on Blazor component parameters to ensure parameters have values, but this doesn't work as expected because:ParameterView.SetParameterProperties()
, which bypassesrequired
restrictionsrequired
modifier makes nullability warnings disappear but provides no actual guaranteesrequired
/init
could cause compilation errorsSolution
The new analyzer detects both
required
andinit
modifiers on[Parameter]
properties and suggests using the[EditorRequired]
attribute instead:Implementation Details
required
/init
modifiers on[Parameter]
properties[EditorRequired]
attribute as the proper alternativeThe analyzer integrates seamlessly with the existing ASP.NET Core analyzer infrastructure and follows established patterns for diagnostic reporting and localization.
Fixes #48051
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.