File tree Expand file tree Collapse file tree 4 files changed +19
-15
lines changed Expand file tree Collapse file tree 4 files changed +19
-15
lines changed Original file line number Diff line number Diff line change @@ -23,14 +23,16 @@ void AvoidCStyleCastsCheck::registerMatchers(
23
23
// Filter out (EnumType)IntegerLiteral construct, which is generated
24
24
// for non-type template arguments of enum types.
25
25
// FIXME: Remove this once this is fixed in the AST.
26
- unless (hasParent (substNonTypeTemplateParmExpr ())),
27
- // Avoid matches in template instantiations.
28
- unless (isInTemplateInstantiation ()))
26
+ unless (hasParent (substNonTypeTemplateParmExpr ())))
29
27
.bind (" cast" ),
30
28
this );
29
+
31
30
Finder->addMatcher (
32
- cxxFunctionalCastExpr (unless (hasDescendant (cxxConstructExpr ())),
33
- unless (hasDescendant (initListExpr ())))
31
+ cxxFunctionalCastExpr (
32
+ hasDestinationType (hasCanonicalType (anyOf (
33
+ builtinType (), references (qualType ()), pointsTo (qualType ())))),
34
+ unless (
35
+ hasSourceExpression (anyOf (cxxConstructExpr (), initListExpr ()))))
34
36
.bind (" cast" ),
35
37
this );
36
38
}
Original file line number Diff line number Diff line change @@ -31,6 +31,9 @@ class AvoidCStyleCastsCheck : public ClangTidyCheck {
31
31
: ClangTidyCheck(Name, Context) {}
32
32
void registerMatchers (ast_matchers::MatchFinder *Finder) override ;
33
33
void check (const ast_matchers::MatchFinder::MatchResult &Result) override ;
34
+ std::optional<TraversalKind> getCheckTraversalKind () const override {
35
+ return TK_IgnoreUnlessSpelledInSource;
36
+ }
34
37
};
35
38
36
39
} // namespace clang::tidy::google::readability
Original file line number Diff line number Diff line change @@ -370,6 +370,10 @@ Changes in existing checks
370
370
to ignore unused parameters when they are marked as unused and parameters of
371
371
deleted functions and constructors.
372
372
373
+ - Improved :doc: `google-readability-casting
374
+ <clang-tidy/checks/google/readability-casting>` check to ignore constructor
375
+ calls disguised as functional casts.
376
+
373
377
- Improved :doc: `llvm-namespace-comment
374
378
<clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
375
379
``inline `` namespaces in the same format as :program: `clang-format `.
Original file line number Diff line number Diff line change @@ -322,17 +322,10 @@ void conversions() {
322
322
}
323
323
324
324
template <class T >
325
- T functional_cast_template_used_by_class (float i) {
325
+ T functional_cast_template (float i) {
326
326
return T (i);
327
327
}
328
328
329
- template <class T >
330
- T functional_cast_template_used_by_int (float i) {
331
- return T (i);
332
- // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: C-style casts are discouraged; use static_cast
333
- // CHECK-FIXES: return static_cast<T>(i);
334
- }
335
-
336
329
struct S2 {
337
330
S2 (float );
338
331
};
@@ -356,8 +349,8 @@ void functional_casts() {
356
349
auto s = S (str);
357
350
358
351
// Functional casts in template functions
359
- functional_cast_template_used_by_class <S2>(x);
360
- functional_cast_template_used_by_int <int >(x);
352
+ functional_cast_template <S2>(x);
353
+ functional_cast_template <int >(x);
361
354
362
355
// New expressions are not functional casts
363
356
auto w = new int (x);
@@ -366,4 +359,6 @@ void functional_casts() {
366
359
S2 t = T (x); // OK, constructor call
367
360
S2 u = U (x); // NOK, it's a reinterpret_cast in disguise
368
361
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast
362
+
363
+ throw S2 (5 .0f );
369
364
}
You can’t perform that action at this time.
0 commit comments