Skip to content

Add BindablePropertyDefaultValueCreatorAnalyzer to to avoid Static References from being passed into CreateDefaultValueMethodName when using [BindableProperty] and [AttachedBindableProperty<T>]. - #3096

Open
TheCodeTraveler wants to merge 20 commits into
mainfrom
Remove-All-Static-References-From-CreateDefaultValue

Conversation

@TheCodeTraveler

@TheCodeTraveler TheCodeTraveler commented Feb 14, 2026 •

Copy link
Copy Markdown
Collaborator

Description of Change

This PR continues the effort started in #3095 to avoid Static References from being passed into CreateDefaultValueMethodName when using [BindableProperty] and [AttachedBindableProperty<T>].

This PR adds the BindablePropertyDefaultValueCreatorAnalyzer to detect these scenarios and generate a Warning when one is found. This Analyzer is public and will also be leveraged by any developer using CommunityToolkit.Maui. It also adds a comprehensive unit test suite to verify the different scenarios where a static reference may be passed into CreateDefaultValueMethodName.

To ensure we on the Community Toolkit team do not introduce improper usage I have promoted this Warning to an Error in Directory.Build.props: <WarningsAsErrors>MCT003</WarnindsAsErrors>.

Lastly, this PR fixes the one discovered violation of this new Analyzer, found in NavigationBar.CreateColorDefaultValue.

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard

Additional information

This PR also adds Benchmarks for the new Analyzer

Copilot AI review requested due to automatic review settings February 14, 2026 22:48

This comment was marked as outdated.

Copilot AI review requested due to automatic review settings February 14, 2026 22:56

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 8 out of 9 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • src/CommunityToolkit.Maui.Analyzers/Resources.Designer.cs: Language not supported

This comment was marked as outdated.

This comment was marked as outdated.

bijington
bijington previously approved these changes Feb 21, 2026

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

The changes look good. Just one question around whether we should be covering the const option

Copilot AI review requested due to automatic review settings February 21, 2026 19:16
@TheCodeTraveler

Copy link
Copy Markdown
Collaborator Author

The changes look good. Just one question around whether we should be covering the const option

Thanks Shaun! Nah, we're good. The bug occurs when a static reference is used that is writable. Since we can't write to a const, it won't produce the bug.

This comment was marked as 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 10 out of 11 changed files in this pull request and generated 4 comments.

Files not reviewed (1)
  • src/CommunityToolkit.Maui.Analyzers/Resources.Designer.cs: Language not supported
Comments suppressed due to low confidence (1)

src/CommunityToolkit.Maui.Analyzers/BindablePropertyDefaultValueCreatorAnalyzer.cs:332

  • The diagnostic message/resources say "static readonly field or property", but the analyzer logic flags any static field/property (regardless of readonly). Either update the analyzer to check readonly as well, or adjust the rule title/message/description (and related helper method names) to match the intended behavior (e.g., warn on any static shared instance).
	}
}

Comment on lines +143 to +149
static MethodDeclarationSyntax? FindMethodInType(TypeDeclarationSyntax typeDeclaration, string methodName)
{
return typeDeclaration
.DescendantNodes()
.OfType<MethodDeclarationSyntax>()
.FirstOrDefault(method => method.Identifier.ValueText == methodName);
}

Copilot AI Feb 23, 2026

Copy link

Choose a reason for hiding this comment

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

FindMethodInType uses DescendantNodes(), which can pick up methods inside nested types and miss methods declared in other partial declarations of the same type (in different files). Consider resolving the containing type symbol via the semantic model and using INamedTypeSymbol.GetMembers(methodName) (or restricting to direct members) to avoid false positives/negatives.

Copilot uses AI. Check for mistakes.
Comment on lines +171 to +176
static bool DoesReturnStaticReadOnlyFieldOrProperty(MethodDeclarationSyntax methodDeclaration, SemanticModel semanticModel, CancellationToken cancellationToken)
{
var returnStatements = methodDeclaration
.DescendantNodes()
.OfType<ReturnStatementSyntax>();

Copilot AI Feb 23, 2026

Copy link

Choose a reason for hiding this comment

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

DoesReturnStaticReadOnlyFieldOrProperty enumerates return statements via DescendantNodes(), which also walks into lambdas/local functions inside the method. A nested return inside a lambda can incorrectly trigger the diagnostic even if the default value creator itself returns a new instance. Consider limiting traversal to the method body excluding nested functions/lambdas.

Copilot uses AI. Check for mistakes.

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 10 out of 11 changed files in this pull request and generated 5 comments.

Files not reviewed (1)
  • src/CommunityToolkit.Maui.Analyzers/Resources.Designer.cs: Language not supported

Comment thread Directory.Build.props Resolved
Copilot AI review requested due to automatic review settings February 23, 2026 01:14
@TheCodeTraveler

Copy link
Copy Markdown
Collaborator Author

Thanks @pictos + @bijington! Could we get one more review?

I've updated the Analyzer to support both Delegate and const. Great recommendations 🙌

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 10 out of 11 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • src/CommunityToolkit.Maui.Analyzers/Resources.Designer.cs: Language not supported

return propertyDeclaration;
}

static bool DoesReturnStaticReadOnlyFieldOrProperty(MethodDeclarationSyntax methodDeclaration, SemanticModel semanticModel, CancellationToken cancellationToken)

Copilot AI Feb 23, 2026

Copy link

Choose a reason for hiding this comment

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

The method name "DoesReturnStaticReadOnlyFieldOrProperty" suggests it only checks for readonly static members, but the implementation at line 324 actually checks for ANY static field (non-const) or static property, regardless of whether it's readonly. Consider renaming to "DoesReturnStaticFieldOrProperty" to better reflect the actual behavior and avoid confusion.

Suggested change
static bool DoesReturnStaticReadOnlyFieldOrProperty(MethodDeclarationSyntax methodDeclaration, SemanticModel semanticModel, CancellationToken cancellationToken)
static bool DoesReturnStaticFieldOrProperty(MethodDeclarationSyntax methodDeclaration, SemanticModel semanticModel, CancellationToken cancellationToken)

Copilot uses AI. Check for mistakes.
return false;
}

static bool DoesDelegateInitializerReturnStaticReadOnlyMember(SyntaxNode memberDeclaration, SemanticModel semanticModel, CancellationToken cancellationToken)

Copilot AI Feb 23, 2026

Copy link

Choose a reason for hiding this comment

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

The method name "DoesDelegateInitializerReturnStaticReadOnlyMember" suggests it only checks for readonly static members, but the implementation delegates to "IsStaticFieldOrProperty" which checks for ANY static field (non-const) or static property. Consider renaming to "DoesDelegateInitializerReturnStaticMember" to better reflect the actual behavior.

Copilot uses AI. Check for mistakes.
@TheCodeTraveler TheCodeTraveler changed the title Remove All Static References from CreateDefaultValue in Bindable Property Attributes, Add BindablePropertyDefaultValueCreatorAnalyzer to avoid future implementations Add BindablePropertyDefaultValueCreatorAnalyzer to to avoid Static References from being passed into CreateDefaultValueMethodName when using [BindableProperty] and [AttachedBindableProperty<T>]. Feb 25, 2026
@dotnet-policy-service dotnet-policy-service Bot added stale The author has not responded in over 30 days help wanted This proposal has been approved and is ready to be implemented labels Mar 31, 2026
@TheCodeTraveler
TheCodeTraveler force-pushed the Remove-All-Static-References-From-CreateDefaultValue branch from ec859f9 to 877bbe2 Compare November 9, 2026 06:42

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted This proposal has been approved and is ready to be implemented stale The author has not responded in over 30 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants