[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 12 16:50:24 PDT 2024
https://github.com/Da-Viper updated https://github.com/llvm/llvm-project/pull/102831
>From 8a4f6af9fc1f44c2f8b5fd3693ca14eaf776fd02 Mon Sep 17 00:00:00 2001
From: Ebuka Ezike <yerimyah1 at gmail.com>
Date: Sun, 11 Aug 2024 21:39:35 +0100
Subject: [PATCH 1/2] [clang-tidy] use upper cace letters for bool conversion
suffix
When readability-implicit-bool-conversion-check and readability-uppercase-literal-suffix-check is enabled this will cause you to apply a fix twice
from (!i) -> (i == 0u) to (i == 0U) twice instead will skip the middle one
---
.../readability/ImplicitBoolConversionCheck.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index aa115cd450c4f6..258cec80dd0bac 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -43,10 +43,10 @@ StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind,
ASTContext &Context) {
switch (CastExprKind) {
case CK_IntegralToBoolean:
- return Type->isUnsignedIntegerType() ? "0u" : "0";
+ return Type->isUnsignedIntegerType() ? "0U" : "0";
case CK_FloatingToBoolean:
- return Context.hasSameType(Type, Context.FloatTy) ? "0.0f" : "0.0";
+ return Context.hasSameType(Type, Context.FloatTy) ? "0.0F" : "0.0";
case CK_PointerToBoolean:
case CK_MemberPointerToBoolean: // Fall-through on purpose.
@@ -202,13 +202,13 @@ StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral,
if (DestType->isFloatingType()) {
if (Context.hasSameType(DestType, Context.FloatTy)) {
- return BoolLiteral->getValue() ? "1.0f" : "0.0f";
+ return BoolLiteral->getValue() ? "1.0F" : "0.0F";
}
return BoolLiteral->getValue() ? "1.0" : "0.0";
}
if (DestType->isUnsignedIntegerType()) {
- return BoolLiteral->getValue() ? "1u" : "0u";
+ return BoolLiteral->getValue() ? "1U" : "0U";
}
return BoolLiteral->getValue() ? "1" : "0";
}
>From efb102b86e6ba2d05141040faf31eaaff5886f18 Mon Sep 17 00:00:00 2001
From: Ebuka Ezike <yerimyah1 at gmail.com>
Date: Tue, 13 Aug 2024 00:49:41 +0100
Subject: [PATCH 2/2] Update test cases
---
.../readability/implicit-bool-conversion.c | 14 +++++-----
.../readability/implicit-bool-conversion.cpp | 28 +++++++++----------
2 files changed, 21 insertions(+), 21 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c
index a8c69858f76b61..29869b100894f4 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c
@@ -94,7 +94,7 @@ void implicitConversionFromBoolLiterals() {
functionTakingUnsignedLong(false);
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: implicit conversion 'bool' -> 'unsigned long'
- // CHECK-FIXES: functionTakingUnsignedLong(0u);
+ // CHECK-FIXES: functionTakingUnsignedLong(0U);
functionTakingSignedChar(true);
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: implicit conversion 'bool' -> 'signed char'
@@ -102,7 +102,7 @@ void implicitConversionFromBoolLiterals() {
functionTakingFloat(false);
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: implicit conversion 'bool' -> 'float'
- // CHECK-FIXES: functionTakingFloat(0.0f);
+ // CHECK-FIXES: functionTakingFloat(0.0F);
functionTakingDouble(true);
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'bool' -> 'double'
@@ -159,12 +159,12 @@ void implicitConversionToBoolSimpleCases() {
unsigned long unsignedLong = 10;
functionTakingBool(unsignedLong);
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'unsigned long' -> 'bool'
- // CHECK-FIXES: functionTakingBool(unsignedLong != 0u);
+ // CHECK-FIXES: functionTakingBool(unsignedLong != 0U);
float floating = 0.0f;
functionTakingBool(floating);
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: functionTakingBool(floating != 0.0f);
+ // CHECK-FIXES: functionTakingBool(floating != 0.0F);
double doubleFloating = 1.0f;
functionTakingBool(doubleFloating);
@@ -193,7 +193,7 @@ void implicitConversionToBoolInSingleExpressions() {
bool boolComingFromFloat;
boolComingFromFloat = floating;
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: boolComingFromFloat = (floating != 0.0f);
+ // CHECK-FIXES: boolComingFromFloat = (floating != 0.0F);
signed char character = 'a';
bool boolComingFromChar;
@@ -231,7 +231,7 @@ bool implicitConversionToBoolInReturnValue() {
float floating = 1.0f;
return floating;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: return floating != 0.0f;
+ // CHECK-FIXES: return floating != 0.0F;
}
void implicitConversionToBoolFromLiterals() {
@@ -287,7 +287,7 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() {
functionTakingBool(-0.0f);
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: functionTakingBool((-0.0f) != 0.0f);
+ // CHECK-FIXES: functionTakingBool((-0.0f) != 0.0F);
functionTakingBool(-0.0);
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'double' -> 'bool'
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 d6e7dcc4d8867b..f3c4481a7cc430 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
@@ -98,7 +98,7 @@ void implicitConversionFromBoolLiterals() {
functionTaking<unsigned long>(false);
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit conversion 'bool' -> 'unsigned long'
- // CHECK-FIXES: functionTaking<unsigned long>(0u);
+ // CHECK-FIXES: functionTaking<unsigned long>(0U);
functionTaking<signed char>(true);
// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: implicit conversion 'bool' -> 'signed char'
@@ -106,7 +106,7 @@ void implicitConversionFromBoolLiterals() {
functionTaking<float>(false);
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: implicit conversion 'bool' -> 'float'
- // CHECK-FIXES: functionTaking<float>(0.0f);
+ // CHECK-FIXES: functionTaking<float>(0.0F);
functionTaking<double>(true);
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: implicit conversion 'bool' -> 'double'
@@ -177,14 +177,14 @@ void implicitConversionToBoolSimpleCases() {
unsigned long unsignedLong = 10;
functionTaking<bool>(unsignedLong);
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'unsigned long' -> 'bool'
- // CHECK-FIXES: functionTaking<bool>(unsignedLong != 0u);
+ // CHECK-FIXES: functionTaking<bool>(unsignedLong != 0U);
- float floating = 0.0f;
+ float floating = 0.0F;
functionTaking<bool>(floating);
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: functionTaking<bool>(floating != 0.0f);
+ // CHECK-FIXES: functionTaking<bool>(floating != 0.0F);
- double doubleFloating = 1.0f;
+ double doubleFloating = 1.0F;
functionTaking<bool>(doubleFloating);
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'double' -> 'bool'
// CHECK-FIXES: functionTaking<bool>(doubleFloating != 0.0);
@@ -211,10 +211,10 @@ void implicitConversionToBoolInSingleExpressions() {
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: implicit conversion 'int' -> 'bool'
// CHECK-FIXES: bool boolComingFromInt = integer != 0;
- float floating = 10.0f;
+ float floating = 10.0F;
bool boolComingFromFloat = floating;
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: bool boolComingFromFloat = floating != 0.0f;
+ // CHECK-FIXES: bool boolComingFromFloat = floating != 0.0F;
signed char character = 'a';
bool boolComingFromChar = character;
@@ -239,7 +239,7 @@ void implicitConversionToBoolInComplexExpressions() {
float floating = 0.2f;
bool boolComingFromFloating = floating - 0.3f || boolean;
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: bool boolComingFromFloating = ((floating - 0.3f) != 0.0f) || boolean;
+ // CHECK-FIXES: bool boolComingFromFloating = ((floating - 0.3f) != 0.0F) || boolean;
double doubleFloating = 0.3;
bool boolComingFromDoubleFloating = (doubleFloating - 0.4) && boolean;
@@ -253,10 +253,10 @@ void implicitConversionInNegationExpressions() {
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: implicit conversion 'int' -> 'bool'
// CHECK-FIXES: bool boolComingFromNegatedInt = integer == 0;
- float floating = 10.0f;
+ float floating = 10.0F;
bool boolComingFromNegatedFloat = ! floating;
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: bool boolComingFromNegatedFloat = floating == 0.0f;
+ // CHECK-FIXES: bool boolComingFromNegatedFloat = floating == 0.0F;
signed char character = 'a';
bool boolComingFromNegatedChar = (! character);
@@ -283,7 +283,7 @@ void implicitConversionToBoolInControlStatements() {
float floating = 0.3f;
while (floating) {}
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: while (floating != 0.0f) {}
+ // CHECK-FIXES: while (floating != 0.0F) {}
double doubleFloating = 0.4;
do {} while (doubleFloating);
@@ -295,7 +295,7 @@ bool implicitConversionToBoolInReturnValue() {
float floating = 1.0f;
return floating;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: return floating != 0.0f;
+ // CHECK-FIXES: return floating != 0.0F;
}
void implicitConversionToBoolFromLiterals() {
@@ -354,7 +354,7 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() {
functionTaking<bool>(-0.0f);
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'float' -> 'bool'
- // CHECK-FIXES: functionTaking<bool>((-0.0f) != 0.0f);
+ // CHECK-FIXES: functionTaking<bool>((-0.0f) != 0.0F);
functionTaking<bool>(-0.0);
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'double' -> 'bool'
More information about the cfe-commits
mailing list