diff --git a/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp b/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp @@ -124,12 +124,12 @@ SmallVector Backs; Backs.reserve(Namespaces.size()); - NamespaceDecl const *LastND = nullptr; + NamespaceDecl const *LastNonNestND = nullptr; for (const NamespaceDecl *ND : Namespaces) { if (ND->isNested()) continue; - LastND = ND; + LastNonNestND = ND; std::optional SR = getCleanedNamespaceFrontRange(ND, SM, LangOpts); if (!SR.has_value()) @@ -137,7 +137,7 @@ Fronts.push_back(SR.value()); Backs.push_back(getCleanedNamespaceBackRange(ND, SM, LangOpts)); } - if (LastND == nullptr || Fronts.empty() || Backs.empty()) + if (LastNonNestND == nullptr || Fronts.empty() || Backs.empty()) return; // the last one should be handled specially Fronts.pop_back(); @@ -147,9 +147,11 @@ for (SourceRange const &Front : Fronts) DB << FixItHint::CreateRemoval(Front); DB << FixItHint::CreateReplacement( - SourceRange{LastND->getBeginLoc(), LastND->getLocation()}, + SourceRange{LastNonNestND->getBeginLoc(), + Namespaces.back()->getLocation()}, ConcatNameSpace); - if (LastRBrace != SourceRange{LastND->getRBraceLoc(), LastND->getRBraceLoc()}) + if (LastRBrace != + SourceRange{LastNonNestND->getRBraceLoc(), LastNonNestND->getRBraceLoc()}) DB << FixItHint::CreateReplacement(LastRBrace, ("} // " + ConcatNameSpace).str()); for (SourceRange const &Back : llvm::reverse(Backs)) diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp @@ -105,7 +105,7 @@ namespace n28 { namespace n29::n30 { // CHECK-MESSAGES-DAG: :[[@LINE-3]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces] -// CHECK-FIXES: namespace n26::n27::n28::n29::n30 +// CHECK-FIXES: namespace n26::n27::n28::n29::n30 { void t() {} } // namespace n29::n30 } // namespace n28