setlocale: Silence a -Wdiscarded-qualifiers warning on Windows.

Collin Funk <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Message-ID <[email protected]>
On Windows (x86_64-w64-mingw32), I see the following warning:

    $ gnulib-tool --create-testdir --dir testdir1
    $ cd testdir1
    $ ./configure CFLAGS='-Wdiscarded-qualifiers'
    $ cd glllib
    $ make setlocale.o
    depbase=`echo setlocale.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
    gcc -DHAVE_CONFIG_H -DEXEEXT=\".exe\" -DEXEEXT=\".exe\" -DNO_XMALLOC -DEXEEXT=\".exe\" -I. -I..  -DGNULIB_STRICT_CHECKING=1 -I/mingw64/include  -Wdiscarded-qualifiers -MT setlocale.o -MD -MP -MF $depbase.Tpo -c -o setlocale.o setlocale.c &&\
    mv -f $depbase.Tpo $depbase.Po
    setlocale.c: In function 'setlocale_single':
    setlocale.c:889:12: warning: return discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
      889 |     return setlocale_messages (locale);
          |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~

The attached patch fixes it by casting away the const. Another
reasonable solution is changing the definition of setlocale_messages to
return a plain "char *". However, I think it would be a bit strange for
it to have a different prototype than setlocale_messages_null. I've left
it unpushed in case you prefer the other solution, Bruno.

Collin
0001-setlocale-Silence-a-Wdiscarded-qualifiers-warning-on.patch (text/x-patch, 1.3 KB)
From 868ed28fde7b9d1b8176303b6aba205170dc4559 Mon Sep 17 00:00:00 2001
Message-ID: <868ed28fde7b9d1b8176303b6aba205170dc4559.1780797990.git.collin.funk1@gmail.com>
From: Collin Funk <[email protected]>
Date: Sat, 6 Jun 2026 19:05:41 -0700
Subject: [PATCH] setlocale: Silence a -Wdiscarded-qualifiers warning on
 Windows.

* lib/setlocale.c (setlocale_single): Cast away the const from
setlocale_messages.
---
 ChangeLog       | 6 ++++++
 lib/setlocale.c | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index e194208c19..d788795f59 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2026-06-06  Collin Funk  <[email protected]>
+
+	setlocale: Silence a -Wdiscarded-qualifiers warning on Windows.
+	* lib/setlocale.c (setlocale_single): Cast away the const from
+	setlocale_messages.
+
 2026-06-06  Bruno Haible  <[email protected]>
 
 	mbrtowc: Fix handling of s==NULL on all possible platforms.
diff --git a/lib/setlocale.c b/lib/setlocale.c
index d40e1e2efd..adaefec8a1 100644
--- a/lib/setlocale.c
+++ b/lib/setlocale.c
@@ -886,7 +886,7 @@ static char *
 setlocale_single (int category, const char *locale)
 {
   if (category == LC_MESSAGES)
-    return setlocale_messages (locale);
+    return (char *) setlocale_messages (locale);
   else
     return setlocale_unixlike (category, locale);
 }
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.