]> git.eshelyaron.com Git - emacs.git/commitdiff
Only issue lexical cookie warning for elisp files
authorMattias EngdegÄrd <mattiase@acm.org>
Sat, 4 May 2024 08:08:19 +0000 (10:08 +0200)
committerEshel Yaron <me@eshelyaron.com>
Mon, 6 May 2024 16:38:33 +0000 (18:38 +0200)
* src/lread.c (string_suffix_p): New.
(warn_missing_cookie): Suppress warning for files not ending in ".el",
except ".emacs".
* etc/NEWS: Update accordingly, and mention how the warning can be
suppressed.

(cherry picked from commit 1121f17d7c4bc3b71edcd0799b894f50aa3a715e)

etc/NEWS
src/lread.c

index 5489532b76cae8f66494932171577f7cc8710fce..875a7d2cdb1afa014cf73015fac21d01d9206233 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2576,8 +2576,9 @@ Its warning name is 'docstrings-wide'.
 Emacs now emits a run-time warning if an Elisp source file being loaded
 lacks the '-*- lexical-binding: ... -*-' cookie on the first line.
 See the lexical-binding compiler warning described above for how to make
-the warning go away.  The user's init file (whose name is the value of
-'user-init-file') and early-init file are exempt from this warning.
+the warning go away by adding a declaration to the file.  You can also
+suppress the warning by adding an entry for the warning type
+'lexical-warning' to 'warning-suppress-types'.
 
 ---
 ** New user option 'native-comp-async-warnings-errors-kind'.
index a8ea52a888dd0117fc43b98a848f5f6e2a7ded25..ba890cb673d3da0dd8e8293071e08b538eceb3da 100644 (file)
@@ -1342,20 +1342,33 @@ close_file_unwind_android_fd (void *ptr)
 
 #endif
 
+static bool
+string_suffix_p (const char *string, ptrdiff_t string_len,
+                const char *suffix, ptrdiff_t suffix_len)
+{
+  return string_len >= suffix_len && memcmp (string + string_len - suffix_len,
+                                            suffix, suffix_len) == 0;
+}
+
 static void
 warn_missing_cookie (Lisp_Object file)
 {
-  Lisp_Object msg;
-
-  /* The user init file should not be subject to these warnings, as
-     Emacs doesn't insert cookies into generated init files.  */
-  if (!NILP (Fequal (file, Vuser_init_file)))
+  /* Only warn for files whose name end in .el, to suppress loading of
+     data-as-code.  ".emacs" is an exception, since it does tend to contain
+     actual hand-written code.  */
+  if (!STRINGP (file))
+    return;
+  const char *name = SSDATA (file);
+  ptrdiff_t nb = SBYTES (file);
+  if (!(string_suffix_p (name, nb, ".el", 3)
+       || (string_suffix_p (name, nb, ".emacs", 6)
+           && (nb == 6 || SREF (file, nb - 7) == '/'))))
     return;
 
-  msg = CALLN (Fformat,
-              build_string ("File %s lacks `lexical-binding'"
-                            " directive on its first line"),
-              file);
+  Lisp_Object msg = CALLN (Fformat,
+                          build_string ("File %s lacks `lexical-binding'"
+                                        " directive on its first line"),
+                          file);
   Vdelayed_warnings_list = Fcons (list2 (Qlexical_binding, msg),
                                  Vdelayed_warnings_list);
 }