Skip to content
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

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

Merged

Conversation

felix642
Copy link
Contributor

Fixes #71852

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 12, 2023

@llvm/pr-subscribers-clang-tidy

Author: Félix-Antoine Constantin (felix642)

Changes

Fixes #71852


Full diff: https://github.com/llvm/llvm-project/pull/72068.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp (+2-2)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp (+30)
diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index 69e6d73c4fcd7bb..9e627d281e4b0f9 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -152,7 +152,7 @@ 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";
   }
 
@@ -385,7 +385,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 {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index f49c412118e7d98..ab9ef8cdfb37a00 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -412,6 +412,10 @@ Changes in existing checks
   do-while loops into account for the `AllowIntegerConditions` and
   `AllowPointerConditions` options.
 
+- Improved :doc:`readability-implicit-bool-conversion
+  <clang-tidy/checks/readability/implicit-bool-conversion>` check to provide
+  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
   false-positives in initializer list of record.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp
index 323cf813c047000..f7f5d506a9ce0e0 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp
@@ -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}; }
 

Copy link

github-actions bot commented Nov 12, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@felix642 felix642 force-pushed the clang-tidy-readability-implicit-bool-return-value branch from 54743ce to 649c7b8 Compare November 12, 2023 21:11
@firewave
Copy link

Another PR for this was opened a few hours ago: #71848.

@felix642
Copy link
Contributor Author

Hi @firewave, I think you are referencing a different issue. If I test #71852 with PR #72050 I do not get the expected behavior.

@firewave
Copy link

firewave commented Nov 12, 2023

Hi @firewave, I think you are referencing a different issue. If I test #71852 with PR #72050 I do not get the expected behavior.

Of course you are right. I missed there being two different issues and even posted the wrong ID. Sorry about the noise.

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description for a change (commit / PR) needed.
Except that looks fine.

@felix642 felix642 force-pushed the clang-tidy-readability-implicit-bool-return-value branch from fc87991 to 2b54c90 Compare November 14, 2023 01:43
…onsistent when using parentheses

Updated ReleaseNotes.rst
…onsistent when using parentheses

[clang-tidy] Improved readability-bool-conversion to be more consistent

The check would sometimes suggest replacing the value with a literal,
but if parentheses were added it would instead suggest to static_cast
the value. With this commit the check always suggest to replace with a
literal when possible.'

Fixes llvm#71852
@felix642 felix642 force-pushed the clang-tidy-readability-implicit-bool-return-value branch from a400579 to a91fc94 Compare November 14, 2023 11:44
@PiotrZSL PiotrZSL merged commit 2602d88 into llvm:main Nov 14, 2023
4 checks passed
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
…nt when using parentheses (llvm#72068)

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

Fixes llvm#71852
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] readability-implicit-bool-conversion gives inconsistent suggestions on return value
4 participants