Skip to content

fix: replace Dictionary with List for wildcard-compatible rule storage#75

Closed
mfogliatto-dev-agent wants to merge 1 commit into
mfogliatto:mainfrom
mfogliatto-dev-agent:fix/issue-70
Closed

fix: replace Dictionary with List for wildcard-compatible rule storage#75
mfogliatto-dev-agent wants to merge 1 commit into
mfogliatto:mainfrom
mfogliatto-dev-agent:fix/issue-70

Conversation

@mfogliatto-dev-agent

Copy link
Copy Markdown

Summary

Replaces the Dictionary<string, Rule> field rules in AssemblyNameViolationDetector with List<KeyValuePair<string, Rule>> to fix a broken IEqualityComparer contract.

Problem

PatternMatchComparer.GetHashCode uses the default string.GetHashCode(), which violates the IEqualityComparer<string> contract: when Equals("*", "SomeAssembly") returns true, their hash codes differ. This means:

  • Any Dictionary using this comparer cannot perform correct hash-based lookups for wildcard keys
  • Duplicate pattern detection via Dictionary.Add was also broken (two patterns the comparer considers equal but with different hashes would not be detected as duplicates)
  • The bug was masked because GetViolationsFrom iterates the dictionary via foreach, bypassing hash lookups entirely

Fix

Since rules is only iterated (never looked up by key), replace it with a List<KeyValuePair<>> — this correctly reflects the actual usage pattern and eliminates the broken hash dependency.

Duplicate detection now uses the comparer's Equals method directly via List.Any(), which is correct for all comparer types including wildcard-aware ones.

The exactMatchRules dictionary (using StringComparer.InvariantCulture) and patternRules list in the experimental path are unaffected.

Testing

All 18 AssemblyNameViolationDetectorTests pass. The 8 pre-existing failures in ProjectPathProviderTests are unrelated (Windows path tests on Linux).

Fixes #70

The rules dictionary used PatternMatchComparer as its equality comparer,
but PatternMatchComparer.GetHashCode used the default string.GetHashCode,
violating the IEqualityComparer contract (equal objects must have equal
hash codes). This made Dictionary lookups silently broken for wildcard
patterns.

Since the rules collection is only iterated (never looked up by key),
replace it with List<KeyValuePair<>> which correctly reflects its usage.
Duplicate detection now uses the comparer's Equals method directly
instead of relying on Dictionary.Add's broken hash-based check.

The exactMatchRules dictionary (using StringComparer.InvariantCulture)
and patternRules list in the experimental path are unaffected — they
already use correct comparers for their respective purposes.

Fixes #70
@mfogliatto-dev-agent mfogliatto-dev-agent closed this by deleting the head repository Jun 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.

[Performance] PatternMatchComparer.GetHashCode breaks Dictionary invariant — O(1) lookups degrade to O(n)

1 participant