Static declarations in header files is considered bad practice,
since header files are meant for sharing code, and static
leads to internal linkage. This can cause code bloat and lead
to subtle issues, for example ODR violations. Essentially, this
is as problematic as having anonymous namespaces in headers,
which we already have checks for.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang-tools-extra/clang-tidy/misc/StaticDeclarationInHeaderCheck.cpp | ||
---|---|---|
47 | Look like this setting should be global for Clang-tidy, because it already used in other checks. It also accompanied by other option. | |
clang-tools-extra/docs/ReleaseNotes.rst | ||
123 | Please add first sentence from documentation. | |
clang-tools-extra/docs/clang-tidy/checks/misc/static-declaration-in-header.rst | ||
40 | Please replace quotes with single back-ticks. |
Add description in release notes.
clang-tools-extra/docs/clang-tidy/checks/misc/static-declaration-in-header.rst | ||
---|---|---|
40 | There's 3 other such instances of this pattern:
None of them follow the format you propose, so I'd be reluctant to introduce a third convention :) Would it be OK to keep the most prominent format for consistency, and fix the formatting for all in a separate patch? |
clang-tools-extra/docs/clang-tidy/checks/misc/static-declaration-in-header.rst | ||
---|---|---|
40 | Sorry, there's 2, not 3 (the third one is this check :) ) |
clang-tools-extra/docs/clang-tidy/checks/misc/static-declaration-in-header.rst | ||
---|---|---|
40 | Single back-ticks are used for option values. Will be good idea to fix other places too. |
Warn only about cases where there really is a problem.
There are other cases which are just a readability issue,
where a separate check for "redundant static" would fit
better. Remove also the automatic fix-it since it leads
to potentially broken code.
I'm happy with the check now, feel free to review! I have run it on the LLVM repo and it seems to do what it's supposed to (including fix-its).
Friendly ping. I realized @aaron.ballman you probably have very good insights into this use case, since (AFAICT) you wrote a guideline about anonymous namespaces in headers, which is very closely related to this:
https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL59-CPP.+Do+not+define+an+unnamed+namespace+in+a+header+file
Would appreciate your feedback!
Look like this setting should be global for Clang-tidy, because it already used in other checks. It also accompanied by other option.