]> git.eshelyaron.com Git - emacs.git/commitdiff
Better `sort` ignored-return-value warning
authorMattias EngdegÄrd <mattiase@acm.org>
Sun, 24 Mar 2024 17:18:41 +0000 (18:18 +0100)
committerEshel Yaron <me@eshelyaron.com>
Fri, 29 Mar 2024 12:33:49 +0000 (13:33 +0100)
* lisp/emacs-lisp/bytecomp.el (byte-compile-form)
(bytecomp--actually-important-return-value-p):
Special handling of `sort` that takes into account that it may return
an important value depending on the :in-place keyword argument.

(cherry picked from commit b20866c4b3aa1446efda252bd5c3fa54f68c5d7f)

lisp/emacs-lisp/bytecomp.el

index 7af568cfe34a1125672f62431d657f9b8c6e86da..2b5eb34e57109fecc7a5688eb690daa930882740 100644 (file)
@@ -3445,6 +3445,7 @@ lambda-expression."
                ((and (or sef (function-get (car form) 'important-return-value))
                      ;; Don't warn for arguments to `ignore'.
                      (not (eq byte-compile--for-effect 'for-effect-no-warn))
+                     (bytecomp--actually-important-return-value-p form)
                      (byte-compile-warning-enabled-p
                       'ignored-return-value (car form)))
                 (byte-compile-warn-x
@@ -3471,6 +3472,15 @@ lambda-expression."
       (if byte-compile--for-effect
           (byte-compile-discard)))))
 
+(defun bytecomp--actually-important-return-value-p (form)
+  "Whether FORM is really a call with a return value that should not go unused.
+This assumes the function has the `important-return-value' property."
+  (cond ((eq (car form) 'sort)
+         ;; For `sort', we only care about non-destructive uses.
+         (and (zerop (% (length form) 2))  ; new-style call
+              (not (plist-get (cddr form) :in-place))))
+        (t t)))
+
 (let ((important-return-value-fns
        '(
          ;; These functions are side-effect-free except for the
@@ -3478,9 +3488,11 @@ lambda-expression."
          mapcar mapcan mapconcat
          assoc plist-get plist-member
 
-         ;; It's safe to ignore the value of `sort' and `nreverse'
+         ;; It's safe to ignore the value of `nreverse'
          ;; when used on arrays, but most calls pass lists.
-         nreverse sort
+         nreverse
+
+         sort      ; special handling (non-destructive calls only)
 
          match-data