From 19834830d68c3d08ea07fe2b5fe1ebc2964be337 Mon Sep 17 00:00:00 2001
From: Luke Cartey <lcartey@github.com>
Date: Tue, 17 Sep 2024 22:39:28 +0100
Subject: [PATCH] Rule 8.3: Remove false positives for decls that don't share a
 link target

---
 .../DeclarationsOfAnObjectSameNameAndType.ql         | 12 ++++++++++++
 change_notes/2024-09-17-rule-8-3-linker-aware.md     |  2 ++
 2 files changed, 14 insertions(+)
 create mode 100644 change_notes/2024-09-17-rule-8-3-linker-aware.md

diff --git a/c/misra/src/rules/RULE-8-3/DeclarationsOfAnObjectSameNameAndType.ql b/c/misra/src/rules/RULE-8-3/DeclarationsOfAnObjectSameNameAndType.ql
index dfd9d622e9..d68382503a 100644
--- a/c/misra/src/rules/RULE-8-3/DeclarationsOfAnObjectSameNameAndType.ql
+++ b/c/misra/src/rules/RULE-8-3/DeclarationsOfAnObjectSameNameAndType.ql
@@ -21,7 +21,19 @@ where
   not isExcluded(decl2, Declarations4Package::declarationsOfAnObjectSameNameAndTypeQuery()) and
   not decl1 = decl2 and
   not decl1.getVariable().getDeclaringType().isAnonymous() and
+  // Declarations are for the same qualified name
+  // Note: decl1.getVariable() = decl2.getVariable() does not work for common cases where an aliased
+  //       type is used.
   decl1.getVariable().getQualifiedName() = decl2.getVariable().getQualifiedName() and
+  // As we use qualified name, require that they share a common link target to ensure they are
+  // for the same object
+  (
+    decl1.getVariable().(GlobalVariable).getALinkTarget() =
+      decl2.getVariable().(GlobalVariable).getALinkTarget()
+    or
+    decl1.getVariable().(Field).getDeclaringType().(Class).getALinkTarget() =
+      decl2.getVariable().(Field).getDeclaringType().(Class).getALinkTarget()
+  ) and
   not typesCompatible(decl1.getType(), decl2.getType())
 select decl1,
   "The object $@ of type " + decl1.getType().toString() +
diff --git a/change_notes/2024-09-17-rule-8-3-linker-aware.md b/change_notes/2024-09-17-rule-8-3-linker-aware.md
new file mode 100644
index 0000000000..3e48bb1228
--- /dev/null
+++ b/change_notes/2024-09-17-rule-8-3-linker-aware.md
@@ -0,0 +1,2 @@
+ - `RULE-8-3` - `DeclarationsOfAnObjectSameNameAndType.ql`
+   - Remove false positives where two conflicting declarations are never linked together.
\ No newline at end of file