[clangd] Fix the code action `RemoveUsingNamespace`
authorv1nh1shungry <v1nh1shungry@outlook.com>
Wed, 9 Nov 2022 20:04:20 +0000 (21:04 +0100)
committerTom Praschan <13141438+tom-anders@users.noreply.github.com>
Wed, 9 Nov 2022 20:04:21 +0000 (21:04 +0100)
Avoid adding qualifiers before user-defined literals

Reviewed By: tom-anders

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

clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp

index 93fdbb9..f4228ae 100644 (file)
@@ -155,12 +155,23 @@ Expected<Tweak::Effect> RemoveUsingNamespace::apply(const Selection &Inputs) {
             if (!visibleContext(T->getDeclContext())
                      ->Equals(TargetDirective->getNominatedNamespace()))
               return;
+            auto Kind = T->getDeclName().getNameKind();
             // Avoid adding qualifiers before operators, e.g.
             //   using namespace std;
             //   cout << "foo"; // Must not changed to std::cout std:: << "foo"
-            // FIXME: User-defined literals are not handled
-            if (T->isInIdentifierNamespace(
-                    Decl::IdentifierNamespace::IDNS_NonMemberOperator))
+            if (Kind == DeclarationName::CXXOperatorName)
+              return;
+            // Avoid adding qualifiers before user-defined literals, e.g.
+            //   using namespace std;
+            //   auto s = "foo"s; // Must not changed to auto s = "foo" std::s;
+            // FIXME: Add a using-directive for user-defined literals
+            // declared in an inline namespace, e.g.
+            //   using namespace s^td;
+            //   int main() { cout << "foo"s; }
+            // change to
+            //   using namespace std::literals;
+            //   int main() { std::cout << "foo"s; }
+            if (Kind == DeclarationName::NameKind::CXXLiteralOperatorName)
               return;
           }
           SourceLocation Loc = Ref.NameLoc;
index 3449c64..03a46d9 100644 (file)
@@ -249,6 +249,21 @@ TEST_F(RemoveUsingNamespaceTest, All) {
         ns::Foo foo;
         foo + 10;
       }
+    )cpp"},
+      {// Does not qualify user-defined literals
+       R"cpp(
+      namespace ns {
+      long double operator "" _w(long double);
+      }
+      using namespace n^s;
+      int main() { 1.5_w; }
+    )cpp",
+       R"cpp(
+      namespace ns {
+      long double operator "" _w(long double);
+      }
+      
+      int main() { 1.5_w; }
     )cpp"}};
   for (auto C : Cases)
     EXPECT_EQ(C.second, apply(C.first)) << C.first;