branch: externals/relint commit cbde6e35c777ce9e1fe5ace93d3d4374c867d2fd Author: Mattias Engdegård <matti...@acm.org> Commit: Mattias Engdegård <matti...@acm.org>
Add check for duplicates in rx or-forms Since rx may run `regexp-opt` on strings in an or-pattern and that function implicitly deduplicates the set of strings, we would otherwise not know about the duplication. --- relint.el | 17 +++++++++++++++++ test/11.elisp | 3 +++ test/11.expected | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/relint.el b/relint.el index 753a6e2c32..2b293cff99 100644 --- a/relint.el +++ b/relint.el @@ -1470,6 +1470,23 @@ than just to a surrounding or producing expression." 'group 'submatch 'group-n 'submatch-n) . ,args) + (when (memq (car item) '(| or)) + ;; Check or-patterns for duplicates, because if rx runs `regexp-opt' + ;; on them then they are effectively deduplicated and we'd never + ;; know about it. + (let ((i 1) + (tail args)) + (while (consp tail) + (when (member (car tail) (cdr tail)) + (relint--warn pos (if exact-path (cons i path) path) + (format-message + "Duplicated rx form in or-pattern: %s" + (replace-regexp-in-string + (rx (+ (in " \n"))) " " + (string-trim (xr-pp-rx-to-str (car tail))) + t t)))) + (setq i (1+ i)) + (setq tail (cdr tail))))) ;; Form with subforms: recurse. (let ((i 1)) (dolist (arg args) diff --git a/test/11.elisp b/test/11.elisp index d629a5639a..3f4527b23a 100644 --- a/test/11.elisp +++ b/test/11.elisp @@ -31,4 +31,7 @@ (rx (any "a-z" ?m)) (rx (any "a-f" "\000-\377")) (rx (any "\240-\277" "\000-\377")) + + (rx (or "abc" ?A "def" "ghi" ?A "def")) + (rx (| "abc" (= 3 ?*) "def" (= 3 ?*) "ghi" "abc")) ) diff --git a/test/11.expected b/test/11.expected index 2c0eb6b5b1..9b8c9b1b19 100644 --- a/test/11.expected +++ b/test/11.expected @@ -59,3 +59,7 @@ 11.elisp:33:25: Range `\000-\377' overlaps previous `\240-\277' (pos 0) "\000-\377" ^ +11.elisp:35:17: Duplicated rx form in or-pattern: ?A +11.elisp:35:20: Duplicated rx form in or-pattern: "def" +11.elisp:36:10: Duplicated rx form in or-pattern: "abc" +11.elisp:36:16: Duplicated rx form in or-pattern: (= 3 ?*)