Skip to content

Commit

Permalink
[clang-tidy] fix hint use correct range to replace last NamespaceDecl
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HerrCai0907 committed Apr 8, 2023
1 parent 72e049c commit 0f9b71d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,20 @@ 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())
return;
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();
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f9b71d

Please sign in to comment.