Skip to content

Commit a786f62

Browse files
committed
[clang-tidy] Improved readability redundant casting with enums
Fixed false negatives with readability-redundant-casting when the underlying types are the same and the option IgnoreTypeAliases is set to true. Fixes #111137
1 parent 971b579 commit a786f62

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ static bool areTypesEqual(QualType S, QualType D) {
4040

4141
static bool areTypesEqual(QualType TypeS, QualType TypeD,
4242
bool IgnoreTypeAliases) {
43-
const QualType CTypeS = TypeS.getCanonicalType();
4443
const QualType CTypeD = TypeD.getCanonicalType();
44+
45+
QualType CTypeS;
46+
const auto *const EnumTypeS = TypeS->getAs<EnumType>();
47+
if (EnumTypeS != nullptr && !EnumTypeS->getDecl()->isScoped())
48+
CTypeS = EnumTypeS->getDecl()->getIntegerType();
49+
else
50+
CTypeS = TypeS.getCanonicalType();
51+
4552
if (CTypeS != CTypeD)
4653
return false;
4754

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ Changes in existing checks
222222
by adding the option `UseUpperCaseLiteralSuffix` to select the
223223
case of the literal suffix in fixes.
224224

225+
- Improved :doc:`readability-redundant-casting
226+
<clang-tidy/checks/readability/redundant-casting>` check by fixing
227+
false negatives related to ``enum`` when setting ``IgnoreTypeAliases``
228+
to true.
229+
230+
225231
- Improved :doc:`readability-redundant-smartptr-get
226232
<clang-tidy/checks/readability/redundant-smartptr-get>` check to
227233
remove `->`, when redundant `get()` is removed.

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,23 @@ void testRedundantDependentNTTPCasting() {
221221
// CHECK-MESSAGES: :[[@LINE-4]]:25: note: source type originates from referencing this non-type template parameter
222222
// CHECK-FIXES: {{^}} T a = V;
223223
}
224+
225+
enum E1 : char {};
226+
enum class E2 : char {};
227+
enum E3 {};
228+
229+
void testEnum(E1 e1, E2 e2, E3 e3){
230+
char a = static_cast<char>(e1);
231+
// 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]
232+
// CHECK-MESSAGES-ALIASES: :[[@LINE-3]]:18: note: source type originates from referencing this parameter
233+
// CHECK-FIXES-ALIASES: {{^}} char a = e1;
234+
235+
unsigned int d = static_cast<unsigned int>(e3);
236+
// 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]
237+
// CHECK-MESSAGES-ALIASES: :[[@LINE-8]]:32: note: source type originates from referencing this parameter
238+
// CHECK-FIXES-ALIASES: {{^}} unsigned int d = e3;
239+
240+
char b = static_cast<char>(e2);
241+
char c = static_cast<char>(e3);
242+
E1 e = static_cast<E1>('0');
243+
}

0 commit comments

Comments
 (0)