Skip to content

Commit e9c8d42

Browse files
[clang-tidy] misc-unused-using-decls: add correct handling of operator"" 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 <[email protected]>
1 parent c804e86 commit e9c8d42

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,16 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
184184
return;
185185
}
186186
// Check user-defined literals
187-
if (const auto *UDL = Result.Nodes.getNodeAs<UserDefinedLiteral>("used"))
188-
removeFromFoundDecls(UDL->getCalleeDecl());
187+
if (const auto *UDL = Result.Nodes.getNodeAs<UserDefinedLiteral>("used")) {
188+
const Decl *CalleeDecl = UDL->getCalleeDecl();
189+
if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl)) {
190+
if (const FunctionTemplateDecl *FPT = FD->getPrimaryTemplate()) {
191+
removeFromFoundDecls(FPT);
192+
return;
193+
}
194+
}
195+
removeFromFoundDecls(CalleeDecl);
196+
}
189197
}
190198

191199
void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ Changes in existing checks
138138
<clang-tidy/checks/performance/move-const-arg>` check by fixing false negatives
139139
on ternary operators calling ``std::move``.
140140

141+
- Improved :doc:`misc-unused-using-decls
142+
<clang-tidy/checks/misc/unused-using-decls>` check by fixing false positives
143+
on ``operator""`` with template parameters.
144+
141145
Removed checks
142146
^^^^^^^^^^^^^^
143147

clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,19 @@ using gh69714::StructGH69714_1;
222222
using gh69714::StructGH69714_2;
223223
struct StructGH69714_1 a;
224224
struct StructGH69714_2 *b;
225+
226+
namespace gh53444 {
227+
namespace my_literals {
228+
template <char... Ts>
229+
int operator""_r() {
230+
return {};
231+
}
232+
}
233+
234+
using my_literals::operator"" _r;
235+
236+
int foo() {
237+
auto x2 = 123_r;
238+
}
239+
240+
}

0 commit comments

Comments
 (0)