<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Sep 18, 2018, at 12:40 AM, David Chisnall <<a href="mailto:David.Chisnall@cl.cam.ac.uk" class="">David.Chisnall@cl.cam.ac.uk</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">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.</span></div></div></div></blockquote><div><br class=""></div><div>1. There’s actually three ways: one can also do __attribute__((unused))</div><div><br class=""></div><div>2. All three ways are bad for some use cases.</div><div>As a data point, I have recently tried compiling the static analyzer core with -Wunused-parameter,</div><div>and have removed 100+ lines of dead code, so the warning is clearly useful.</div><div>I really wish there was a way to enable it in a not-so-disruptive way.</div><div><br class=""></div><div>One pattern for which no good suppression work is a method in an abstract base class with a dummy implementation:</div><div><br class=""></div><div>e.g. “void foo(A a, B b, C c) {}”</div><div><br class=""></div><div>Then all three suppression ways are bad:</div><div><br class=""></div><div> - Adding "(void) p” for each parameter adds unnecessary code to the method body, hiding the fact that this is a dummy do-nothing implementation</div><div> - __attribute__((unused)) makes declaration much harder to read, and hides the meaning</div><div> - Removing the parameter name makes the documentation unusable</div><div><br class=""></div><div>George</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">Starting with:</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">```</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">int foo(int a, int b)</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">```</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">You can (in all languages) do this:</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">```</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">(void)b;</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">```</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">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:</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">```</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">int foo(int a, int)</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">```</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">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.</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">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.</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">David </span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span><div class=""></div><font class=""><span style="caret-color: rgb(0, 0, 0); background-color: rgba(255, 255, 255, 0);" class="">On 18 Sep 2018, at 02:40, George Karpenkov via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:<br class=""><br class=""></span></font></div><blockquote type="cite" class=""><font class=""><span style="caret-color: rgb(0, 0, 0); background-color: rgba(255, 255, 255, 0);" class="">Many projects, including LLVM have to turn -Wunused-param off, as it has too many false positives.<br class=""><br class="">Most false positives stem from the fact that often a function has to take a parameter not because it wants to, but because:<br class=""><br class="">1. It’s part of an interface, and has to be API/ABI compatible<br class="">2. It overrides another function which does need that parameter<br class=""><br class="">However, this warning is still very useful when the function is:<br class=""><br class="">- static <br class="">- private<br class="">- in an anonymous namespace<br class=""><br class="">This asks for a warning on unused parameters which is restricted to “private" (static local / etc) functions.<br class="">Am I missing something there?<br class="">Has anyone tried to implement such a warning before?<br class="">_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a></span></font><br class=""></blockquote></div></div></blockquote></div><br class=""></body></html>