aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias EngdegÄrd2023-04-08 19:17:17 +0200
committerMattias EngdegÄrd2023-04-08 19:34:30 +0200
commitaef996cd34f421da6540cccb9cc3ac2153458e36 (patch)
treec2e20adfef2ebbdeb9f0854f9eee2dd21596708a
parent10b58633b566cf8f66f12e2126da3b43cd09dfc8 (diff)
downloademacs-aef996cd34f421da6540cccb9cc3ac2153458e36.tar.gz
emacs-aef996cd34f421da6540cccb9cc3ac2153458e36.zip
Consolidate existing warnings about unused return values
Move the warning about unused return values from calls to side-effect-free functions from the source-level optimiser to the code generator, where it can be unified with the special-purpose warning about unused values from `mapcar`. This change also cures spurious duplicate warnings about the same code, makes the warnings amenable to suppression through `with-suppressed-warnings`, and now warns about some unused values that weren't caught before. * lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker): Move warning away from here. * lisp/emacs-lisp/byte-run.el (with-suppressed-warnings): * lisp/emacs-lisp/bytecomp.el (byte-compile-warnings): Doc string updates. (byte-compile-form): Put the new warnings here. (byte-compile-normal-call): Move mapcar warning away from here. * lisp/emacs-lisp/bytecomp.el (byte-compile-ignore): Compile args to `ignore` for value to avoid unused-value warnings, and then discard the generated values immediately thereafter. Mostly this does not affect the generated code but in rare cases it might result in slightly worse code. * test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-test--with-suppressed-warnings): Adapt test.
-rw-r--r--lisp/emacs-lisp/byte-opt.el8
-rw-r--r--lisp/emacs-lisp/byte-run.el7
-rw-r--r--lisp/emacs-lisp/bytecomp.el39
-rw-r--r--test/lisp/emacs-lisp/bytecomp-tests.el4
4 files changed, 36 insertions, 22 deletions
diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el
index 0891ec80beb..70317e2365d 100644
--- a/lisp/emacs-lisp/byte-opt.el
+++ b/lisp/emacs-lisp/byte-opt.el
@@ -506,13 +506,7 @@ for speeding up processing.")
506 ((guard (when for-effect 506 ((guard (when for-effect
507 (if-let ((tmp (byte-opt--fget fn 'side-effect-free))) 507 (if-let ((tmp (byte-opt--fget fn 'side-effect-free)))
508 (or byte-compile-delete-errors 508 (or byte-compile-delete-errors
509 (eq tmp 'error-free) 509 (eq tmp 'error-free)))))
510 (progn
511 (byte-compile-warn-x
512 form
513 "value returned from %s is unused"
514 form)
515 nil)))))
516 (byte-compile-log " %s called for effect; deleted" fn) 510 (byte-compile-log " %s called for effect; deleted" fn)
517 (byte-optimize-form (cons 'progn (cdr form)) t)) 511 (byte-optimize-form (cons 'progn (cdr form)) t))
518 512
diff --git a/lisp/emacs-lisp/byte-run.el b/lisp/emacs-lisp/byte-run.el
index 9345665eea8..fd9913d1be8 100644
--- a/lisp/emacs-lisp/byte-run.el
+++ b/lisp/emacs-lisp/byte-run.el
@@ -650,11 +650,8 @@ in `byte-compile-warning-types'; see the variable
650`byte-compile-warnings' for a fuller explanation of the warning 650`byte-compile-warnings' for a fuller explanation of the warning
651types. The types that can be suppressed with this macro are 651types. The types that can be suppressed with this macro are
652`free-vars', `callargs', `redefine', `obsolete', 652`free-vars', `callargs', `redefine', `obsolete',
653`interactive-only', `lexical', `mapcar', `constants', 653`interactive-only', `lexical', `ignored-return-value', `constants',
654`suspicious' and `empty-body'. 654`suspicious' and `empty-body'."
655
656For the `mapcar' case, only the `mapcar' function can be used in
657the symbol list."
658 ;; Note: during compilation, this definition is overridden by the one in 655 ;; Note: during compilation, this definition is overridden by the one in
659 ;; byte-compile-initial-macro-environment. 656 ;; byte-compile-initial-macro-environment.
660 (declare (debug (sexp body)) (indent 1)) 657 (declare (debug (sexp body)) (indent 1))
diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index a122e81ba3c..4a10ae29804 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -317,7 +317,9 @@ Elements of the list may be:
317 lexical-dynamic 317 lexical-dynamic
318 lexically bound variable declared dynamic elsewhere 318 lexically bound variable declared dynamic elsewhere
319 make-local calls to `make-variable-buffer-local' that may be incorrect. 319 make-local calls to `make-variable-buffer-local' that may be incorrect.
320 mapcar mapcar called for effect. 320 ignored-return-value
321 function called without using the return value where this
322 is likely to be a mistake
321 not-unused warning about using variables with symbol names starting with _. 323 not-unused warning about using variables with symbol names starting with _.
322 constants let-binding of, or assignment to, constants/nonvariables. 324 constants let-binding of, or assignment to, constants/nonvariables.
323 docstrings docstrings that are too wide (longer than 325 docstrings docstrings that are too wide (longer than
@@ -330,7 +332,7 @@ Elements of the list may be:
330 empty-body body argument to a special form or macro is empty. 332 empty-body body argument to a special form or macro is empty.
331 333
332If the list begins with `not', then the remaining elements specify warnings to 334If the list begins with `not', then the remaining elements specify warnings to
333suppress. For example, (not mapcar) will suppress warnings about mapcar. 335suppress. For example, (not free-vars) will suppress the `free-vars' warning.
334 336
335The t value means \"all non experimental warning types\", and 337The t value means \"all non experimental warning types\", and
336excludes the types in `byte-compile--emacs-build-warning-types'. 338excludes the types in `byte-compile--emacs-build-warning-types'.
@@ -3490,6 +3492,27 @@ lambda-expression."
3490 (byte-compile-report-error 3492 (byte-compile-report-error
3491 (format-message "`%s' defined after use in %S (missing `require' of a library file?)" 3493 (format-message "`%s' defined after use in %S (missing `require' of a library file?)"
3492 (car form) form))) 3494 (car form) form)))
3495
3496 (when byte-compile--for-effect
3497 (let ((sef (function-get (car form) 'side-effect-free)))
3498 (cond
3499 ((and sef (or (eq sef 'error-free)
3500 byte-compile-delete-errors))
3501 ;; This transform is normally done in the Lisp optimiser,
3502 ;; so maybe we don't need to bother about it here?
3503 (setq form (cons 'progn (cdr form)))
3504 (setq handler #'byte-compile-progn))
3505 ((and (or sef (eq (car form) 'mapcar))
3506 (byte-compile-warning-enabled-p
3507 'ignored-return-value (car form)))
3508 (byte-compile-warn-x
3509 (car form)
3510 "value from call to `%s' is unused%s"
3511 (car form)
3512 (cond ((eq (car form) 'mapcar)
3513 "; use `mapc' or `dolist' instead")
3514 (t "")))))))
3515
3493 (if (and handler 3516 (if (and handler
3494 ;; Make sure that function exists. 3517 ;; Make sure that function exists.
3495 (and (functionp handler) 3518 (and (functionp handler)
@@ -3523,11 +3546,7 @@ lambda-expression."
3523 (byte-compile-callargs-warn form)) 3546 (byte-compile-callargs-warn form))
3524 (if byte-compile-generate-call-tree 3547 (if byte-compile-generate-call-tree
3525 (byte-compile-annotate-call-tree form)) 3548 (byte-compile-annotate-call-tree form))
3526 (when (and byte-compile--for-effect (eq (car form) 'mapcar) 3549
3527 (byte-compile-warning-enabled-p 'mapcar 'mapcar))
3528 (byte-compile-warn-x
3529 (car form)
3530 "`mapcar' called for effect; use `mapc' or `dolist' instead"))
3531 (byte-compile-push-constant (car form)) 3550 (byte-compile-push-constant (car form))
3532 (mapc 'byte-compile-form (cdr form)) ; wasteful, but faster. 3551 (mapc 'byte-compile-form (cdr form)) ; wasteful, but faster.
3533 (byte-compile-out 'byte-call (length (cdr form)))) 3552 (byte-compile-out 'byte-call (length (cdr form))))
@@ -4367,7 +4386,11 @@ This function is never called when `lexical-binding' is nil."
4367 4386
4368(defun byte-compile-ignore (form) 4387(defun byte-compile-ignore (form)
4369 (dolist (arg (cdr form)) 4388 (dolist (arg (cdr form))
4370 (byte-compile-form arg t)) 4389 ;; Compile args for value (to avoid warnings about unused values),
4390 ;; emit a discard after each, and trust the LAP peephole optimiser
4391 ;; to annihilate useless ops.
4392 (byte-compile-form arg)
4393 (byte-compile-discard))
4371 (byte-compile-form nil)) 4394 (byte-compile-form nil))
4372 4395
4373;; Return the list of items in CONDITION-PARAM that match PRED-LIST. 4396;; Return the list of items in CONDITION-PARAM that match PRED-LIST.
diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el
index 5bad1ce41a8..9ade47331df 100644
--- a/test/lisp/emacs-lisp/bytecomp-tests.el
+++ b/test/lisp/emacs-lisp/bytecomp-tests.el
@@ -1438,8 +1438,8 @@ literals (Bug#20852)."
1438 '(defun zot () 1438 '(defun zot ()
1439 (mapcar #'list '(1 2 3)) 1439 (mapcar #'list '(1 2 3))
1440 nil) 1440 nil)
1441 '((mapcar mapcar)) 1441 '((ignored-return-value mapcar))
1442 "Warning: .mapcar. called for effect") 1442 "Warning: value from call to `mapcar' is unused; use `mapc' or `dolist' instead")
1443 1443
1444 (test-suppression 1444 (test-suppression
1445 '(defun zot () 1445 '(defun zot ()