This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add a checker that flags unnamed parameters.
ClosedPublic

Authored by bkramer on Jul 15 2014, 6:09 AM.

Details

Summary

We still allow the escape hatch foo(int /*x*/) and also suggest this
in a fixit. This is more powerful than the corresponding cpplint.py check
it also flags functions with multiple arguments as naming all arguments is
recommended by the google style guide.

Diff Detail

Repository
rL LLVM

Event Timeline

bkramer updated this revision to Diff 11437.Jul 15 2014, 6:09 AM
bkramer retitled this revision from to [clang-tidy] Add a checker that flags unnamed parameters..
bkramer updated this object.
bkramer added reviewers: alexfh, djasper.
bkramer added a subscriber: Unknown Object (MLST).
djasper added inline comments.Jul 15 2014, 6:16 AM
clang-tidy/google/NamedParameterCheck.cpp
63 ↗(On Diff #11437)

IMO, most functions with unnamed parameters will be derived from some base method. Otherwise, why would they have an unnamed parameter (yes, there are cases, but not that many)?

To this end:

  • Add tests for methods overriding methods with named parameters.
  • I think the common pattern for the unnamed parameter comment is /*<parameter name>*/ Could we extract the parameter name from the overridden function.
alexfh added inline comments.Jul 15 2014, 6:31 AM
clang-tidy/google/NamedParameterCheck.cpp
31 ↗(On Diff #11437)

Why don't you check for isImplicit()?

36 ↗(On Diff #11437)

Alternatively this could be done in the swapped arguments check. Both places seem good for this.

50 ↗(On Diff #11437)

I'm curious where does Parm->getLocation() point in case where there is a parameter name (to the identifier?) and where there is not?

clang-tidy/google/NamedParameterCheck.h
19 ↗(On Diff #11437)

nit: Do we need an empty line to separate the \brief from the rest of the class comment?

test/clang-tidy/google-readability-function.cpp
1 ↗(On Diff #11437)

Please add tests with multiple instantiations of the same template. I suspect, currently the check will suggest multiple replacements.

bkramer added inline comments.Jul 15 2014, 6:52 AM
clang-tidy/google/NamedParameterCheck.cpp
50 ↗(On Diff #11437)

It always points to the name or the place where the name *would* be. This behavior is a bit strange sometimes but makes things like getting the name in function pointers right very easy,

test/clang-tidy/google-readability-function.cpp
1 ↗(On Diff #11437)

I checked that and it doesn't (do warnings get deduplicated somewhere)? I'll add another test.

bkramer updated this revision to Diff 11439.Jul 15 2014, 6:57 AM
  • Added a testcase with multiple template instantiations.
  • If a method has an unnamed argument and is overriding a method from a base cla

ss try to copy the argument name from the base class first.

alexfh added inline comments.Jul 15 2014, 8:24 AM
test/clang-tidy/google-readability-function.cpp
69 ↗(On Diff #11439)

What does the check do in this case?

struct Base1 { virtual void f(int i) {} };
struct Base2 { virtual void f(bool b) {} };
template <typename T, typename Y>
struct Derived : public T {
  void f(Y) {}
};
int main() {
  Derived<Base1, int>();
  Derived<Base2, bool>();
}
1 ↗(On Diff #11437)

Warnings get deduplicated by location and message text, and in your test case deduplication worked correctly. But it's not a good idea to rely on the deduplication in general.

bkramer added inline comments.Jul 15 2014, 9:02 AM
test/clang-tidy/google-readability-function.cpp
69 ↗(On Diff #11439)

I think it works because we see the template definition first. But I'll add the bulky template instance matcher, safe is better.

5:13: warning: all parameters should be named in a function [google-readability-function]
    void f(Y) {}
            ^
             /*unused*/
bkramer updated this revision to Diff 11443.Jul 15 2014, 9:06 AM

Added a check so we don't emit warnings inside of template instantiations.

alexfh accepted this revision.Jul 15 2014, 9:28 AM
alexfh edited edge metadata.

Looks good!

clang-tidy/google/NamedParameterCheck.cpp
73 ↗(On Diff #11443)

Add a comma after "overridden"?

This revision is now accepted and ready to land.Jul 15 2014, 9:28 AM
bkramer closed this revision.Jul 15 2014, 9:55 AM
bkramer updated this revision to Diff 11450.

Closed by commit rL213075 (authored by d0k).