From 459869a528ff02787255391ab90f68195c27b807 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Jo=C3=A3o=20T=C3=A1vora?= <joaotavora@gmail.com>
Date: Sun, 10 Feb 2019 21:38:46 +0000
Subject: [PATCH] Properly remove stale Flymake diagnostics on :region reports

Among other bugs fixed, modifying a list structure while iterating it
is a no-no.  This would again cause duplicate diagnostics.  See
https://github.com/joaotavora/eglot/issues/223 for an example.

* lisp/progmodes/flymake.el (Version): Bump to 1.0.5
(flymake--handle-report): Use cl-loop.
---
 lisp/progmodes/flymake.el | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/lisp/progmodes/flymake.el b/lisp/progmodes/flymake.el
index 15a4d259859..d991ccafc98 100644
--- a/lisp/progmodes/flymake.el
+++ b/lisp/progmodes/flymake.el
@@ -4,7 +4,7 @@
 
 ;; Author:  Pavel Kobyakov <pk_at_work@yahoo.com>
 ;; Maintainer: João Távora <joaotavora@gmail.com>
-;; Version: 1.0.4
+;; Version: 1.0.5
 ;; Package-Requires: ((emacs "26.1"))
 ;; Keywords: c languages tools
 
@@ -742,14 +742,13 @@ report applies to that region."
           ;; the associated overlay.
           (cond
            (region
-            (dolist (diag (flymake--backend-state-diags state))
-              (let ((diag-beg (flymake--diag-beg diag))
-                    (diag-end (flymake--diag-beg diag)))
-                (when (and (< diag-beg (cdr region))
-                           (> diag-end (car region)))
-                  (delete-overlay (flymake--diag-overlay diag))
-                  (setf (flymake--backend-state-diags state)
-                        (delq diag (flymake--backend-state-diags state)))))))
+            (cl-loop for diag in (flymake--backend-state-diags state)
+                     if (or (> (flymake--diag-end diag) (car region))
+                            (< (flymake--diag-beg diag) (cdr region)))
+                     do (delete-overlay (flymake--diag-overlay diag))
+                     else collect diag into surviving
+                     finally (setf (flymake--backend-state-diags state)
+                                   surviving)))
            (first-report
             (dolist (diag (flymake--backend-state-diags state))
               (delete-overlay (flymake--diag-overlay diag)))
-- 
2.39.5