On FreeBSD 11, I see this warning:

../src/ln.c:383:35: warning: data argument not used by format string 
[-Wformat-extra-args]

The code there passes two string arguments and uses
  - sometimes both,
  - sometimes only the first one,
  - sometimes only the second one, with "%.0s" to skip and ignore
    the first one.

Especially the latter trick can cause confusion among translators.
If possible, it should therefore be avoided.

Here's a patch that eliminates the warning and the use of "%.0s".
The result is somewhat bigger but just as maintainable (at least)
than the previous code.

>From 8dd442b2d804d60b52dd77c9ca5999bc7a86a2dd Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Thu, 18 Sep 2025 23:19:01 +0200
Subject: [PATCH] ln: Eliminate a clang -Wformat-extra-args warning

* src/ln.c (do_link): Don't pass unused arguments to error(). Don't use
"%.0s" to consume a string argument without printing it.
---
 src/ln.c | 37 +++++++++++++++++++++++++------------
 1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/src/ln.c b/src/ln.c
index 3a40244fb..7a366bc90 100644
--- a/src/ln.c
+++ b/src/ln.c
@@ -369,18 +369,31 @@ do_link (char const *source, int destdir_fd, char const *dest_base,
     }
   else
     {
-      error (0, link_errno,
-             (symbolic_link
-              ? (link_errno != ENAMETOOLONG && *source
-                 ? _("failed to create symbolic link %s")
-                 : _("failed to create symbolic link %s -> %s"))
-              : (link_errno == EMLINK
-                 ? _("failed to create hard link to %.0s%s")
-                 : (link_errno == EDQUOT || link_errno == EEXIST
-                    || link_errno == ENOSPC || link_errno == EROFS)
-                 ? _("failed to create hard link %s")
-                 : _("failed to create hard link %s => %s"))),
-             quoteaf_n (0, dest), quoteaf_n (1, source));
+      char *dest_quoted = quoteaf_n (0, dest);
+      char *source_quoted = quoteaf_n (1, source);
+
+      if (symbolic_link)
+        {
+          if (link_errno != ENAMETOOLONG && *source)
+            error (0, link_errno, _("failed to create symbolic link %s"),
+                   dest_quoted);
+          else
+            error (0, link_errno, _("failed to create symbolic link %s -> %s"),
+                   dest_quoted, source_quoted);
+        }
+      else
+        {
+          if (link_errno == EMLINK)
+            error (0, link_errno, _("failed to create hard link to %s"),
+                   source_quoted);
+          else if (link_errno == EDQUOT || link_errno == EEXIST
+                   || link_errno == ENOSPC || link_errno == EROFS)
+            error (0, link_errno, _("failed to create hard link %s"),
+                   dest_quoted);
+          else
+            error (0, link_errno, _("failed to create hard link %s => %s"),
+                   dest_quoted, source_quoted);
+        }
 
       if (backup_base)
         {
-- 
2.51.0

Reply via email to