Subject: bug#6176: [PATCH 2/2] sort: --debug: output data independent key warnings

From: Pádraig Brady <[email-masked]>
Date: Wed, 12 May 2010 10:21:48 +0100

On 12/05/10 00:39, Pádraig Brady wrote:
> The attached patch gives warnings about questionable
> option combinations. For example:
> 
> $ sort --debug -rb -k1,1n /dev/null
> ! options `-b' are ignored
> ! option `-r' only applies to last-resort comparison

Oops, The previous patch warned about -r even when no keys specified,
and didn't so the sort -ru special case right either.
This should fix it up.

diff --git a/src/sort.c b/src/sort.c
index 66a00ef..211415d 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -2245,18 +2245,20 @@ key_warnings (struct keyfield const *gkey)
       ugkey.reverse &= !key->reverse;
     }

-  /* Warn about ignored global options flagged above.  */
-  if (!default_key_compare (&ugkey) || (stable && ugkey.reverse))
+  /* Warn about ignored global options flagged above.
+     Note if gkey is the only one in the list, all flags are cleared.  */
+  if (!default_key_compare (&ugkey)
+       || (ugkey.reverse && (stable || unique) && keylist))
     {
       bool ugkey_reverse = ugkey.reverse;
-      if (!stable)
+      if (!(stable || unique))
         ugkey.reverse = false;
       char *opts = key_to_opts (&ugkey);
       fprintf (stderr, _("! options `-%s' are ignored\n"), opts);
       free (opts);
       ugkey.reverse = ugkey_reverse;
     }
-  if (!stable && ugkey.reverse)
+  if (ugkey.reverse && !(stable || unique) && keylist)
     fprintf (stderr,
              _("! option `-r' only applies to last-resort comparison\n"));
 }
diff --git a/tests/misc/sort-debug-warn b/tests/misc/sort-debug-warn
index 5295b4b..c2ff01d 100755
--- a/tests/misc/sort-debug-warn
+++ b/tests/misc/sort-debug-warn
@@ -47,6 +47,9 @@ sort -r -k1,1r --debug /dev/null 2>>out # no warning for redundant options
 sort -i -k1,1i --debug /dev/null 2>>out # no warning
 sort -d -k1,1b --debug /dev/null 2>>out
 sort -i -k1,1d --debug /dev/null 2>>out
+sort -r --debug /dev/null 2>>out #no warning
+sort -rM --debug /dev/null 2>>out #no warning
+sort -rM -k1,1 --debug /dev/null 2>>out #no warning

 compare out exp || fail=1