[PATCH] D131517: implicit conversion to std::optional<bool>
Perry Huang via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 9 12:40:50 PDT 2022
huangperry created this revision.
Herald added a subscriber: carlosgalvezp.
Herald added a project: All.
huangperry requested review of this revision.
Herald added a project: clang-tools-extra.
GitHub Issue #56009
Show implicit conversion warning to and from std::optional objects when running clang-tidy.
Examples:
std::optional<bool> a = 5;
std::optional<int> b = false;
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D131517
Files:
clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
Index: clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -8,6 +8,7 @@
#include "ImplicitBoolConversionCheck.h"
#include "clang/AST/ASTContext.h"
+#include "clang/AST/ExprCXX.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
#include "clang/Tooling/FixIt.h"
@@ -270,7 +271,9 @@
// Prior to C++11 cast from bool literal to pointer was allowed.
allOf(anyOf(hasCastKind(CK_NullToPointer),
hasCastKind(CK_NullToMemberPointer)),
- hasSourceExpression(cxxBoolLiteral()))),
+ hasSourceExpression(cxxBoolLiteral())),
+ hasDescendant(materializeTemporaryExpr(
+ allOf(hasType(booleanType()), hasDescendant(cxxBoolLiteral()))))),
hasSourceExpression(expr(hasType(booleanType()))),
unless(ExceptionCases));
auto BoolXor =
@@ -282,7 +285,12 @@
anyOf(hasCastKind(CK_IntegralToBoolean),
hasCastKind(CK_FloatingToBoolean),
hasCastKind(CK_PointerToBoolean),
- hasCastKind(CK_MemberPointerToBoolean)),
+ hasCastKind(CK_MemberPointerToBoolean),
+ hasSourceExpression(cxxConstructExpr(
+ hasType(classTemplateSpecializationDecl(
+ hasName("::std::optional"),
+ hasTemplateArgument(0, refersToType(booleanType())))),
+ unless(hasArgument(0, expr(hasType(booleanType()))))))),
// Exclude case of using if or while statements with variable
// declaration, e.g.:
// if (int var = functionCall()) {}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131517.451242.patch
Type: text/x-patch
Size: 2012 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220809/32cfe211/attachment.bin>
More information about the cfe-commits
mailing list