summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMattias EngdegÄrd <mattiase@acm.org>2023-05-11 19:24:51 +0200
committerMattias EngdegÄrd <mattiase@acm.org>2023-05-13 11:53:25 +0200
commitbfc07100d28d0f687da0a1dd5fdfa42a92a93f88 (patch)
tree4ca024cacb42464e68c51f07bbbae3d1fe9af4eb /test
parentfa598571adab4858282f337b45984517e197f8a9 (diff)
downloademacs-bfc07100d28d0f687da0a1dd5fdfa42a92a93f88.tar.gz
Byte-compiler warning about mutation of constant values
When we can easily detect mutation of constants (quoted lists, strings and vectors), warn. For example, (setcdr '(1 . 2) 3) (nreverse [1 2 3]) (put-text-property 0 3 'face 'highlight "moo") Such code can result in surprising behaviour and problems that are difficult to debug. * lisp/emacs-lisp/bytecomp.el (byte-compile-form, mutating-fns): Add the warning and a list of functions to warn about. * etc/NEWS: Announce. * test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-test--with-suppressed-warnings): Add test cases.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/emacs-lisp/bytecomp-tests.el30
1 files changed, 30 insertions, 0 deletions
diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el
index 222065c2e4e..9136a6cd9b3 100644
--- a/test/lisp/emacs-lisp/bytecomp-tests.el
+++ b/test/lisp/emacs-lisp/bytecomp-tests.el
@@ -1518,6 +1518,36 @@ literals (Bug#20852)."
))
'((empty-body with-suppressed-warnings))
"Warning: `with-suppressed-warnings' with empty body")
+
+ (test-suppression
+ '(defun zot ()
+ (setcar '(1 2) 3))
+ '((suspicious setcar))
+ "Warning: `setcar' on constant list (arg 1)")
+
+ (test-suppression
+ '(defun zot ()
+ (aset [1 2] 1 3))
+ '((suspicious aset))
+ "Warning: `aset' on constant vector (arg 1)")
+
+ (test-suppression
+ '(defun zot ()
+ (aset "abc" 1 ?d))
+ '((suspicious aset))
+ "Warning: `aset' on constant string (arg 1)")
+
+ (test-suppression
+ '(defun zot (x y)
+ (nconc x y '(1 2) '(3 4)))
+ '((suspicious nconc))
+ "Warning: `nconc' on constant list (arg 3)")
+
+ (test-suppression
+ '(defun zot ()
+ (put-text-property 0 2 'prop 'val "abc"))
+ '((suspicious put-text-property))
+ "Warning: `put-text-property' on constant string (arg 5)")
)
(ert-deftest bytecomp-tests--not-writable-directory ()