Skip to content

Validate fix conditions in RegisterCodeFixesAsync before registering code fixes#1077

Merged
meziantou merged 8 commits intomainfrom
copilot/update-codefix-validation
Mar 31, 2026
Merged

Validate fix conditions in RegisterCodeFixesAsync before registering code fixes#1077
meziantou merged 8 commits intomainfrom
copilot/update-codefix-validation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

  • Analyze all 88 CodeFixProvider files and identify which need changes
  • Update ~34 CodeFixProvider files to validate conditions in RegisterCodeFixesAsync before calling context.RegisterCodeFix
  • Avoid duplicate GetBestTypeByMetadataName calls by capturing symbols in RegisterCodeFixesAsync and passing them via closure to fix methods
  • Reuse IBinaryOperation in DoNotUseEqualityOperatorsForSpanOfCharFixer — captured in RegisterCodeFixesAsync and passed to Refactor, eliminating the redundant GetOperation call
  • Update .github/copilot-instructions.md to document the best practice with code examples
  • Build: 0 errors, 0 warnings
  • Tests: all passed

Copilot AI and others added 5 commits March 31, 2026 01:32
…mplementations

Validate all conditions that could prevent a fix from being applied
before calling context.RegisterCodeFix, avoiding registering code fixes
that would return the document unchanged.

- AbstractTypesShouldNotHaveConstructorsFixer: validate node is
  ConstructorDeclarationSyntax before registering; update method signature
- DoNotRemoveOriginalExceptionFromThrowStatementFixer: validate node is
  ThrowStatementSyntax before registering; update method signature
- DoNotUseStringGetHashCodeFixer: validate StringComparer type exists
  before registering; remove dead null-checks in fix method
- UseStringComparisonFixer: validate InvocationExpressionSyntax and
  StringComparison type before registering; remove dead null-checks
- DoNotUseEqualityComparerDefaultOfStringFixer: validate StringComparer
  type before registering; remove dead null-check in fix method
- DoNotUseEqualityOperatorsForSpanOfCharFixer: validate IBinaryOperation
  before registering; remove dead null-check in fix method
- UseStructLayoutAttributeFixer: validate StructLayoutAttribute and
  LayoutKind types before registering; remove dead null-checks
- MarkAttributesWithAttributeUsageAttributeFixer: validate
  AttributeUsageAttribute and AttributeTargets before registering
- NonFlagsEnumsShouldNotBeMarkedWithFlagsAttributeFixer: validate
  FlagsAttribute type before registering; remove dead null-check
- UseDateTimeUnixEpochFixer: validate DateTime/DateTimeOffset types
  before registering branch-specific code fixes

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
… fixes

Move early-return conditions from fix methods into RegisterCodeFixesAsync
so that code fixes are never registered when they would return the document
unchanged. Affected fixers:

- AvoidComparisonWithBoolConstantFixer: check BinaryExpressionSyntax type
- UseIsPatternInsteadOfSequenceEqualFixer: check IInvocationOperation
- UsePatternMatchingForEqualityComparisonsFixer: check IBinaryOperation
- UsePatternMatchingInsteadOfHasvalueFixer: check IPropertyReferenceOperation
- NotPatternShouldBeParenthesizedCodeFixer: guard 'Negate all or patterns' fix
- UseStringComparerFixer: check System.StringComparer availability
- UseArrayEmptyFixer: check array type symbol
- UseStringCreateInsteadOfFormattableStringFixer: check CultureInfo and IInvocationOperation
- ReplaceEnumToStringWithNameofFixer: check operation is not null

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
…code fixes

Move all validation that could prevent a fix from being applied into
RegisterCodeFixesAsync so that code fixes are not registered when they
would return the document unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
…eFixProviders

Validate all conditions that could prevent a fix from being applied
before calling context.RegisterCodeFix, so no fix is registered that
would return the document unchanged.

- DoNotUseBlockingCallInAsyncContextFixer: validate Task type exists and
  MemberAccess.Expression in RegisterCodeFixesAsync; remove dead-code
  null check in ReplaceTaskResultWithAwait (Expression is non-nullable)
- DoNotUseImplicitCultureSensitiveToStringInterpolationFixer: add
  CultureInfo type check to RegisterCodeFixesAsync; remove redundant
  checks from Fix
- AvoidUnusedInternalTypesFixer: only register DynamicallyAccessedMembers
  fix when attribute types exist; remove null check from fix method
- UseLazyInitializerEnsureInitializeFixer: add semantic model, operation,
  and LazyInitializer type checks to RegisterCodeFixesAsync
- ReturnTaskFromResultInsteadOfReturningNullFixer: add Task type check to
  RegisterCodeFixesAsync; remove null checks from fix methods
- ValidateArgumentsCorrectlyFixer: add MethodDeclarationSyntax, body, and
  IMethodSymbol checks to RegisterCodeFixesAsync
- UseStringEqualsFixer: fetch semantic model first; validate IBinaryOperation
  and StringComparison type before registering fixes; remove null checks
  from fix methods

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Update codefixer to validate supported fixes before suggesting Validate fix conditions in RegisterCodeFixesAsync before registering code fixes Mar 31, 2026
Copilot AI requested a review from meziantou March 31, 2026 02:03
… instead of recomputing

Agent-Logs-Url: https://github.com/meziantou/Meziantou.Analyzer/sessions/caad0036-1a7d-490b-ad6a-c874f6a978db

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
This was referenced Apr 13, 2026
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.

2 participants