Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 19, 2025

This PR implements the CT0003 analyzer rule that prevents the use of empty parentheses in object initialization when using object initializers, promoting cleaner and more consistent code style.

Rule Behavior

The new CT0003 rule flags object creation expressions that use empty parentheses followed by object initializers as a warning, encouraging developers to omit unnecessary parentheses.

❌ Flagged (triggers CT0003):

var account = new Account()
{
    Name = "MoneyMan",
};

✅ Allowed:

// No parentheses - preferred style
var account = new Account
{
    Name = "MoneyMan",
};

// With parameters - parentheses are necessary
var account = new Account(accountId)
{
    Name = "MoneyMan",
};

Implementation Details

  • Analyzer: ObjectInitializationAnalyzer detects ObjectCreationExpressionSyntax nodes with empty argument lists and object initializers
  • Rule ID: CT0003 with Warning severity
  • Category: Style
  • Coverage: Works with all types including built-in collections, custom classes, and generic types

Testing

Added comprehensive test coverage with 7 test cases covering:

  • Object creation with empty parentheses and initializers (should trigger)
  • Object creation without parentheses and initializers (should not trigger)
  • Object creation with parameters and initializers (should not trigger)
  • Object creation with empty parentheses but no initializers (should not trigger)
  • Built-in types like List<T> (should trigger when applicable)
  • Anonymous type creation (should not trigger)
  • Multiple violations in the same method

All existing tests continue to pass (26/26), ensuring no regressions in CT0001 and CT0002 rules.

Documentation

Updated README.md to include documentation for all three rules (CT0001, CT0002, CT0003) with clear examples of allowed and disallowed patterns.

Fixes #5.


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

Co-authored-by: magesoe <8904582+magesoe@users.noreply.github.com>
Copilot AI changed the title [WIP] CT0003 Opinionated object initialization Implement CT0003 rule for opinionated object initialization Aug 19, 2025
Copilot AI requested a review from magesoe August 19, 2025 06:35
@magesoe magesoe marked this pull request as ready for review August 19, 2025 08:06
@magesoe magesoe merged commit ced4424 into master Aug 19, 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.

CT0003 Opinionated object initialization

2 participants