[cfe-dev] Applicability of -Wunused-param

Erik Pilkington via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 18 10:14:35 PDT 2018



On 9/18/18 12:40 AM, David Chisnall via cfe-dev wrote:
> 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.

Might be a good idea to add a fix-it note to the diagnostic to do this 
too. I think that would stop a lot of users from disabling it.

> David
>
> On 18 Sep 2018, at 02:40, George Karpenkov via cfe-dev 
> <cfe-dev at lists.llvm.org <mailto: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 <mailto:cfe-dev at lists.llvm.org>
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
> _______________________________________________
> 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/77cf9e35/attachment.html>


More information about the cfe-dev mailing list