Skip to content

Commit

Permalink
[clang-tidy] fix false positve for namespace with attrs in modernize-…
Browse files Browse the repository at this point in the history
…concat-nested-namespaces

Fixed #57530
Add pre check to avoid false positive for namespace with attributes like
```
namespace [[deprecated]] ns {}
```

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D147857
  • Loading branch information
HerrCai0907 committed Apr 9, 2023
1 parent 0bc0edb commit bdf7fd8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ static StringRef getRawStringRef(const SourceRange &Range,
return Lexer::getSourceText(TextRange, Sources, LangOpts);
}

static bool anonymousOrInlineNamespace(const NamespaceDecl &ND) {
return ND.isAnonymousNamespace() || ND.isInlineNamespace();
static bool unsupportedNamespace(const NamespaceDecl &ND) {
return ND.isAnonymousNamespace() || ND.isInlineNamespace() ||
!ND.attrs().empty();
}

static bool singleNamedNamespaceChild(const NamespaceDecl &ND) {
Expand All @@ -41,7 +42,7 @@ static bool singleNamedNamespaceChild(const NamespaceDecl &ND) {
return false;

const auto *ChildNamespace = dyn_cast<const NamespaceDecl>(*Decls.begin());
return ChildNamespace && !anonymousOrInlineNamespace(*ChildNamespace);
return ChildNamespace && !unsupportedNamespace(*ChildNamespace);
}

static bool alreadyConcatenated(std::size_t NumCandidates,
Expand Down Expand Up @@ -166,7 +167,7 @@ void ConcatNestedNamespacesCheck::check(
if (!locationsInSameFile(Sources, ND.getBeginLoc(), ND.getRBraceLoc()))
return;

if (anonymousOrInlineNamespace(ND))
if (unsupportedNamespace(ND))
return;

Namespaces.push_back(&ND);
Expand Down
7 changes: 4 additions & 3 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,10 @@ Changes in existing checks
<clang-tidy/checks/google/readability-avoid-underscore-in-googletest-name>` when using
``DISABLED_`` in the test suite name.

- Fixed an issue in :doc:`modernize-concat-nested-namespaces
<clang-tidy/checks/modernize/concat-nested-namespaces>` when using macro between
namespace declarations could result incorrect fix.
- Improved :doc:`modernize-concat-nested-namespaces
<clang-tidy/checks/modernize/concat-nested-namespaces>` to fix incorrect fixes when
using macro between namespace declarations and false positive when using namespace
with attributes.

- Fixed a false positive in :doc:`performance-no-automatic-move
<clang-tidy/checks/performance/no-automatic-move>` when warning would be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ void t();
} // namespace n2

namespace n5 {
inline namespace n6 {
inline namespace inline_ns {
void t();
}
} // namespace inline_ns
} // namespace n5

namespace n6 {
namespace [[deprecated]] attr_ns {
void t();
} // namespace attr_ns
} // namespace n6

namespace n7 {
void t();

Expand Down

0 comments on commit bdf7fd8

Please sign in to comment.