emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/xr 4b8d3bc 1/6: Detect `-' not first or last in [...]


From: Mattias Engdegård
Subject: [elpa] externals/xr 4b8d3bc 1/6: Detect `-' not first or last in [...]
Date: Sat, 13 Apr 2019 12:51:16 -0400 (EDT)

branch: externals/xr
commit 4b8d3bcd6a77594b086ec1511443f5bf7b84d0aa
Author: Mattias Engdegård <address@hidden>
Commit: Mattias Engdegård <address@hidden>

    Detect `-' not first or last in [...]
    
    This is arguably bad style and sometimes indicates a problem,
    like in "[+-0-9]".
    This check is also carried out for skip-sets.
---
 xr-test.el | 11 +++++++++++
 xr.el      | 18 ++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/xr-test.el b/xr-test.el
index dbcf84c..4dbd525 100644
--- a/xr-test.el
+++ b/xr-test.el
@@ -367,6 +367,9 @@
                  nil))
   (should (equal (xr-lint "[^]-][]-^]")
                  '((6 . "Two-character range `]-^'"))))
+  (should (equal
+           (xr-lint "[-A-Z][A-Z-][A-Z-a][^-A-Z][]-a][A-Z---.]")
+           '((16 . "Literal `-' not first or last in character alternative"))))
   )
 
 (ert-deftest xr-skip-set ()
@@ -431,6 +434,14 @@
                  '((0 . "Empty set matches nothing"))))
   (should (equal (xr-skip-set-lint "^")
                  '((0 . "Negated empty set matches anything"))))
+  (should (equal (xr-skip-set-lint "A-Z-")
+                 nil))
+  (should (equal (xr-skip-set-lint "-A-Z")
+                 nil))
+  (should (equal (xr-skip-set-lint "^-A-Z")
+                 nil))
+  (should (equal (xr-skip-set-lint "A-Z-z")
+                 '((3 . "Literal `-' not first or last"))))
 )
 
 (provide 'xr-test)
diff --git a/xr.el b/xr.el
index 7540cef..9f3e214 100644
--- a/xr.el
+++ b/xr.el
@@ -114,7 +114,8 @@
     (push (cons (1- position) message) (car warnings))))
 
 (defun xr--parse-char-alt (negated warnings)
-  (let ((intervals nil)
+  (let ((start-pos (point))
+        (intervals nil)
         (classes nil))
     (cond
      ;; Initial ]-x range
@@ -190,6 +191,11 @@
                                (eq (aref (car (last intervals)) 0) ?\]))))
             (xr--report warnings (point)
                         "Suspect `[' in char alternative"))
+          (when (and (looking-at (rx "-" (not (any "]"))))
+                     (> (point) start-pos))
+            (xr--report
+             warnings (point)
+             "Literal `-' not first or last in character alternative"))
           (push (vector ch ch (point)) intervals))
         (forward-char 1))))
 
@@ -660,10 +666,12 @@
     (xr--report warnings (point) "Suspect skip set framed in `[...]'"))
 
   (let ((negated (looking-at (rx "^")))
+        (start-pos (point))
         (ranges nil)
         (classes nil))
     (when negated
-      (forward-char 1))
+      (forward-char 1)
+      (setq start-pos (point)))
     (while (not (eobp))
       (cond
        ((looking-at (rx "[:" (group (*? anything)) ":]"))
@@ -705,6 +713,12 @@
             (xr--report warnings (1- (match-beginning 3))
                         (xr--escape-string
                          (format "Unnecessarily escaped `%c'" end) nil)))
+          (when (and (eq start ?-)
+                     (not end)
+                     (match-beginning 2)
+                     (< start-pos (point) (1- (point-max))))
+            (xr--report warnings (point)
+                        "Literal `-' not first or last"))
           (if (and end (> start end))
               (xr--report warnings (point)
                           (xr--escape-string



reply via email to

[Prev in Thread] Current Thread [Next in Thread]