Repository navigation
Fix MSTEST0037 incorrectly rewriting non-generic IDictionary.Contains - #9968
Conversation
Non-generic System.Collections.IDictionary.Contains(object) checks for a matching key, whereas Assert.Contains enumerates the dictionary (yielding DictionaryEntry items). The two are not equivalent, so the code fix silently changed test behavior. The analyzer now skips the Contains-to-Assert.Contains suggestion when the invoked Contains is IDictionary.Contains (or an implementation of it, e.g. Hashtable). Fixes #9966
There was a problem hiding this comment.
Pull request overview
Prevents MSTEST0037 from changing non-generic dictionary key checks into collection-entry checks.
Changes:
- Detects
IDictionary.Containsimplementations and suppresses unsafe diagnostics. - Threads the
IDictionarysymbol through analyzer logic. - Adds regression coverage for
IDictionaryandHashtable.
Show a summary per file
| File | Description |
|---|---|
UseProperAssertMethodsAnalyzerTests.cs |
Adds regression tests. |
UseProperAssertMethodsAnalyzer.IsTrueIsFalse.cs |
Passes dictionary symbol to recognition logic. |
UseProperAssertMethodsAnalyzer.cs |
Resolves and propagates IDictionary. |
UseProperAssertMethodsAnalyzer.Collection.cs |
Excludes non-generic dictionary key checks. |
WellKnownTypeNames.cs |
Adds the IDictionary metadata name. |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 1
- Review effort level: Medium
This comment has been minimized.
This comment has been minimized.
…ionary check Co-authored-by: Copilot App <[email protected]> Copilot-Session: 2fa4d247-7176-4fac-8687-c53ae849933a
🧪 Test quality grade — PR #9968
This advisory comment was generated automatically. Grades are heuristic and informational — they do not block merging. Re-run with /grade-tests. Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "awmgmcpg"See Network Configuration for more information.
|
|
/backport to rel/4.3 |
|
Started backporting to rel/4.3: https://github.com/microsoft/testfx/actions/runs/29426857663 |
… by @Evangelink in #9968 (backport to rel/4.3) (#9979) Co-authored-by: Amaury Levé <[email protected]>
Fixes #9966.
Problem
MSTEST0037 (
UseProperAssertMethods) fired onAssert.IsTrue(dict.Contains("a"))wheredictis typed as the non-genericSystem.Collections.IDictionary, and the code fix rewrote it toAssert.Contains("a", dict)— which changes behavior and makes the test fail.IDictionary.Contains(object key)checks for a matching key, whereasAssert.Contains(item, collection)enumerates the collection. Enumerating a dictionary yieldsDictionaryEntry/KeyValuePairitems, not keys, so the two are not equivalent.Root cause
The collection-
Containsrecognizer reported a diagnostic for any non-genericIEnumerablecollection (where the genericIEnumerable<T>element type resolved tonull). Non-generic dictionaries fell into that branch.Fix
Before reporting in the non-generic branch, the analyzer now detects whether the invoked
ContainsisSystem.Collections.IDictionary.Contains— either called directly through the interface, or as the implementation on a concrete type such asHashtable— and skips the suggestion. GenericDictionary<K,V>/ICollection<KeyValuePair<K,V>>.Containsis unaffected because its element type matches the parameter type.Changes
WellKnownTypeNames.cs— addSystem.Collections.IDictionarywell-known name.UseProperAssertMethodsAnalyzer.cs— resolve and thread theIDictionarysymbol through.UseProperAssertMethodsAnalyzer.IsTrueIsFalse.cs— pass the symbol to the recognizer.UseProperAssertMethodsAnalyzer.Collection.cs— newIsNonGenericDictionaryContainshelper + guard.WhenAssertIsTrueOrIsFalseWithNonGenericIDictionaryContains_NoDiagnosticcovering bothIDictionaryandHashtable.Verification
Analyzer builds clean. All 132
UseProperAssertMethodsAnalyzerTestspass on net8.0; the subset passes on net472.