[cfe-dev] Applicability of -Wunused-param
David Chisnall via cfe-dev
cfe-dev at lists.llvm.org
Tue Sep 18 00:40:56 PDT 2018
It’s worth noting that this warning is very easy to silence and it’s probably better to do that in most cases. There are two common ways of silencing the warning.
Starting with:
```
int foo(int a, int b)
```
You can (in all languages) do this:
```
(void)b;
```
To indicate that `b` is unused. Most projects have an `UNUSED` macro to do this. This improves code readability by indicating that the parameter is intentionally unused in this implementation of the function. C++ provides a nicer way of doing this and you can just rewrite the function declaration to:
```
int foo(int a, int)
```
Now the second parameter exists, but has no name and so is implicitly unused because nothing can possibly refer to it. This is even easier to read because you know from the first line of the function that this parameter will not be used in the definition.
I believe that clang-tidy can perform the latter transformation automatically, though it’s generally a good idea to audit the non-uses of parameters to check that they’re intentional and unavoidable.
David
> On 18 Sep 2018, at 02:40, George Karpenkov via cfe-dev <cfe-dev at lists.llvm.org> wrote:
>
> Many projects, including LLVM have to turn -Wunused-param off, as it has too many false positives.
>
> Most false positives stem from the fact that often a function has to take a parameter not because it wants to, but because:
>
> 1. It’s part of an interface, and has to be API/ABI compatible
> 2. It overrides another function which does need that parameter
>
> However, this warning is still very useful when the function is:
>
> - static
> - private
> - in an anonymous namespace
>
> This asks for a warning on unused parameters which is restricted to “private" (static local / etc) functions.
> Am I missing something there?
> Has anyone tried to implement such a warning before?
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180918/5e4e1d7d/attachment.html>
More information about the cfe-dev
mailing list