Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 13, 2025

This PR implements the new CT0002 analyzer rule that warns when enum properties are assigned literal numeric values instead of proper enum values.

Problem

Assigning literal values to enum properties reduces code readability and maintainability. For example:

public Account_AccountCategoryCode? AccountCategoryCode { get; set; }

// Poor practice - using literal values
AccountCategoryCode = 2;  // What does 2 mean?

// Better practice - using enum values  
AccountCategoryCode = Account_AccountCategoryCode.Preferred;  // Clear intent

Solution

The new CT0002 rule detects and warns about literal assignments to enum properties while allowing valid scenarios:

✅ Allowed (no warnings):

// Enum value assignment
AccountCategoryCode = Account_AccountCategoryCode.Standard;

// Explicit cast (intentional conversion)
AccountCategoryCode = (Account_AccountCategoryCode)2;

// Variable assignment
var value = GetCategoryCode();
AccountCategoryCode = value;

❌ Warns with CT0002:

// Property initializer with literal
public Priority Priority { get; set; } = 1;

// Assignment with literal
AccountCategoryCode = 2;

Implementation Details

  • Analyzer: EnumAssignmentAnalyzer.cs detects literal assignments to enum properties
  • Coverage: Handles nullable enums, property initializers, field assignments, and member access
  • Smart detection: Only flags numeric literals, ignores casts, variables, and non-enum types
  • Comprehensive tests: 11 test scenarios covering edge cases and valid scenarios

The implementation follows the existing CT0001 pattern and adheres to the project's coding standards and analyzer conventions.

Fixes #3.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add a new rule CT0002 Add CT0002 rule to warn when enum properties are assigned literal values Aug 13, 2025
Copilot AI requested a review from magesoe August 13, 2025 09:25
@magesoe magesoe marked this pull request as ready for review August 13, 2025 13:04
@magesoe magesoe merged commit 1d7bfae into master Aug 13, 2025
2 checks 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.

Add a new rule CT0002

2 participants