Validate fix conditions in RegisterCodeFixesAsync before registering code fixes#1077
Merged
Validate fix conditions in RegisterCodeFixesAsync before registering code fixes#1077
Conversation
…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>
Agent-Logs-Url: https://github.com/meziantou/Meziantou.Analyzer/sessions/fa4a7d1f-f6c9-43f5-895e-c12c916c9deb 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
meziantou
reviewed
Mar 31, 2026
meziantou
reviewed
Mar 31, 2026
…a closure Agent-Logs-Url: https://github.com/meziantou/Meziantou.Analyzer/sessions/3d2cedd1-2260-49aa-ac0b-0fba90bb42a3 Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
… 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 Mar 31, 2026
Closed
This was referenced Apr 13, 2026
Open
This was referenced Apr 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RegisterCodeFixesAsyncbefore callingcontext.RegisterCodeFixGetBestTypeByMetadataNamecalls by capturing symbols inRegisterCodeFixesAsyncand passing them via closure to fix methodsIBinaryOperationinDoNotUseEqualityOperatorsForSpanOfCharFixer— captured inRegisterCodeFixesAsyncand passed toRefactor, eliminating the redundantGetOperationcall.github/copilot-instructions.mdto document the best practice with code examples