Files
TheRock/compiler/amd-llvm/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

289 lines
13 KiB
C++

// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing
// RUN: %check_clang_tidy -std=c++11,c++14,c++17 -check-suffix=,MACROS %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \
// RUN: -- -fno-delayed-template-parsing
// RUN: %check_clang_tidy -std=c++11,c++14,c++17 -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \
// RUN: -- -fno-delayed-template-parsing
// RUN: %check_clang_tidy -std=c++11,c++14,c++17 -check-suffix=IMPLICIT %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreImplicitCasts: true }}' \
// RUN: -- -fno-delayed-template-parsing
// RUN: %check_clang_tidy -std=c++20-or-later %s readability-redundant-casting %t -- \
// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \
// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \
// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=IMPLICIT %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreImplicitCasts: true }}' \
// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
#include <cstdint>
#include <cstddef>
struct A {};
struct B : A {};
A getA();
void testRedundantStaticCasting(A& value) {
A& a1 = static_cast<A&>(value);
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-3]]:36: note: source type originates from referencing this parameter
// CHECK-FIXES: A& a1 = value;
}
void testRedundantConstCasting1(A& value) {
A& a2 = const_cast<A&>(value);
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-3]]:36: note: source type originates from referencing this parameter
// CHECK-FIXES: A& a2 = value;
}
void testRedundantConstCasting2(const A& value) {
const A& a3 = const_cast<const A&>(value);
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant explicit casting to the same type 'const A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-3]]:42: note: source type originates from referencing this parameter
// CHECK-FIXES: const A& a3 = value;
}
void testRedundantReinterpretCasting(A& value) {
A& a4 = reinterpret_cast<A&>(value);
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-3]]:41: note: source type originates from referencing this parameter
// CHECK-FIXES: A& a4 = value;
}
void testRedundantCCasting(A& value) {
A& a5 = (A&)(value);
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-3]]:31: note: source type originates from referencing this parameter
// CHECK-FIXES: A& a5 = value;
}
void testDoubleCasting(A& value) {
A& a6 = static_cast<A&>(reinterpret_cast<A&>(value));
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-2]]:27: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-4]]:27: note: source type originates from referencing this parameter
// CHECK-FIXES: A& a6 = value;
}
void testDiffrentTypesCast(B& value) {
A& a7 = static_cast<A&>(value);
}
#ifdef CXX_20
void testParenListInitExpr(A value) {
B b = static_cast<B>(value);
}
#endif
void testCastingWithAuto() {
auto a = getA();
A& a8 = static_cast<A&>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-3]]:8: note: source type originates from referencing this variable
// CHECK-FIXES: A& a8 = a;
}
void testCastingWithConstAuto() {
const auto a = getA();
const A& a9 = static_cast<const A&>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant explicit casting to the same type 'const A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-3]]:14: note: source type originates from referencing this variable
// CHECK-FIXES: const A& a9 = a;
}
void testCastingWithAutoPtr(A& ptr) {
auto* a = &ptr;
A* a10 = static_cast<A*>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant explicit casting to the same type 'A *' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-3]]:9: note: source type originates from referencing this variable
// CHECK-FIXES: A* a10 = a;
}
template<typename T>
void testRedundantTemplateCasting(T& value) {
A& a = static_cast<A&>(value);
T& t = static_cast<T&>(value);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant explicit casting to the same type 'T' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-4]]:38: note: source type originates from referencing this parameter
// CHECK-FIXES: T& t = value;
}
void testTemplate() {
A value;
testRedundantTemplateCasting(value);
}
void testValidRefConstCast() {
const auto a = getA();
A& a11 = const_cast<A&>(a);
}
void testValidPtrConstCast(const A* ptr) {
A* a12 = const_cast<A*>(ptr);
}
#define CAST(X) static_cast<int>(X)
void testMacroCasting(int value) {
int a = CAST(value);
// CHECK-MESSAGES-MACROS: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
}
#define PTR_NAME name
void testMacroCasting(A* PTR_NAME) {
A* a13 = static_cast<A*>(PTR_NAME);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant explicit casting to the same type 'A *' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: A* a13 = PTR_NAME;
}
struct CastBool {
operator bool() const {
return true;
}
};
void testUserOperatorCast(const CastBool& value) {
bool b = static_cast<bool>(value);
}
using TypeA = A;
void testTypedefCast(A& value) {
TypeA& a = static_cast<TypeA&>(value);
// CHECK-MESSAGES-ALIASES: :[[@LINE-1]]:14: warning: redundant explicit casting to the same type 'TypeA' (aka 'A') as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES-ALIASES: TypeA& a = value;
}
void testTypedefCast2(TypeA& value) {
A& a = static_cast<A&>(value);
// CHECK-MESSAGES-ALIASES: :[[@LINE-1]]:10: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES-ALIASES: A& a = value;
}
void testFunctionalCastWithPrimitive(int a) {
int b = int(a);
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: int b = a;
}
void testFunctionalCastWithInitExpr(unsigned a) {
unsigned b = ~unsigned{!a};
unsigned c = unsigned{0};
}
void testBinaryOperatorRedundantCasting() {
const auto diff_types_operands1 { static_cast<float>(1.0f + 1) };
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: redundant explicit casting to the same type 'float' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: const auto diff_types_operands1 { (1.0f + 1) };
// CHECK-FIXES-IMPLICIT: const auto diff_types_operands1 { static_cast<float>(1.0f + 1) };
const auto diff_types_operands2 { static_cast<float>(2 + 3.0f) };
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: redundant explicit casting to the same type 'float' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: const auto diff_types_operands2 { (2 + 3.0f) };
// CHECK-FIXES-IMPLICIT: const auto diff_types_operands2 { static_cast<float>(2 + 3.0f) };
const auto diff_types_operands3 { static_cast<int>(1 + static_cast<uint8_t>(1)) };
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: const auto diff_types_operands3 { (1 + static_cast<uint8_t>(1)) };
// CHECK-FIXES-IMPLICIT: const auto diff_types_operands3 { static_cast<int>(1 + static_cast<uint8_t>(1)) };
const auto diff_types_operands4 {
static_cast<size_t>(static_cast<size_t>(3) + 2)
};
// CHECK-MESSAGES: :[[@LINE-2]]:5: warning: redundant explicit casting to the same type 'size_t' (aka 'unsigned {{(int|long( long)?)}}') as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: (static_cast<size_t>(3) + 2)
// CHECK-FIXES-IMPLICIT: static_cast<size_t>(static_cast<size_t>(3) + 2)
const auto diff_types_operands5 { unsigned(7 + unsigned(4)) };
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: redundant explicit casting to the same type 'unsigned int' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: const auto diff_types_operands5 { (7 + unsigned(4)) };
// CHECK-FIXES-IMPLICIT: const auto diff_types_operands5 { unsigned(7 + unsigned(4)) };
// casting isn't redundant here
const auto diff_types_operands6 { (int)(-7 + unsigned(4)) };
}
void testBinaryOperator(char c) {
int a = int(c - 'C');
}
struct BIT {
bool b:1;
};
template<typename ...Args>
void make(Args&& ...);
void testBinaryOperator(BIT b) {
make((bool)b.b);
}
struct Class {
using Iterator = const char*;
Iterator begin() {
return static_cast<Iterator>(first());
// CHECK-MESSAGES-ALIASES: :[[@LINE-1]]:12: warning: redundant explicit casting to the same type 'Iterator' (aka 'const char *') as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES-ALIASES: :[[@LINE+4]]:15: note: source type originates from the invocation of this method
// CHECK-FIXES-ALIASES: return first();
}
const char* first();
};
void testAddOperation(int aa, int bb) {
int c = static_cast<int>(aa + bb) * aa;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: int c = (aa + bb) * aa;
}
void testAddOperationWithParen(int a, int b) {
int c = static_cast<int>((a+b))*a;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: int c = (a+b)*a;
}
void testRValueCast(int&& a) {
int&& b = static_cast<int&&>(a);
int&& c = static_cast<int&&>(10);
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: int&& c = 10;
}
template <int V>
void testRedundantNTTPCasting() {
int a = static_cast<int>(V);
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-4]]:15: note: source type originates from referencing this non-type template parameter
// CHECK-FIXES: int a = V;
}
template <typename T, T V>
void testValidNTTPCasting() {
int a = static_cast<int>(V);
}
template <typename T, T V>
void testRedundantDependentNTTPCasting() {
T a = static_cast<T>(V);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant explicit casting to the same type 'T' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-MESSAGES: :[[@LINE-4]]:25: note: source type originates from referencing this non-type template parameter
// CHECK-FIXES: T a = V;
}
namespace gh170476 {
int f(void);
int g1() {
int (*fp)() = (int(*)(void))&f;
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant explicit casting to the same type 'int (*)()' as the sub-expression, remove this casting [readability-redundant-casting]
// CHECK-FIXES: int (*fp)() = (&f);
return fp();
}
} // namespace gh170476