|
|
Subscribe / Log in / New account

Unused parameters in C are easy

Unused parameters in C are easy

Posted Feb 11, 2025 14:42 UTC (Tue) by alx.manpages (subscriber, #145117)
In reply to: Catching the mismatch by farnz
Parent article: Maintainer opinions on Rust-for-Linux

The way to have unused parameters in C is even easier than in Rust. No need for an underscore; just leave it unnamed.

In the following program, I don't use argc. Simply don't name it.

```
alx@devuan:~/tmp$ cat unused.c
#include <stdio.h>
int
main(int, char *argv[])
{
puts(argv[0]);
return 0;
}
alx@devuan:~/tmp$ gcc -Wall -Wextra unused.c
alx@devuan:~/tmp$ ./a.out
./a.out
```


to post comments

Unused parameters in C are easy

Posted Feb 11, 2025 14:47 UTC (Tue) by farnz (subscriber, #17727) [Link] (4 responses)

OK, so how do I name the parameter for documentation purposes while marking it as unused?

Unused parameters in C are easy

Posted Feb 11, 2025 15:15 UTC (Tue) by alx.manpages (subscriber, #145117) [Link] (3 responses)

> how do I name the parameter for documentation purposes while marking it as unused?

Then you'll need what others have mentioned.

Either a comment:

int
main(int /*argc*/, char *argv[])
{...}

or a cast to void:

int
main(int argc, char *argv[])
{
(void) argc;

...
}

But Rust's _ is no documentation name either, so the C equivalent is indeed no name at all.
The cast-to-void is nice, when you want a name.

Unused parameters in C are worse than in Rust

Posted Feb 11, 2025 15:18 UTC (Tue) by farnz (subscriber, #17727) [Link] (2 responses)

I'm sorry, you've confused me; why is Rust's use of _ignored_cpu_mask "no documentation name either"? All names starting with an underscore are flagged as explicitly unused, not just the underscore on its own. And the C things so far are all much more work than just adding an `_` to the name to indicate that it's currently deliberately unused - my contention is that if you don't make it really simple, people will prefer to turn off the entire warning rather than use the convention for unused parameters.

Unused parameters in C are worse than in Rust

Posted Feb 11, 2025 15:30 UTC (Tue) by alx.manpages (subscriber, #145117) [Link]

> why is Rust's use of _ignored_cpu_mask "no documentation name either"?
> All names starting with an underscore are flagged as explicitly unused, not just the underscore on its own.

Ahh, sorry, I missed that. How about this?

#define _ [[maybe_unused]]

int
main(_ int argc, char *argv[])
{...}

The _() function already exists (for internationalization of strings), which is why I wouldn't shadow it with this macro, but if you don't use _(), when you could define the undescore to be [[maybe_unused]]. Or you could find another name that serves you.

Unused parameters in C are worse than in Rust

Posted Feb 11, 2025 21:33 UTC (Tue) by mathstuf (subscriber, #69389) [Link]

I filed this issue[1] about the case of default implementations of trait methods which don't use all of their arguments. I don't want the underscore-leading name to show up in the docs for the trait, but I also don't want the diagnostic. `let _for_rustdoc = unused_arg;` works and documents *why* I am doing it, but I'd still like a better solution.

[1] https://github.com/rust-lang/rust/issues/91074


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds