[clang-tools-extra] 0f9b71d - [clang-tidy] fix hint use correct range to replace last NamespaceDecl

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 8 12:04:13 PDT 2023


Author: Congcong Cai
Date: 2023-04-08T21:03:59+02:00
New Revision: 0f9b71d11b91d6fb7fa678a12327bafbc43605b5

URL: https://github.com/llvm/llvm-project/commit/0f9b71d11b91d6fb7fa678a12327bafbc43605b5
DIFF: https://github.com/llvm/llvm-project/commit/0f9b71d11b91d6fb7fa678a12327bafbc43605b5.diff

LOG: [clang-tidy] fix hint use correct range to replace last NamespaceDecl

range of replacing last namespace decl should be from last non nested namespace to last namespace

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D147843

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp b/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
index d89b0a9165e88..e18dcc2b3e591 100644
--- a/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
@@ -124,12 +124,12 @@ void ConcatNestedNamespacesCheck::reportDiagnostic(
   SmallVector<SourceRange, 6> 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<SourceRange> SR =
         getCleanedNamespaceFrontRange(ND, SM, LangOpts);
     if (!SR.has_value())
@@ -137,7 +137,7 @@ void ConcatNestedNamespacesCheck::reportDiagnostic(
     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 @@ void ConcatNestedNamespacesCheck::reportDiagnostic(
   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
index a67c279ddd765..7728ada6a18db 100644
--- 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 n26::n27 {
 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


        


More information about the cfe-commits mailing list