Skip to content

Commit 044dc0f

Browse files
committed
xaml: XAML105 requires 2+ distinct scopes (no same-scope dup-key FP)
A ResourceDictionary that declares the same x:Key twice directly (or twice inside one inline merged dictionary) is a duplicate-key error within a single scope — runtime-invalid, but a different problem from XAML105's order-dependent merge shadowing. The old check fired on any key with 2+ occurrences, so two primary entries sharing a key were mislabeled as [primary, primary] shadowing. Collisions now require the key to span 2+ DISTINCT scopes (primary vs merged, or two different merged dicts). Added a selftest: two same-key primary entries plus a non-colliding merged dict must not emit XAML105. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015zoG4YNCUf7RA5r8GAXMmb
1 parent b351ea4 commit 044dc0f

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

‎audit/static/tools/xaml_check.py‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,11 @@ def _rule_merged_dict_shadowing(root: Node, avalonia: bool) -> list[XamlFinding]
430430
keymap.setdefault(key, []).append((f"merged #{i}", c.line))
431431

432432
for key, occ in keymap.items():
433-
if len(occ) < 2:
434-
continue # the normal case: a key defined once
433+
# Shadowing requires the key in 2+ DISTINCT scopes (primary vs merged, or
434+
# two different merged dicts). Two entries in the SAME scope are a
435+
# duplicate-key error (runtime-invalid) — a different problem, not XAML105.
436+
if len({w for w, _ln in occ}) < 2:
437+
continue
435438
where = ", ".join(f"{w} (line {ln})" for w, ln in occ)
436439
out.append(XamlFinding(
437440
"XAML105", occ[0][1],
@@ -1014,6 +1017,19 @@ def rules(text: str) -> dict[str, XamlFinding]:
10141017
# external Source dictionaries are not resolved -> not compared (deferred).
10151018
check("XAML105" not in rules(dup),
10161019
"XAML105 must not compare keys of external Source= dictionaries (cross-file deferred)")
1020+
# two primary entries with the SAME key are a duplicate-key error in ONE scope, not
1021+
# order-dependent merge shadowing -> XAML105 must not fire (needs 2+ distinct scopes).
1022+
samescope = (f'<ResourceDictionary {_WPF_NS}>\n'
1023+
' <SolidColorBrush x:Key="accent" Color="Green" />\n'
1024+
' <SolidColorBrush x:Key="accent" Color="Teal" />\n'
1025+
' <ResourceDictionary.MergedDictionaries>\n'
1026+
' <ResourceDictionary>\n'
1027+
' <SolidColorBrush x:Key="other" Color="Blue" />\n'
1028+
' </ResourceDictionary>\n'
1029+
' </ResourceDictionary.MergedDictionaries>\n'
1030+
'</ResourceDictionary>\n')
1031+
check("XAML105" not in rules(samescope),
1032+
"XAML105 false positive: same-scope duplicate keys are not merge-order shadowing")
10171033

10181034
# XAML101 — duplicate stateless converter across dictionaries.
10191035
conv = (f'<ResourceDictionary {_WPF_NS} xmlns:c="clr-namespace:App.Converters">\n'

0 commit comments

Comments
 (0)