-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Improved readability redundant casting with enums #111424
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 redundant casting with enums #111424
Conversation
310315a to
9c28fc5
Compare
|
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: Félix-Antoine Constantin (felix642) ChangesFixed false negatives with readability-redundant-casting when the underlying types are the same and the option IgnoreTypeAliases is set to true. Fixes #111137 Full diff: https://github.com/llvm/llvm-project/pull/111424.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
index b9ff0e81cbc522..ce0b32439c43a9 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
@@ -40,8 +40,15 @@ static bool areTypesEqual(QualType S, QualType D) {
static bool areTypesEqual(QualType TypeS, QualType TypeD,
bool IgnoreTypeAliases) {
- const QualType CTypeS = TypeS.getCanonicalType();
const QualType CTypeD = TypeD.getCanonicalType();
+
+ QualType CTypeS;
+ const auto *const EnumTypeS = TypeS->getAs<EnumType>();
+ if(EnumTypeS != nullptr && !EnumTypeS->getDecl()->isScoped())
+ CTypeS = EnumTypeS->getDecl()->getIntegerType();
+ else
+ CTypeS = TypeS.getCanonicalType();
+
if (CTypeS != CTypeD)
return false;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3e051c7db6adcc..32bae30821092f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -222,6 +222,12 @@ Changes in existing checks
by adding the option `UseUpperCaseLiteralSuffix` to select the
case of the literal suffix in fixes.
+- Improved :doc:`readability-redundant-casting
+ <clang-tidy/checks/readability/redundant-casting>` check by fixing
+ false negatives related to ``enum`` when setting ``IgnoreTypeAliases``
+ to true.
+
+
- Improved :doc:`readability-redundant-smartptr-get
<clang-tidy/checks/readability/redundant-smartptr-get>` check to
remove `->`, when redundant `get()` is removed.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
index 30cac6bd5cca06..ed49f32364cba8 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
@@ -221,3 +221,23 @@ void testRedundantDependentNTTPCasting() {
// CHECK-MESSAGES: :[[@LINE-4]]:25: note: source type originates from referencing this non-type template parameter
// CHECK-FIXES: {{^}} T a = V;
}
+
+enum E1 : char {};
+enum class E2 : char {};
+enum E3 {};
+
+void testEnum(E1 e1, E2 e2, E3 e3){
+ char a = static_cast<char>(e1);
+ // CHECK-MESSAGES-ALIASES: :[[@LINE-1]]:12: warning: redundant explicit casting to the same type 'char' as the sub-expression, remove this casting [readability-redundant-casting]
+ // CHECK-MESSAGES-ALIASES: :[[@LINE-3]]:18: note: source type originates from referencing this parameter
+ // CHECK-FIXES-ALIASES: {{^}} char a = e1;
+
+ unsigned int d = static_cast<unsigned int>(e3);
+ // CHECK-MESSAGES-ALIASES: :[[@LINE-1]]:20: warning: redundant explicit casting to the same type 'unsigned int' as the sub-expression, remove this casting [readability-redundant-casting]
+ // CHECK-MESSAGES-ALIASES: :[[@LINE-8]]:32: note: source type originates from referencing this parameter
+ // CHECK-FIXES-ALIASES: {{^}} unsigned int d = e3;
+
+ char b = static_cast<char>(e2);
+ char c = static_cast<char>(e3);
+ E1 e = static_cast<E1>('0');
+}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Fixed false negatives with readability-redundant-casting when the underlying types are the same and the option IgnoreTypeAliases is set to true. Fixes llvm#111137
9c28fc5 to
a786f62
Compare
To me,
sounds better as the commit description. The CI failure is probably because the implicit type for |
Code Review & Removed non-portable test
[clang-tidy] Improved readability redundant casting with enums Fixed false negatives with readability-redundant-casting when casting an enum to its underlying type and the option IgnoreTypeAliases is set to true. Fixes llvm#111137
I agree, thank you for the suggestion.
You are right. I could not find a way to make this test platform independent. I would either have to include <type_traits> or add preprocessor definitions to check for the OS (which might also break in the future). So I have decided to remove this test, since it didn't really add any value to the test suite. |
Removed excessive newline
5chmidti
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
+- some people may want to control this behavior and keep the explicit cast for enums, WDYT?
I can see why some people might want to explicitly cast the enum to its underlying type. |
That sounds good, or, in line with the other options: |
HerrCai0907
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really a false negative? In another word, does enum E1 : char {}; mean E1 is the type alias of char?
IMO is not. for me a new option is more suitable for this case.
I think it is a false negative in the sense that the cast is redundant, but it is a type conversion, so maybe the default should not warn on explicit casts from enums, and make the loss of explicit Information opt-in. |
|
@5chmidti and @HerrCai0907 I was revisiting this PR and I just remembered that my changes will only warn if
which, from my understanding, should cover enums. |
|
I am not the expect of cpp standard, in my opinion, enum is not a type alias. it is a new type. |
PiotrZSL
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please implement this change under separate option like: AllowEnumsBeRecognizedAs Aliases or something similar.
Implicit conversion between enums and underlying type is not always welcome by developers.
| enum class E2 : char {}; | ||
|
|
||
| void testEnum(E1 e1, E2 e2){ | ||
| char a = static_cast<char>(e1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That isn't a redundant cast. Such casts should be done in explicit way. In C++ is even better to use:
std::to_underlying since C++23 or std::underlying_type now.
|
Closing this PR since I no longer think this is an option that we need. |
Fixed false negatives with readability-redundant-casting when the underlying types are the same and the option IgnoreTypeAliases is set to true.
Fixes #111137