Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2025

Conversation

vbvictor
Copy link
Contributor

@vbvictor vbvictor commented Mar 1, 2025

Fixes false-positives when operator"" has template paremetes, e.g.

template <char... Ts>
int operator""_r() {
    return {};
}

Closes #53444.

@llvmbot
Copy link
Member

llvmbot commented Mar 1, 2025

@llvm/pr-subscribers-clang-tidy

Author: Baranov Victor (vbvictor)

Changes

Fixes false-positives when operator"" has template paremetes, e.g.

template &lt;char... Ts&gt;
  int operator""_r() {
    return {};
  }

Full diff: https://github.com/llvm/llvm-project/pull/129392.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (+10-2)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp (+16)
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
index 5d74907aa9fab..0b501b9c9d6a3 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>(UDL->getCalleeDecl())) {
+      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 07a79d6bbe807..ccdb741ef5879 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -137,6 +137,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;
+}
+
+}

@llvmbot
Copy link
Member

llvmbot commented Mar 1, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Baranov Victor (vbvictor)

Changes

Fixes false-positives when operator"" has template paremetes, e.g.

template &lt;char... Ts&gt;
  int operator""_r() {
    return {};
  }

Full diff: https://github.com/llvm/llvm-project/pull/129392.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (+10-2)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp (+16)
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
index 5d74907aa9fab..0b501b9c9d6a3 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>(UDL->getCalleeDecl())) {
+      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 07a79d6bbe807..ccdb741ef5879 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -137,6 +137,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;
+}
+
+}

@vbvictor vbvictor changed the title [clang-tidy]: misc-unused-using-decls: add correct handling of operator"" with template parametes [clang-tidy] misc-unused-using-decls: add correct handling of operator"" with template parametes Mar 1, 2025
Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Co-authored-by: Congcong Cai <congcongcai0907@163.com>
@vbvictor
Copy link
Contributor Author

vbvictor commented Mar 2, 2025

@HerrCai0907 Could you please merge this PR, thank you.

@HerrCai0907 HerrCai0907 merged commit e9c8d42 into llvm:main Mar 3, 2025
12 checks passed
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
…ator""` with template parametes (llvm#129392)

Fixes false-positives when operator"" has template paremetes, e.g.
```cpp
template <char... Ts>
int operator""_r() {
    return {};
}
```
Closes llvm#53444.

---------

Co-authored-by: Congcong Cai <congcongcai0907@163.com>
@vbvictor vbvictor deleted the issue-53444 branch June 22, 2025 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] False Positive in misc-unused-using-decls with user-declared literals
3 participants