This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] check for flagging using declarations not in the inner most namespace
Needs ReviewPublic

Authored by Ywicheng on Dec 6 2018, 10:04 PM.

Details

Summary

This patch adds one check corresponding to the Scope of the Alias section in https://abseil.io/tips/119.

In particular, it is better to put aliases in the innermost namespace when there is any such scope.

Diff Detail

Event Timeline

Ywicheng created this revision.Dec 6 2018, 10:04 PM
JonasToth added a project: Restricted Project.

Please upload the patch with full context.

aaron.ballman added inline comments.Dec 10 2018, 4:55 AM
clang-tidy/abseil/SafelyScopedCheck.cpp
23

Why is this not also checking that the using declaration is not within a header file?

25–28

Did clang-format produce this? If not, you should run the patch through clang-format.

33

I suspect this diagnostic is going to be too aggressive and once you add more tests will find it needs some additional constraints.

33–34

Do not add links to diagnostic messages. They should stand on their own, and not be grammatical with punctuation. I would suggest: using declaration not declared in the innermost namespace

test/clang-tidy/abseil-safely-scoped.cpp
2

You're missing a lot of tests, such as using declarations at function scope, within a class scope, etc.

Ywicheng updated this revision to Diff 194614.Apr 10 2019, 5:07 PM
Ywicheng marked an inline comment as done.
Ywicheng edited the summary of this revision. (Show Details)
Ywicheng updated this revision to Diff 194623.Apr 10 2019, 6:33 PM
Ywicheng marked 2 inline comments as done.
Ywicheng updated this revision to Diff 194631.Apr 10 2019, 7:12 PM
aaron.ballman added inline comments.Apr 11 2019, 10:22 AM
clang-tidy/abseil/SafelyScopedCheck.cpp
38

You should remove the full stop at the end of the diagnostic.

test/clang-tidy/abseil-safely-scoped.cpp
16–17

This should be moved closer to where the actual warning will show up.

However, the diagnostic doesn't help me to understand what is wrong with the code or how to fix it. I think it might be telling me that the using declaration is supposed to be inside of the anonymous namespace, but moving the using declaration there will break the declaration of f().

Ywicheng updated this revision to Diff 195661.Apr 17 2019, 7:01 PM
Ywicheng marked 3 inline comments as done.
aaron.ballman added inline comments.Apr 22 2019, 5:44 AM
clang-tidy/abseil/SafelyScopedCheck.cpp
23

I'm still wondering about this. The Abseil tip specifically says Never declare namespace aliases or convenience using-declarations at namespace scope in header files, only in .cc files., which is why I had figured I would see some enforcement from this check.

38

The diagnostic still doesn't meet our (somewhat strange) requirements; diagnostics should not be full sentences with capitalization and punctuation. However, it also doesn't really tell the user what is wrong with their code, just how to fix it to make the diagnostic go away.

I don't have a particularly great suggestion for a replacement, however. I'm still trying to wrap my mind around the suggested changes to code, because it seems like this advice isn't general purpose. Consider:

namespace Foo { // I control this namespace.
  namespace details {
    int func(int I);
  } // namespace details

  using Frobble::Bobble;

  void f(SomeNameFromFrobbleBobble FB) {
    FB.whatever(details::func(12));
  }
} // namespace Foo

The suggestion to move the using declaration into the innermost namespace is actually a poor one -- we don't want the interface of f() to be void f(details::SomeNameFromFrobbleBobble FB), which is what this check seems to suggest we do. I don't see how we can determine whether the using declaration should or should not be moved, so I can't really tell what the diagnostic text should be.