Skip to content

Commit

Permalink
[clang-tidy] Improved readability-bool-conversion to be more consiste…
Browse files Browse the repository at this point in the history
…nt when using parentheses (#72068)

Provides more consistent suggestions when parentheses are added to the return value.

Fixes #71852
  • Loading branch information
felix642 committed Nov 14, 2023
1 parent 2aec866 commit 2602d88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ StringRef getEquivalentBoolLiteralForExpr(const Expr *Expression,
return "false";
}

if (const auto *IntLit = dyn_cast<IntegerLiteral>(Expression)) {
if (const auto *IntLit =
dyn_cast<IntegerLiteral>(Expression->IgnoreParens())) {
return (IntLit->getValue() == 0) ? "false" : "true";
}

Expand Down Expand Up @@ -385,7 +386,7 @@ void ImplicitBoolConversionCheck::handleCastFromBool(
<< DestType;

if (const auto *BoolLiteral =
dyn_cast<CXXBoolLiteralExpr>(Cast->getSubExpr())) {
dyn_cast<CXXBoolLiteralExpr>(Cast->getSubExpr()->IgnoreParens())) {
Diag << tooling::fixit::createReplacement(
*Cast, getEquivalentForBoolLiteral(BoolLiteral, DestType, Context));
} else {
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ Changes in existing checks
- Improved :doc:`readability-implicit-bool-conversion
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
do-while loops into account for the `AllowIntegerConditions` and
`AllowPointerConditions` options.
`AllowPointerConditions` options. It also now provides more consistent
suggestions when parentheses are added to the return value.

- Improved :doc:`readability-non-const-parameter
<clang-tidy/checks/readability/non-const-parameter>` check to ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,36 @@ bool f(S& s) {

} // namespace ignore_1bit_bitfields

int implicitConversionReturnInt()
{
return true;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion bool -> 'int'
// CHECK-FIXES: return 1
}

int implicitConversionReturnIntWithParens()
{
return (true);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion bool -> 'int'
// CHECK-FIXES: return 1
}


bool implicitConversionReturnBool()
{
return 1;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion 'int' -> bool
// CHECK-FIXES: return true
}

bool implicitConversionReturnBoolWithParens()
{
return (1);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: implicit conversion 'int' -> bool
// CHECK-FIXES: return true
}


namespace PR47000 {
int to_int(bool x) { return int{x}; }

Expand Down

0 comments on commit 2602d88

Please sign in to comment.