Skip to content

Commit ba34476

Browse files
authored
[clang-tidy] Fix crash in modernize-use-constraints (#92019)
Improved modernize-use-constraints check by fixing a crash that occurred in some scenarios and excluded system headers from analysis. Problem were with DependentNameTypeLoc having null type location as getQualifierLoc().getTypeLoc(). Fixes #91872
1 parent 3a8d176 commit ba34476

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

clang-tools-extra/clang-tidy/modernize/UseConstraintsCheck.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ AST_MATCHER(FunctionDecl, hasOtherDeclarations) {
4141
void UseConstraintsCheck::registerMatchers(MatchFinder *Finder) {
4242
Finder->addMatcher(
4343
functionTemplateDecl(
44+
// Skip external libraries included as system headers
45+
unless(isExpansionInSystemHeader()),
4446
has(functionDecl(unless(hasOtherDeclarations()), isDefinition(),
4547
hasReturnTypeLoc(typeLoc().bind("return")))
4648
.bind("function")))
@@ -57,6 +59,8 @@ matchEnableIfSpecializationImplTypename(TypeLoc TheType) {
5759
return std::nullopt;
5860
}
5961
TheType = Dep.getQualifierLoc().getTypeLoc();
62+
if (TheType.isNull())
63+
return std::nullopt;
6064
}
6165

6266
if (const auto SpecializationLoc =

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ Changes in existing checks
322322
don't remove parentheses used in ``sizeof`` calls when they have array index
323323
accesses as arguments.
324324

325+
- Improved :doc:`modernize-use-constraints
326+
<clang-tidy/checks/modernize/use-constraints>` check by fixing a crash that
327+
occurred in some scenarios and excluding system headers from analysis.
328+
325329
- Improved :doc:`modernize-use-nullptr
326330
<clang-tidy/checks/modernize/use-nullptr>` check to include support for C23,
327331
which also has introduced the ``nullptr`` keyword.

clang-tools-extra/docs/clang-tidy/checks/modernize/use-constraints.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ The tool will replace the above code with,
6868
// The tool will not emit a diagnostic or attempt to replace the code.
6969
template <typename T, std::enable_if_t<T::some_trait, int> = 0>
7070
struct my_class {};
71+
72+
.. note::
73+
74+
System headers are not analyzed by this check.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-constraints.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,3 +724,35 @@ void not_last_param() {
724724
}
725725

726726
} // namespace enable_if_trailing_type_parameter
727+
728+
729+
// Issue fixes:
730+
731+
namespace PR91872 {
732+
733+
enum expression_template_option { value1, value2 };
734+
735+
template <typename T> struct number_category {
736+
static const int value = 0;
737+
};
738+
739+
constexpr int number_kind_complex = 1;
740+
741+
template <typename T, expression_template_option ExpressionTemplates>
742+
struct number {
743+
using type = T;
744+
};
745+
746+
template <typename T> struct component_type {
747+
using type = T;
748+
};
749+
750+
template <class T, expression_template_option ExpressionTemplates>
751+
inline typename std::enable_if<
752+
number_category<T>::value == number_kind_complex,
753+
component_type<number<T, ExpressionTemplates>>>::type::type
754+
abs(const number<T, ExpressionTemplates> &v) {
755+
return {};
756+
}
757+
758+
}

0 commit comments

Comments
 (0)