From 94963b5b3ab8d70840fc658b45267979fe536f0e Mon Sep 17 00:00:00 2001
From: Jeff Young <jeff@rokeby.ie>
Date: Tue, 7 Feb 2023 18:25:18 +0000
Subject: [PATCH] Remove DRC exclusions that no longer have items.

Fixes https://gitlab.com/kicad/code/kicad/issues/13802

(cherry picked from commit 13391176ae2f447d4669675b977e08bf14e9b683)
---
 pcbnew/board.cpp          | 14 ++++++++++++++
 pcbnew/pcb_edit_frame.cpp |  5 +----
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/pcbnew/board.cpp b/pcbnew/board.cpp
index 17353aa115..d365ccfb82 100644
--- a/pcbnew/board.cpp
+++ b/pcbnew/board.cpp
@@ -316,6 +316,20 @@ std::vector<PCB_MARKER*> BOARD::ResolveDRCExclusions()
     {
         PCB_MARKER* marker = PCB_MARKER::Deserialize( exclusionData );
 
+        if( !marker )
+            continue;
+
+        // Check to see if items still exist
+        for( const KIID& guid : marker->GetRCItem()->GetIDs() )
+        {
+            if( GetItem( guid ) == DELETED_BOARD_ITEM::GetInstance() )
+            {
+                delete marker;
+                marker = nullptr;
+                break;
+            }
+        }
+
         if( marker )
         {
             marker->SetExcluded( true );
diff --git a/pcbnew/pcb_edit_frame.cpp b/pcbnew/pcb_edit_frame.cpp
index 60e39bf7e6..c7bde0bd34 100644
--- a/pcbnew/pcb_edit_frame.cpp
+++ b/pcbnew/pcb_edit_frame.cpp
@@ -987,10 +987,7 @@ void PCB_EDIT_FRAME::ResolveDRCExclusions()
     for( PCB_MARKER* marker : GetBoard()->Markers() )
     {
         if( marker->GetSeverity() == RPT_SEVERITY_EXCLUSION )
-        {
-            GetCanvas()->GetView()->Remove( marker );
-            GetCanvas()->GetView()->Add( marker );
-        }
+            GetCanvas()->GetView()->Update( marker );
     }
 
     GetBoard()->UpdateRatsnestExclusions();