Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(e1);
Copy link
Member

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.

// 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<char>(e2);
E1 e = static_cast<E1>('0');
}
Loading