Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
warn about suspicious casts of incomplete classes, do not warn about …
…cast from derived pointer to base pointer
  • Loading branch information
danmar committed May 21, 2025
commit e37d52fff217f5d2618de393f57df30acf17632b
12 changes: 11 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,19 @@ void CheckOther::warningOldStylePointerCast()
continue;
if (!tok->valueType() || !from->valueType())
continue;
if (tok->valueType()->type == from->valueType()->type &&
if (tok->valueType()->typeScope != nullptr &&
tok->valueType()->typeScope == from->valueType()->typeScope)
continue;
if (tok->valueType()->type == from->valueType()->type &&
tok->valueType()->isPrimitive())
continue;
// cast from derived object to base object is safe..
if (tok->valueType()->typeScope && from->valueType()->typeScope) {
const Type* fromType = from->valueType()->typeScope->definedType;
const Type* toType = tok->valueType()->typeScope->definedType;
if (fromType && toType && fromType->isDerivedFrom(toType->name()))
continue;
}
const bool refcast = (tok->valueType()->reference != Reference::None);
if (!refcast && tok->valueType()->pointer == 0)
continue;
Expand Down
46 changes: 30 additions & 16 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1912,37 +1912,51 @@ class TestOther : public TestFixture {
}

void oldStylePointerCast() {
checkOldStylePointerCast("class Base{};\n"
"class Derived: public Base {};\n"
"void foo(Base* base)\n"
"{\n"
" Derived * d = (Derived *) base;\n"
"}");
ASSERT_EQUALS("[test.cpp:5:19]: (style) C-style pointer casting [cstyleCast]\n", errout_str());

checkOldStylePointerCast("class Base{};\n"
"class Derived: public Base {};\n"
"void foo(Derived* derived)\n"
"{\n"
" Base * b = (Base *) derived;\n"
"}");
ASSERT_EQUALS("[test.cpp:5:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
ASSERT_EQUALS("", errout_str()); // <- cast from derived to base is safe

checkOldStylePointerCast("void foo(Base* base)\n"
"{\n"
" Derived * d = (Derived *) base;\n"
"}");
ASSERT_EQUALS("[test.cpp:3:19]: (style) C-style pointer casting [cstyleCast]\n", errout_str());

checkOldStylePointerCast("class Base{};\n"
"class Derived: public Base {};\n"
"void foo(Derived* derived)\n"
"void foo(Base* base)\n"
"{\n"
" Base * b = (const Base *) derived;\n"
" Derived * d = (const Derived *) base;\n"
"}");
ASSERT_EQUALS("[test.cpp:5:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
ASSERT_EQUALS("[test.cpp:5:19]: (style) C-style pointer casting [cstyleCast]\n", errout_str());

checkOldStylePointerCast("class Base{};\n"
"class Derived: public Base {};\n"
"void foo()\n"
"{\n"
" Base * b = (Base *) ( new Derived() );\n"
" Derived * d = (const Derived *) ( new Base() );\n"
"}");
ASSERT_EQUALS("[test.cpp:5:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
ASSERT_EQUALS("[test.cpp:5:19]: (style) C-style pointer casting [cstyleCast]\n", errout_str());

checkOldStylePointerCast("class Base{};\n"
"class Derived: public Base {};\n"
"void foo()\n"
"{\n"
" Base * b = (Base *) new Derived();\n"
" Derived * d = (const Derived *) new Base();\n"
"}");
ASSERT_EQUALS("[test.cpp:5:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
ASSERT_EQUALS("[test.cpp:5:19]: (style) C-style pointer casting [cstyleCast]\n", errout_str());

checkOldStylePointerCast("class Base{};\n"
"void foo()\n"
Expand Down Expand Up @@ -2071,12 +2085,12 @@ class TestOther : public TestFixture {
// #5210
checkOldStylePointerCast("class Base {};\n"
"class Derived: public Base {};\n"
"void f(Derived** d1, Derived*** d2) {\n"
" Base** p1 = (Base**)d1;\n"
" Base*** p2 = (Base***)d2;\n"
"void f(Base** b1, Base*** b2) {\n"
" Derived** p1 = (Derived**)b1;\n"
" Derived*** p2 = (Derived***)b2;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:17]: (style) C-style pointer casting [cstyleCast]\n"
"[test.cpp:5:18]: (style) C-style pointer casting [cstyleCast]\n",
ASSERT_EQUALS("[test.cpp:4:20]: (style) C-style pointer casting [cstyleCast]\n"
"[test.cpp:5:21]: (style) C-style pointer casting [cstyleCast]\n",
errout_str());

// #12446
Expand All @@ -2095,10 +2109,10 @@ class TestOther : public TestFixture {
// #12447
checkOldStylePointerCast("class Base {};\n"
"class Derived: public Base {};\n"
"void f(const Derived& derived) {\n"
" Base& b = (Base&)derived;\n"
"void f(const Base& base) {\n"
" d = (const Derived&)base;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:13]: (style) C-style reference casting [cstyleCast]\n", errout_str());
ASSERT_EQUALS("[test.cpp:4:7]: (style) C-style reference casting [cstyleCast]\n", errout_str());

// #11430
checkOldStylePointerCast("struct B {\n"
Expand Down