Skip to content

Commit 1fb3f1e

Browse files
Fix #12446 FN cstyleCast with scope operator (#5989)
1 parent 685c7b5 commit 1fb3f1e

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

lib/checkother.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,26 @@ void CheckOther::warningOldStylePointerCast()
313313
tok = scope->bodyStart;
314314
for (; tok && tok != scope->bodyEnd; tok = tok->next()) {
315315
// Old style pointer casting..
316-
if (!Token::Match(tok, "( const|volatile| const|volatile|class|struct| %type% * *| *| const|&| ) (| %name%|%num%|%bool%|%char%|%str%|&"))
316+
if (tok->str() != "(")
317317
continue;
318+
const Token* castTok = tok->next();
319+
while (Token::Match(castTok, "const|volatile|class|struct|union|%type%|::")) {
320+
castTok = castTok->next();
321+
if (Token::simpleMatch(castTok, "<") && castTok->link())
322+
castTok = castTok->link()->next();
323+
}
324+
if (castTok == tok->next() || !Token::simpleMatch(castTok, "*"))
325+
continue;
326+
while (Token::Match(castTok, "*|const|&"))
327+
castTok = castTok->next();
328+
if (!Token::Match(castTok, ") (| %name%|%num%|%bool%|%char%|%str%|&"))
329+
continue;
330+
318331
if (Token::Match(tok->previous(), "%type%"))
319332
continue;
320333

321334
// skip first "const" in "const Type* const"
322-
while (Token::Match(tok->next(), "const|volatile|class|struct"))
335+
while (Token::Match(tok->next(), "const|volatile|class|struct|union"))
323336
tok = tok->next();
324337
const Token* typeTok = tok->next();
325338
// skip second "const" in "const Type* const"

test/testother.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,22 @@ class TestOther : public TestFixture {
18951895
ASSERT_EQUALS("[test.cpp:2]: (style) C-style pointer casting\n"
18961896
"[test.cpp:3]: (style) C-style pointer casting\n",
18971897
errout.str());
1898+
1899+
// #12446
1900+
checkOldStylePointerCast("namespace N { struct S {}; }\n"
1901+
"union U {\n"
1902+
" int i;\n"
1903+
" char c[4];\n"
1904+
"};\n"
1905+
"void f(void* p) {\n"
1906+
" auto ps = (N::S*)p;\n"
1907+
" auto pu = (union U*)p;\n"
1908+
" auto pv = (std::vector<int>*)(p);\n"
1909+
"}\n");
1910+
ASSERT_EQUALS("[test.cpp:7]: (style) C-style pointer casting\n"
1911+
"[test.cpp:8]: (style) C-style pointer casting\n"
1912+
"[test.cpp:9]: (style) C-style pointer casting\n",
1913+
errout.str());
18981914
}
18991915

19001916
#define checkInvalidPointerCast(...) checkInvalidPointerCast_(__FILE__, __LINE__, __VA_ARGS__)

0 commit comments

Comments
 (0)