Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 1, 2025

The CT0002 enum assignment analyzer previously only flagged direct literal assignments to enum properties but missed cast expressions with numeric literals. This enhancement extends the analyzer to also detect problematic patterns like:

// Previously flagged ✅
statuscode = 3;

// Now also flagged ✅
statuscode = (demo_Entity_statuscode)3;

// Still allowed ✅
statuscode = demo_Entity_statuscode.ValidStatus;
statuscode = (demo_Entity_statuscode)variable;

Changes Made

Enhanced Detection Logic: Modified both AnalyzeEnumAssignment and AnalyzeEnumAssignmentForProperty methods to detect:

  • Direct numeric literal assignments (existing behavior)
  • Cast expressions where the operand is a numeric literal (new behavior)

Comprehensive Test Coverage: Added new test cases covering:

  • Basic cast expressions (EnumPropertyAssignedCastShouldTrigger)
  • Dataverse naming patterns (EnumCastWithDataverseNamingPatternShouldTrigger)
  • Property initializers with casts (PropertyInitializerWithCastShouldTrigger)
  • Member access with casts (MemberAccessEnumAssignedCastShouldTrigger)
  • Variable-based casts that should remain allowed (EnumCastFromVariableShouldNotTrigger)

The analyzer now properly encourages developers to use proper enum values instead of casting numeric literals, improving code maintainability and reducing magic numbers in Dataverse-related code.

Fixes #11.


💡 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.

Co-authored-by: magesoe <8904582+magesoe@users.noreply.github.com>
Copilot AI changed the title [WIP] CT002 ammendment Extend CT0002 to detect enum cast expressions with numeric literals Sep 1, 2025
Copilot AI requested a review from magesoe September 1, 2025 09:10
@magesoe magesoe marked this pull request as ready for review September 1, 2025 09:33
@magesoe magesoe merged commit 3e2dec8 into master Sep 1, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CT002 ammendment

2 participants