[clang-tools-extra] e9c8d42 - [clang-tidy] `misc-unused-using-decls`: add correct handling of `operator""` with template parametes (#129392)

via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 2 21:14:31 PST 2025


Author: Baranov Victor
Date: 2025-03-03T13:14:27+08:00
New Revision: e9c8d42b895fe4934a149478788fa020bd69f7bf

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

LOG: [clang-tidy] `misc-unused-using-decls`: add correct handling of `operator""` with template parametes (#129392)

Fixes false-positives when operator"" has template paremetes, e.g.
```cpp
template <char... Ts>
int operator""_r() {
    return {};
}
```
Closes https://github.com/llvm/llvm-project/issues/53444.

---------

Co-authored-by: Congcong Cai <congcongcai0907 at 163.com>

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
index 5d74907aa9fab..d5c5fa3364d63 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -184,8 +184,16 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
     return;
   }
   // Check user-defined literals
-  if (const auto *UDL = Result.Nodes.getNodeAs<UserDefinedLiteral>("used"))
-    removeFromFoundDecls(UDL->getCalleeDecl());
+  if (const auto *UDL = Result.Nodes.getNodeAs<UserDefinedLiteral>("used")) {
+    const Decl *CalleeDecl = UDL->getCalleeDecl();
+    if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl)) {
+      if (const FunctionTemplateDecl *FPT = FD->getPrimaryTemplate()) {
+        removeFromFoundDecls(FPT);
+        return;
+      }
+    }
+    removeFromFoundDecls(CalleeDecl);
+  }
 }
 
 void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 71edb704b49d6..ce1418a2a7d58 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -138,6 +138,10 @@ Changes in existing checks
   <clang-tidy/checks/performance/move-const-arg>` check by fixing false negatives
   on ternary operators calling ``std::move``.
 
+- Improved :doc:`misc-unused-using-decls
+  <clang-tidy/checks/misc/unused-using-decls>` check by fixing false positives
+  on ``operator""`` with template parameters.
+
 Removed checks
 ^^^^^^^^^^^^^^
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp
index 12fc18f340f21..62aa17b0b1c22 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp
@@ -222,3 +222,19 @@ using gh69714::StructGH69714_1;
 using gh69714::StructGH69714_2;
 struct StructGH69714_1 a;
 struct StructGH69714_2 *b;
+
+namespace gh53444 {
+namespace my_literals {
+  template <char... Ts>
+  int operator""_r() {
+    return {};
+  }
+}
+
+using my_literals::operator"" _r;
+
+int foo() {
+  auto x2 = 123_r;
+}
+
+}


        


More information about the cfe-commits mailing list