Skip to content

[clang-tidy] Improved readability-bool-conversion to be more consistent when using parentheses #72068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -410,7 +410,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