1

I'm creating a clang-tidy checker to replace a function with another function. The replaced function is relying on a using namespace ..; declaration. As part of my clang-tidy checker I also want to remove that declaration.

#include <filesystem>

namespace fs = std::filesystem;
using namespace ns1::ns2; // declaration I want to remove
using namespace std;

If have it currently working to remove the SourceRange of using namespace ns1::ns2 and trailing ;. But this leaves a empty line behind as this diff shows:

 #include <filesystem>

 namespace fs = std::filesystem;
-using namespace ns1::ns2;
+
 using namespace std;

I achieved it with clang-tidy check():

void RenameFunctionCheck::check(const MatchFinder::MatchResult &Result)
{
  // rename function

  if (const auto *MatchNS = Result.Nodes.getNodeAs<UsingDirectiveDecl>("replaceNS")) {
    ASTContext *Context = Result.Context;
    SourceLocation Semicolon = Lexer::getLocForEndOfToken(
      MatchedNS->getIdentLocation(), 0, Context->getSourceManager(),
      Context->getLangOpts());
    
    diag(MatchedNS->getUsingLoc(), "obsolete nested namespace ns1::ns2")
        << FixItHint::CreateRemoval(MatchedNS->getSourceRange())
        << FixItHint::CreateRemoval(Semicolon);
  }
}

How do I delete the whole line?

ByteNudger
  • 1,545
  • 5
  • 29
  • 37

0 Answers0