Skip to content

Commit 03b3c93

Browse files
committed
Add tests and notes for NonTypeTemplateParmDecl
1 parent d3c974a commit 03b3c93

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ void RedundantCastingCheck::check(const MatchFinder::MatchResult &Result) {
242242
DiagnosticIDs::Note);
243243
return;
244244
}
245+
246+
if (const auto *D = dyn_cast<NonTypeTemplateParmDecl>(SourceExprDecl)) {
247+
diag(D->getLocation(),
248+
"source type originates from referencing this non-type template "
249+
"parameter",
250+
DiagnosticIDs::Note);
251+
return;
252+
}
245253
}
246254

247255
} // namespace clang::tidy::readability

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,24 @@ void testRValueCast(int&& a) {
198198
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
199199
// CHECK-FIXES: {{^}} int&& c = 10;
200200
}
201+
202+
template <int V>
203+
void testRedundantNTTPCasting() {
204+
int a = static_cast<int>(V);
205+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
206+
// CHECK-MESSAGES: :[[@LINE-4]]:15: note: source type originates from referencing this non-type template parameter
207+
// CHECK-FIXES: {{^}} int a = V;
208+
}
209+
210+
template <typename T, T V>
211+
void testValidNTTPCasting() {
212+
int a = static_cast<int>(V);
213+
}
214+
215+
template <typename T, T V>
216+
void testRedundantDependentNTTPCasting() {
217+
T a = static_cast<T>(V);
218+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant explicit casting to the same type 'T' as the sub-expression, remove this casting [readability-redundant-casting]
219+
// CHECK-MESSAGES: :[[@LINE-4]]:25: note: source type originates from referencing this non-type template parameter
220+
// CHECK-FIXES: {{^}} T a = V;
221+
}

0 commit comments

Comments
 (0)