Fix MA0065/MA0066 never checking for a GetHashCode override - #1391
Merged
Merged
Conversation
A copy-paste bug resolved ValueTypeGetHashCodeSymbol from ValueTypeSymbol.GetMembers(nameof(ValueType.Equals)) instead of nameof(ValueType.GetHashCode). Both fields held the same symbol, so HasDefaultEqualsOrHashCodeImplementations tested for an Equals override twice and never looked at GetHashCode. As a result, a struct that overrides Equals but not GetHashCode still uses the reflection-based ValueType.GetHashCode -- the exact performance problem these rules exist to catch -- yet was reported by neither MA0065 nor MA0066.
This was referenced Sep 6, 2026
Closed
Closed
This was referenced Sep 24, 2026
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.290
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#564
Open
Open
Open
Open
Open
Open
Open
Open
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.
What
DoNotUseDefaultEqualsOnValueTypeAnalyzerresolvedValueTypeGetHashCodeSymbolwithnameof(ValueType.Equals)instead ofnameof(ValueType.GetHashCode):Both fields held the same symbol, so the two checks in
HasDefaultEqualsOrHashCodeImplementationsboth tested for anEqualsoverride andGetHashCodewas never looked at.Why it matters
A struct that overrides
Equalsbut notGetHashCodestill uses the reflection-basedValueType.GetHashCode— precisely the performance problem MA0065 and MA0066 exist to catch — yet was reported by neither rule:Every such struct escaped both rules. The
Equalshalf of the check was never broken, so this is purely a set of missed diagnostics — no false positives were being produced.Tests
Added to
DoNotUseDefaultEqualsOnValueTypeAnalyzerTests:GetHashCode_OnlyEqualsOverriden— MA0065 on aGetHashCode()callConstructor_OnlyEqualsOverriden— MA0066 onHashSet<T>/Dictionary<TKey, TValue>Equals_OnlyGetHashCodeOverriden— guards the symmetric case (passed before the fix too)Verification
dotnet run --project src/DocumentationGeneratorexits 0 with no markdown changes. The MA0065/MA0066 docs already describe checking bothEqualsandGetHashCode, so the fix brings the implementation in line with the documented behavior and no doc update was needed.Reviewer note
This makes the rules report in cases they previously stayed silent on, so consumers with structs that override only
Equalsmay see new MA0065/MA0066 warnings after upgrading. That is the intended behavior of both rules.