From 1e963747156d21c9c6f4313c13df1035fad3ffd8 Mon Sep 17 00:00:00 2001
From: Seth Hillbrand <seth@kipro-pcb.com>
Date: Fri, 8 Jan 2021 08:36:18 -0800
Subject: [PATCH] Consider tracks and vias together

Dangling tracks and vias affect each other and need to be considered at
the same time in order to fully remove the dangling segments
---
 pcbnew/tracks_cleaner.cpp | 16 +++++++++-------
 pcbnew/tracks_cleaner.h   |  5 +++--
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/pcbnew/tracks_cleaner.cpp b/pcbnew/tracks_cleaner.cpp
index 7159d63a33..06472216d0 100644
--- a/pcbnew/tracks_cleaner.cpp
+++ b/pcbnew/tracks_cleaner.cpp
@@ -66,11 +66,7 @@ void TRACKS_CLEANER::CleanupBoard( bool aDryRun, std::vector<std::shared_ptr<CLE
     if( aDeleteTracksinPad )
         deleteTracksInPads();
 
-    if( aDeleteUnconnected )
-        has_deleted = deleteDanglingTracks( false );
-
-    if( aDeleteDanglingVias )
-        has_deleted |= deleteDanglingTracks( true );
+    has_deleted = deleteDanglingTracks( aDeleteUnconnected, aDeleteDanglingVias );
 
     if( has_deleted && aMergeSegments )
         cleanup( false, false, false, true );
@@ -160,11 +156,14 @@ bool TRACKS_CLEANER::testTrackEndpointIsNode( TRACK* aTrack, bool aTstStart )
 }
 
 
-bool TRACKS_CLEANER::deleteDanglingTracks( bool aVia )
+bool TRACKS_CLEANER::deleteDanglingTracks( bool aTrack, bool aVia )
 {
     bool item_erased = false;
     bool modified = false;
 
+    if( !aTrack && !aVia )
+        return false;
+
     do // Iterate when at least one track is deleted
     {
         item_erased = false;
@@ -176,7 +175,10 @@ bool TRACKS_CLEANER::deleteDanglingTracks( bool aVia )
 
         for( TRACK* track : temp_tracks )
         {
-            if( ( aVia && track->Type() != PCB_VIA_T ) || ( !aVia && track->Type() == PCB_VIA_T ) )
+            if( !aVia && track->Type() == PCB_VIA_T )
+                continue;
+
+            if( !aTrack && ( track->Type() == PCB_TRACE_T || track->Type() == PCB_ARC_T ) )
                 continue;
 
             // Test if a track (or a via) endpoint is not connected to another track or zone.
diff --git a/pcbnew/tracks_cleaner.h b/pcbnew/tracks_cleaner.h
index bb1c4b034d..71c18af4e8 100644
--- a/pcbnew/tracks_cleaner.h
+++ b/pcbnew/tracks_cleaner.h
@@ -59,10 +59,11 @@ private:
 
     /**
      * Removes tracks or vias only connected on one end
-     * @param aVia if true, clean vias, if false clean tracks
+     * @param aTrack if true, clean dangling tracks
+     * @param aVia if true, clean dangling vias
      * @return true if any items were deleted
      */
-    bool deleteDanglingTracks( bool aVia );
+    bool deleteDanglingTracks( bool aTrack, bool aVia );
 
     void deleteTracksInPads();