diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp index b9ff0e81cbc52..612a5a9159335 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(); + 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 3e051c7db6adc..489ade133cf79 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -222,6 +222,11 @@ Changes in existing checks by adding the option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in fixes. +- Improved :doc:`readability-redundant-casting + ` check by fixing + false negatives related to ``enum`` when setting `IgnoreTypeAliases` + to `true`. + - Improved :doc:`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 30cac6bd5cca0..52fa57b55459c 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,16 @@ 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 {}; + +void testEnum(E1 e1, E2 e2){ + char a = static_cast(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; + + char b = static_cast(e2); + E1 e = static_cast('0'); +}