Skip to content

Commit e329bfc

Browse files
authored
[clang-tidy] Ignore implicit functions in readability-implicit-bool-conversion (#94512)
Ignore implicit declarations and defaulted functions. Helps with issues in generated code like, C++ spaceship operator. Closes #93409
1 parent 32b7043 commit e329bfc

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
279279
hasParent(callExpr()),
280280
hasSourceExpression(binaryOperator(hasAnyOperatorName("==", "!="))));
281281

282+
auto IsInCompilerGeneratedFunction = hasAncestor(namedDecl(anyOf(
283+
isImplicit(), functionDecl(isDefaulted()), functionTemplateDecl())));
284+
282285
Finder->addMatcher(
283286
traverse(TK_AsIs,
284287
implicitCastExpr(
@@ -299,7 +302,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
299302
// additional parens in replacement.
300303
optionally(hasParent(stmt().bind("parentStmt"))),
301304
unless(isInTemplateInstantiation()),
302-
unless(hasAncestor(functionTemplateDecl())))
305+
unless(IsInCompilerGeneratedFunction))
303306
.bind("implicitCastToBool")),
304307
this);
305308

@@ -331,7 +334,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
331334
anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
332335
anything()),
333336
unless(isInTemplateInstantiation()),
334-
unless(hasAncestor(functionTemplateDecl())))),
337+
unless(IsInCompilerGeneratedFunction))),
335338
this);
336339
}
337340

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ Changes in existing checks
408408
valid fix suggestions for ``static_cast`` without a preceding space and
409409
fixed problem with duplicate parentheses in double implicit casts. Corrected
410410
the fix suggestions for C23 and later by using C-style casts instead of
411-
``static_cast``.
411+
``static_cast``. Fixed false positives in C++20 spaceship operator by ignoring
412+
casts in implicit and defaulted functions.
412413

413414
- Improved :doc:`readability-redundant-inline-specifier
414415
<clang-tidy/checks/readability/redundant-inline-specifier>` check to properly
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %check_clang_tidy -std=c++20 %s readability-implicit-bool-conversion %t
2+
3+
namespace std {
4+
struct strong_ordering {
5+
int n;
6+
constexpr operator int() const { return n; }
7+
static const strong_ordering equal, greater, less;
8+
};
9+
constexpr strong_ordering strong_ordering::equal = {0};
10+
constexpr strong_ordering strong_ordering::greater = {1};
11+
constexpr strong_ordering strong_ordering::less = {-1};
12+
} // namespace std
13+
14+
namespace PR93409 {
15+
struct X
16+
{
17+
auto operator<=>(const X&) const = default;
18+
bool m_b;
19+
};
20+
21+
struct Y
22+
{
23+
auto operator<=>(const Y&) const = default;
24+
X m_x;
25+
};
26+
27+
bool compare(const Y& y1, const Y& y2)
28+
{
29+
return y1 == y2 || y1 < y2 || y1 > y2;
30+
}
31+
}

0 commit comments

Comments
 (0)