This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Do not warn about redundant static in misc-use-anonymous-namespace
ClosedPublic

Authored by carlosgalvezp on Dec 2 2022, 6:25 AM.

Details

Summary

The same functionality is already implemented in the
readability-static-definition-in-anonymous-namespace
check, including automatic fixes.

Diff Detail

Event Timeline

carlosgalvezp created this revision.Dec 2 2022, 6:25 AM
Herald added a project: Restricted Project. · View Herald Transcript
carlosgalvezp requested review of this revision.Dec 2 2022, 6:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 2 2022, 6:25 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Reorder matchers to reduce diff.

This patch should not land before that one does.

This patch should not land before that one does.

The original code is 1 day old. Do we really need to be so strict?

My plan was to apply this removal in the same patch where I add the new check, but I know I'd get comments like "please do the removal in a separate patch". So that's what I'm doing :)

I was commenting because we already have a similar diagnostic in clang-tidy 15:

namespace {
  static void foo();
  static void bar(){}
  void foo() {}
  static int x;
}
$ clang-tidy-15  -checks=\*,-llvmlibc-* /tmp/test.cpp 
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "/tmp/test.cpp"
No compilation database found in /tmp or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
5 warnings generated.
/tmp/test.cpp:3:17: warning: 'bar' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]
    static void bar(){}
    ~~~~~~~     ^
/tmp/test.cpp:5:16: warning: variable 'x' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
    static int x;
               ^
/tmp/test.cpp:5:16: warning: variable name 'x' is too short, expected at least 3 characters [readability-identifier-length]
/tmp/test.cpp:5:16: warning: 'x' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]
    static int x;
    ~~~~~~~    ^
/tmp/test.cpp:6:3: warning: anonymous namespace not terminated with a closing comment [llvm-namespace-comment]
  }
  ^
    // namespace
/tmp/test.cpp:1:13: note: anonymous namespace starts here
  namespace {
            ^

that for some reason only fires on definitions, not declarations.
I thought this was changing that old check.
I think it may be good to not have duplicate coverage,
but do we need a new check given that there is one already?

Ah, then it has the exact same functionality! My idea for readability-redundant-static was to warn about:

  • static in anonymous namespace
  • static const/constexpr at global scope (since const gives implicit internal linkage in C++).

I will see what I can do about that.

This shouldn't be a blocker for this patch though, it's just removing functionality that belongs in another check. As said, the check is 1 day old so it's unlikely anyone is relying on it yet :)

Eugene.Zelenko retitled this revision from Do not warn about redundant static in misc-use-anonymous-namespace to [clang-tidy] Do not warn about redundant static in misc-use-anonymous-namespace.Dec 2 2022, 8:37 AM
carlosgalvezp edited the summary of this revision. (Show Details)

Update commit message.

Simplify anonymous namespace matcher.

This shouldn't be a blocker for this patch though, it's just removing functionality that belongs in another check. As said, the check is 1 day old so it's unlikely anyone is relying on it yet :)

SGTM in general.

@lebedev.ri If you are happy with the patch, would you mind approving it? Or do you have additional comments that should be addressed? The patch does not need to be approved by the Code Owner.

@lebedev.ri If you are happy with the patch, would you mind approving it? Or do you have additional comments that should be addressed? The patch does not need to be approved by the Code Owner.

No further comments. Thanks.

@lebedev.ri If you are happy with the patch, would you mind approving it? Or do you have additional comments that should be addressed? The patch does not need to be approved by the Code Owner.

No further comments. Thanks.

Ops, I realize you weren't in the reviewers list, I added you now.

lebedev.ri resigned from this revision.Dec 7 2022, 8:02 AM

@njames93 @JonasToth @LegalizeAdulthood Do you have any comments that I should address? If not, could you approve the patch?

To recap: this check is a few days old (I added it myself). I realized there's functionality that shouldn't be here so I'm just removing it. Should be a fairly safe change.

aaron.ballman accepted this revision.Dec 7 2022, 8:44 AM

LGTM, though please add a release note describing the change.

This revision is now accepted and ready to land.Dec 7 2022, 8:44 AM

LGTM, though please add a release note describing the change

The original check is just a few days old. It didn't exist in the previous release. Should I still add a comment about this change?

PS thanks a lot for the review! :)

LGTM, though please add a release note describing the change

The original check is just a few days old. It didn't exist in the previous release. Should I still add a comment about this change?

Oh, I hadn't realized the original check was part of this release. No need for a release note in this case.

PS thanks a lot for the review! :)

Happy to help where I can!