This commit is contained in:
parent
5d25050b06
commit
cd7532df21
|
@ -301,11 +301,11 @@ void CheckOther::warningOldStylePointerCast()
|
|||
tok = scope->bodyStart;
|
||||
for (; tok && tok != scope->bodyEnd; tok = tok->next()) {
|
||||
// Old style pointer casting..
|
||||
if (!Token::Match(tok, "( const|volatile| const|volatile| %type% * const| ) (| %name%|%num%|%bool%|%char%|%str%"))
|
||||
if (!Token::Match(tok, "( const|volatile| const|volatile|class|struct| %type% * const|&| ) (| %name%|%num%|%bool%|%char%|%str%"))
|
||||
continue;
|
||||
|
||||
// skip first "const" in "const Type* const"
|
||||
while (Token::Match(tok->next(), "const|volatile"))
|
||||
while (Token::Match(tok->next(), "const|volatile|class|struct"))
|
||||
tok = tok->next();
|
||||
const Token* typeTok = tok->next();
|
||||
// skip second "const" in "const Type* const"
|
||||
|
@ -316,8 +316,7 @@ void CheckOther::warningOldStylePointerCast()
|
|||
if (p->hasKnownIntValue() && p->values().front().intvalue==0) // Casting nullpointers is safe
|
||||
continue;
|
||||
|
||||
// Is "type" a class?
|
||||
if (typeTok->type())
|
||||
if (typeTok->tokType() == Token::eType || typeTok->tokType() == Token::eName)
|
||||
cstyleCastError(tok);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -586,7 +586,7 @@ bool Token::simpleMatch(const Token *tok, const char pattern[], size_t pattern_l
|
|||
return false; // shortcut
|
||||
const char *current = pattern;
|
||||
const char *end = pattern + pattern_len;
|
||||
const char *next = (const char*)std::memchr(pattern, ' ', pattern_len);
|
||||
const char *next = static_cast<const char*>(std::memchr(pattern, ' ', pattern_len));
|
||||
if (!next)
|
||||
next = end;
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ namespace ExampleNamespace {
|
|||
|
||||
TEST(ASSERT, ASSERT)
|
||||
{
|
||||
int *a = (int*)calloc(10,sizeof(int));
|
||||
int *a = (int*)calloc(10,sizeof(int)); // cppcheck-suppress cstyleCast
|
||||
ASSERT_TRUE(a != nullptr);
|
||||
|
||||
a[0] = 10;
|
||||
|
|
|
@ -27,7 +27,7 @@ void validCode(char* argStr)
|
|||
cvStr += " World";
|
||||
std::cout << cvStr;
|
||||
|
||||
char * pBuf = (char *)cv::fastMalloc(20);
|
||||
char * pBuf = (char *)cv::fastMalloc(20); // cppcheck-suppress cstyleCast
|
||||
cv::fastFree(pBuf);
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@ void ignoredReturnValue()
|
|||
|
||||
void memleak()
|
||||
{
|
||||
char * pBuf = (char *)cv::fastMalloc(1000);
|
||||
char * pBuf = (char *)cv::fastMalloc(1000); // cppcheck-suppress cstyleCast
|
||||
std::cout << pBuf;
|
||||
// cppcheck-suppress memleak
|
||||
}
|
||||
|
|
|
@ -2029,7 +2029,7 @@ void uninitvar_longjmp(void)
|
|||
void uninitvar_malloc(void)
|
||||
{
|
||||
size_t size;
|
||||
// cppcheck-suppress uninitvar
|
||||
// cppcheck-suppress [uninitvar, cstyleCast]
|
||||
int *p = (int*)std::malloc(size);
|
||||
free(p);
|
||||
}
|
||||
|
@ -2261,7 +2261,7 @@ void uninivar_bsearch(void)
|
|||
void* base;
|
||||
size_t num;
|
||||
size_t size;
|
||||
// cppcheck-suppress uninitvar
|
||||
// cppcheck-suppress [uninitvar, cstyleCast]
|
||||
(void)std::bsearch(key,base,num,size,(int (*)(const void*,const void*))strcmp);
|
||||
}
|
||||
|
||||
|
@ -2271,11 +2271,11 @@ void minsize_bsearch(const void* key, const void* base,
|
|||
{
|
||||
int Base[3] = {42, 43, 44};
|
||||
|
||||
(void)std::bsearch(key,Base,2,size,(int (*)(const void*,const void*))strcmp);
|
||||
(void)std::bsearch(key,Base,3,size,(int (*)(const void*,const void*))strcmp);
|
||||
(void)std::bsearch(key,Base,4,size,(int (*)(const void*,const void*))strcmp);
|
||||
(void)std::bsearch(key,Base,2,size,(int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast
|
||||
(void)std::bsearch(key,Base,3,size,(int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast
|
||||
(void)std::bsearch(key,Base,4,size,(int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast
|
||||
|
||||
(void)std::bsearch(key,base,2,size,(int (*)(const void*,const void*))strcmp);
|
||||
(void)std::bsearch(key,base,2,size,(int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast
|
||||
}
|
||||
|
||||
void uninitvar_qsort(void)
|
||||
|
@ -2284,7 +2284,7 @@ void uninitvar_qsort(void)
|
|||
size_t n;
|
||||
size_t size;
|
||||
// cppcheck-suppress uninitvar
|
||||
(void)std::qsort(base,n,size, (int (*)(const void*,const void*))strcmp);
|
||||
(void)std::qsort(base,n,size, (int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast
|
||||
}
|
||||
|
||||
void uninitvar_putc(void)
|
||||
|
|
|
@ -513,7 +513,7 @@ void memleak_HeapAlloc()
|
|||
void memleak_LocalAlloc()
|
||||
{
|
||||
LPTSTR pszBuf;
|
||||
// cppcheck-suppress LocalAllocCalled
|
||||
// cppcheck-suppress [LocalAllocCalled, cstyleCast]
|
||||
pszBuf = (LPTSTR)LocalAlloc(LPTR, MAX_PATH*sizeof(TCHAR));
|
||||
(void)LocalSize(pszBuf);
|
||||
(void)LocalFlags(pszBuf);
|
||||
|
|
|
@ -1460,6 +1460,50 @@ private:
|
|||
" v.push_back((Base*)new Derived);\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:5]: (style) C-style pointer casting\n", errout.str());
|
||||
|
||||
// #7709
|
||||
checkOldStylePointerCast("typedef struct S S;\n"
|
||||
"typedef struct S SS;\n"
|
||||
"typedef class C C;\n"
|
||||
"typedef long LONG;\n"
|
||||
"typedef long* LONGP;\n"
|
||||
"struct T {};\n"
|
||||
"typedef struct T TT;\n"
|
||||
"typedef struct T2 {} TT2;\n"
|
||||
"void f(int* i) {\n"
|
||||
" S* s = (S*)i;\n"
|
||||
" SS* ss = (SS*)i;\n"
|
||||
" struct S2* s2 = (struct S2*)i;\n"
|
||||
" C* c = (C*)i;\n"
|
||||
" class C2* c2 = (class C2*)i;\n"
|
||||
" long* l = (long*)i;\n"
|
||||
" LONG* l2 = (LONG*)i;\n"
|
||||
" LONGP l3 = (LONGP)i;\n"
|
||||
" TT* tt = (TT*)i;\n"
|
||||
" TT2* tt2 = (TT2*)i;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:10]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:11]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:12]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:13]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:14]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:15]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:16]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:17]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:18]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:19]: (style) C-style pointer casting\n",
|
||||
errout.str());
|
||||
|
||||
// #8649
|
||||
checkOldStylePointerCast("struct S {};\n"
|
||||
"void g(S*& s);\n"
|
||||
"void f(int i) {\n"
|
||||
" g((S*&)i);\n"
|
||||
" S*& r = (S*&)i;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n"
|
||||
"[test.cpp:5]: (style) C-style pointer casting\n",
|
||||
errout.str());
|
||||
}
|
||||
|
||||
#define checkInvalidPointerCast(...) checkInvalidPointerCast_(__FILE__, __LINE__, __VA_ARGS__)
|
||||
|
@ -7623,7 +7667,7 @@ private:
|
|||
" *reg = 12;\n"
|
||||
" *reg = 34;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
ASSERT_EQUALS("test.cpp:2:style:C-style pointer casting\n", errout.str());
|
||||
}
|
||||
|
||||
void redundantVarAssignment_trivial() {
|
||||
|
|
Loading…
Reference in New Issue