Skip to content

Add CodeFixProvider for SetPropertiesInAnyOrderAnalyzer (PH2134)#951

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-950
Draft

Add CodeFixProvider for SetPropertiesInAnyOrderAnalyzer (PH2134)#951
Copilot wants to merge 3 commits intomainfrom
copilot/fix-950

Conversation

Copy link
Contributor

Copilot AI commented Aug 25, 2025

This PR implements a CodeFixProvider for the SetPropertiesInAnyOrderAnalyzer (PH2134) to automatically resolve property setter order dependencies.

Problem

The SetPropertiesInAnyOrderAnalyzer detects when property setters reference other properties in the same class, which creates order dependencies that users must understand to set properties correctly. Previously, developers had to manually fix these issues without automated assistance.

Solution

Added SetPropertiesInAnyOrderCodeFixProvider that automatically converts problematic properties to simple auto-properties, eliminating the order dependency issue.

Example transformation:

// Before (triggers PH2134 diagnostic)
public class BadExample {
  public int One { get; set; }
  public int Two {
    set {
      One = value - 1;  // References other property - creates order dependency
    }
  }
}

// After (CodeFixer applied)  
public class GoodExample {
  public int One { get; set; }
  public int Two { get; set; }  // Simple auto-property - no dependencies
}

Implementation Details

  • CodeFixProvider: Extends SingleDiagnosticCodeFixProvider<AccessorDeclarationSyntax> following the established pattern
  • Fix Logic: Replaces the entire property declaration with a simple auto-property { get; set; }
  • Trivia Preservation: Maintains original formatting and comments
  • Comprehensive Testing: Added test coverage for various scenarios including properties with both custom getters and setters

Changes Made

  1. New Files:

    • SetPropertiesInAnyOrderCodeFixProvider.cs - The CodeFix implementation
    • SetPropertiesInAnyOrderCodeFixProviderTest.cs - Comprehensive test suite
  2. Updated Files:

    • PH2134.md - Updated documentation to reflect CodeFix availability

Testing

  • All 12 related tests pass (8 existing analyzer tests + 4 new CodeFixer tests)
  • Tests cover simple setters, properties with custom getters, and properties with only custom setters
  • Formatting validation passes completely
  • No regressions detected in broader test suite

The CodeFixer provides immediate, automated resolution for PH2134 violations, improving developer productivity and code quality.

Fixes #950.


✨ 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 and others added 2 commits August 25, 2025 21:45
…ests

Co-authored-by: bcollamore <57269455+bcollamore@users.noreply.github.com>
Co-authored-by: bcollamore <57269455+bcollamore@users.noreply.github.com>
Copilot AI changed the title [WIP] Create CodeFixer for SetPropertiesInAnyOrderAnalyzer Add CodeFixProvider for SetPropertiesInAnyOrderAnalyzer (PH2134) Aug 25, 2025
@sonarqubecloud
Copy link

@bcollamore
Copy link
Member

Copilot I didn't provide you enough context. There are already MS tools in the IDE to convert to auto-properties; we don't want to replicate that here. This rule is simply: if the user made a get and a set (for whatever reason), make sure they did the get first, for consistency. So the auto-fixer would simply swap the order of the 2 when there is a finding.

Copy link
Contributor Author

Copilot AI commented Aug 26, 2025

Copilot I didn't provide you enough context. There are already MS tools in the IDE to convert to auto-properties; we don't want to replicate that here. This rule is simply: if the user made a get and...

I need clarification on what should be swapped. The analyzer currently detects when property setters reference other properties (creating order dependencies). When you mention "swap the order of the 2", do you mean:

  1. Swap the order of accessors within the property (e.g., put get before set)?
  2. Swap the order of property declarations in the class?
  3. Something else?

Could you provide an example of the problematic code and the expected fix after swapping? This will help me implement the correct behavior.

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.

Create CodeFixer for SetPropertiesInAnyOrderAnalyzer

2 participants