Skip to content

Fix MSTEST0037 incorrectly rewriting non-generic IDictionary.Contains - #9968

Merged
Amaury Levé (Evangelink) merged 2 commits into
mainfrom
dev/amauryleve/cuddly-pancake
Jul 15, 2026
Merged

Amaury Levé (Evangelink) merged 2 commits into
mainfrom
dev/amauryleve/cuddly-pancake

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Fixes #9966.

Problem

MSTEST0037 (UseProperAssertMethods) fired on Assert.IsTrue(dict.Contains("a")) where dict is typed as the non-generic System.Collections.IDictionary, and the code fix rewrote it to Assert.Contains("a", dict) — which changes behavior and makes the test fail.

IDictionary.Contains(object key) checks for a matching key, whereas Assert.Contains(item, collection) enumerates the collection. Enumerating a dictionary yields DictionaryEntry/KeyValuePair items, not keys, so the two are not equivalent.

Root cause

The collection-Contains recognizer reported a diagnostic for any non-generic IEnumerable collection (where the generic IEnumerable<T> element type resolved to null). Non-generic dictionaries fell into that branch.

Fix

Before reporting in the non-generic branch, the analyzer now detects whether the invoked Contains is System.Collections.IDictionary.Contains — either called directly through the interface, or as the implementation on a concrete type such as Hashtable — and skips the suggestion. Generic Dictionary<K,V> / ICollection<KeyValuePair<K,V>>.Contains is unaffected because its element type matches the parameter type.

Changes

  • WellKnownTypeNames.cs — add System.Collections.IDictionary well-known name.
  • UseProperAssertMethodsAnalyzer.cs — resolve and thread the IDictionary symbol through.
  • UseProperAssertMethodsAnalyzer.IsTrueIsFalse.cs — pass the symbol to the recognizer.
  • UseProperAssertMethodsAnalyzer.Collection.cs — new IsNonGenericDictionaryContains helper + guard.
  • Added test WhenAssertIsTrueOrIsFalseWithNonGenericIDictionaryContains_NoDiagnostic covering both IDictionary and Hashtable.

Verification

Analyzer builds clean. All 132 UseProperAssertMethodsAnalyzerTests pass on net8.0; the subset passes on net472.

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
Copilot AI review requested due to automatic review settings July 15, 2026 09:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Prevents MSTEST0037 from changing non-generic dictionary key checks into collection-entry checks.

Changes:

  • Detects IDictionary.Contains implementations and suppresses unsafe diagnostics.
  • Threads the IDictionary symbol through analyzer logic.
  • Adds regression coverage for IDictionary and Hashtable.
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

@github-actions

This comment has been minimized.

@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) July 15, 2026 10:57
@Evangelink Amaury Levé (Evangelink) added the state/needs-review Awaiting review from the team. label Jul 15, 2026
…ionary check

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 2fa4d247-7176-4fac-8687-c53ae849933a
Copilot AI review requested due to automatic review settings July 15, 2026 11:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 0 new
  • Review effort level: Medium

@github-actions

Copy link
Copy Markdown
Contributor

🧪 Test quality grade — PR #9968

GradeTestNotes
A (90–100) new UseProperAssertMethodsAnalyzerTests.
WhenAssertIsTrueOrIsFalseWith
NonGenericIDictionaryContains_
NoDiagnostic
Clear single-scenario test; VerifyCodeFixAsync(code, code) meaningfully asserts no diagnostic is emitted. Inline comments explain the semantic distinction. No issues found.

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 domain

The following domain was blocked by the firewall during workflow execution:

  • awmgmcpg

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "awmgmcpg"

See Network Configuration for more information.

🤖 Automated content by GitHub Copilot. Generated by the Grade Tests on PR (on open / sync) workflow. · 41.2 AIC · ⌖ 6.04 AIC · ⊞ 8.9K · [◷]( · ◷)

@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) July 15, 2026 14:26
@Evangelink
Amaury Levé (Evangelink) merged commit 819c828 into main Jul 15, 2026
67 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the dev/amauryleve/cuddly-pancake branch July 15, 2026 14:27
@Evangelink

Copy link
Copy Markdown
Member Author

/backport to rel/4.3

@github-actions

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state/needs-review Awaiting review from the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSTEST0037 incorrectly fixes non-generic Dictionary.Contains

3 participants