]> granicus.if.org Git - nethack/commitdiff
silence compiler warning in droppables()
authorPatR <rankin@nethack.org>
Wed, 22 Sep 2021 14:59:36 +0000 (07:59 -0700)
committerPatR <rankin@nethack.org>
Wed, 22 Sep 2021 14:59:36 +0000 (07:59 -0700)
Reported by entrez as a github issue for evilhack, 'gcc -2' issued
warnings about droppables() possibly returning the address of a local
variable (&dummy).  It is mistaken; that never gets returned because
of various checks being performed.  But making 'dummy' be static adds
negligible cost and should shut it up (not verified but no doubt
about viability...).

src/dogmove.c

index cbb82c306c71c22a68d1e4d3a48496691374085a..16c0b9d120b7557331267f9ac272953f913de39e 100644 (file)
@@ -23,7 +23,19 @@ static void quickmimic(struct monst *);
 struct obj *
 droppables(struct monst *mon)
 {
-    struct obj *obj, *wep, dummy, *pickaxe, *unihorn, *key;
+    /*
+     * 'key|pickaxe|&c = &dummy' is used to make various creatures
+     * that can't use a key/pick-axe/&c behave as if they are already
+     * holding one so that any other such item in their inventory will
+     * be considered a duplicate and get treated as a normal candidate
+     * for dropping.
+     *
+     * This could be 'auto', but then 'gcc -O2' warns that this function
+     * might return the address of a local variable.  It's mistaken,
+     * &dummy is never returned.  'static' is simplest way to shut it up.
+     */
+    static struct obj dummy;
+    struct obj *obj, *wep, *pickaxe, *unihorn, *key;
 
     dummy = cg.zeroobj;
     dummy.otyp = GOLD_PIECE; /* not STRANGE_OBJECT or tools of interest */