jean
jean

Reputation: 171

Hide GCC warning "set but not used"?

I want to do a function to get a pointer on a struct. I done this :

void *getTokenList() {
    static t_token *list;

    return &list;
}

At compilation, I have this warning : warning: variable ‘list’ set but not used [-Wunused-but-set-variable]

Is it possible to disable this warning for this function (only this one), or put an GCC attribute on this variable to hide this warning?

I had put #pragma GCC diagnostic ignored "-Wunused-but-set-variable" in top of my file but I want to hide this warning ONLY for this variable in this function.

Thanks, Jean

Upvotes: 15

Views: 16456

Answers (4)

andreee
andreee

Reputation: 4679

The upcoming C23 standard will provide attributes as a standardized way to consider such cases (very similar to attributes in C++).

With this you can write

[[maybe_unused]] static t_token *list;

Although not yet standardized, the functionality is already available from gcc 10.1, even with pre C23 code, see this example on godbolt.

Further resources:

Upvotes: 0

chqrlie
chqrlie

Reputation: 144520

This warning was a bug in gcc. The warning, introduced in version 4.6, can easy be disabled as explained in other answers, but it should be noted that current versions of gcc do not produce the warning.

Upvotes: 1

hroptatyr
hroptatyr

Reputation: 4809

static t_token *__attribute__((unused)) list;

Upvotes: 2

cnicutar
cnicutar

Reputation: 182609

You can use this to shut it off:

(void)list;

Alternatively and less portably you can use __attribute__((unused)).

Upvotes: 21

Related Questions