parent
cae27c5ec7
commit
77bfec4317
|
@ -2543,11 +2543,12 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
|
||||||
tok2 = skipRedundantPtrOp(tok2, tok2->astParent());
|
tok2 = skipRedundantPtrOp(tok2, tok2->astParent());
|
||||||
|
|
||||||
if (tok2->astParent() && tok2->astParent()->isAssignmentOp()) {
|
if (tok2->astParent() && tok2->astParent()->isAssignmentOp()) {
|
||||||
if (tok2 == tok2->astParent()->astOperand1())
|
if ((indirect == 0 || tok2 != tok) && tok2 == tok2->astParent()->astOperand1())
|
||||||
return true;
|
return true;
|
||||||
// Check if assigning to a non-const lvalue
|
// Check if assigning to a non-const lvalue
|
||||||
const Variable * var = getLHSVariable(tok2->astParent());
|
const Variable * var = getLHSVariable(tok2->astParent());
|
||||||
if (var && var->isReference() && !var->isConst() && var->nameToken() && var->nameToken()->next() == tok2->astParent()) {
|
if (var && var->isReference() && !var->isConst() &&
|
||||||
|
((var->nameToken() && var->nameToken()->next() == tok2->astParent()) || var->isPointer())) {
|
||||||
if (!var->isLocal() || isVariableChanged(var, settings, cpp, depth - 1))
|
if (!var->isLocal() || isVariableChanged(var, settings, cpp, depth - 1))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1705,6 +1705,8 @@ void CheckOther::checkConstPointer()
|
||||||
continue;
|
continue;
|
||||||
if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef()))
|
if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef()))
|
||||||
continue;
|
continue;
|
||||||
|
if (p->typeStartToken() && !p->typeStartToken()->originalName().empty())
|
||||||
|
continue;
|
||||||
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr);
|
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -992,7 +992,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
|
||||||
|
|
||||||
mData->enumValue = 0;
|
mData->enumValue = 0;
|
||||||
Token *enumtok = addtoken(tokenList, "enum");
|
Token *enumtok = addtoken(tokenList, "enum");
|
||||||
Token *nametok = nullptr;
|
const Token *nametok = nullptr;
|
||||||
{
|
{
|
||||||
int nameIndex = mExtTokens.size() - 1;
|
int nameIndex = mExtTokens.size() - 1;
|
||||||
while (nameIndex > colIndex && mExtTokens[nameIndex][0] == '\'')
|
while (nameIndex > colIndex && mExtTokens[nameIndex][0] == '\'')
|
||||||
|
|
|
@ -1914,6 +1914,7 @@ void TokenList::simplifyPlatformTypes()
|
||||||
tok = tok->previous();
|
tok = tok->previous();
|
||||||
tok->deleteThis();
|
tok->deleteThis();
|
||||||
}
|
}
|
||||||
|
tok->originalName(tok->str());
|
||||||
Token *typeToken;
|
Token *typeToken;
|
||||||
if (platformtype->mConstPtr) {
|
if (platformtype->mConstPtr) {
|
||||||
tok->str("const");
|
tok->str("const");
|
||||||
|
@ -1930,7 +1931,6 @@ void TokenList::simplifyPlatformTypes()
|
||||||
tok->insertToken("*");
|
tok->insertToken("*");
|
||||||
tok->insertToken("*");
|
tok->insertToken("*");
|
||||||
} else {
|
} else {
|
||||||
tok->originalName(tok->str());
|
|
||||||
tok->str(platformtype->mType);
|
tok->str(platformtype->mType);
|
||||||
typeToken = tok;
|
typeToken = tok;
|
||||||
}
|
}
|
||||||
|
|
|
@ -269,7 +269,7 @@ void g_new_if_test()
|
||||||
int b;
|
int b;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct a * pNew3;
|
const struct a * pNew3;
|
||||||
if (pNew3 = g_new(struct a, 6)) {
|
if (pNew3 = g_new(struct a, 6)) {
|
||||||
printf("%p", pNew3);
|
printf("%p", pNew3);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8946,14 +8946,14 @@ private:
|
||||||
" state_t *x = NULL;\n"
|
" state_t *x = NULL;\n"
|
||||||
" x = dostuff();\n"
|
" x = dostuff();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", errout.str());
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" state_t *x;\n"
|
" state_t *x;\n"
|
||||||
" x = NULL;\n"
|
" x = NULL;\n"
|
||||||
" x = dostuff();\n"
|
" x = dostuff();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", errout.str());
|
||||||
|
|
||||||
check("int foo() {\n" // #4420
|
check("int foo() {\n" // #4420
|
||||||
" int x;\n"
|
" int x;\n"
|
||||||
|
@ -9086,7 +9086,8 @@ private:
|
||||||
" barney(x);\n"
|
" barney(x);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("test.cpp:2:style:The scope of the variable 'p' can be reduced.\n",
|
ASSERT_EQUALS("test.cpp:2:style:The scope of the variable 'p' can be reduced.\n"
|
||||||
|
"test.cpp:2:style:Variable 'p' can be declared as pointer to const\n",
|
||||||
errout.str());
|
errout.str());
|
||||||
|
|
||||||
check("void foo() {\n"
|
check("void foo() {\n"
|
||||||
|
@ -9178,14 +9179,14 @@ private:
|
||||||
" a = (void*)0;\n"
|
" a = (void*)0;\n"
|
||||||
" a = p;\n"
|
" a = p;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", errout.str());
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" void* a;\n"
|
" void* a;\n"
|
||||||
" a = (void*)0U;\n"
|
" a = (void*)0U;\n"
|
||||||
" a = p;\n"
|
" a = p;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void redundantVarAssignment_struct() {
|
void redundantVarAssignment_struct() {
|
||||||
|
@ -9442,7 +9443,7 @@ private:
|
||||||
" int *p = NULL;\n"
|
" int *p = NULL;\n"
|
||||||
" p = dostuff();\n"
|
" p = dostuff();\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("test.cpp:2:style:Variable 'p' can be declared as pointer to const\n", errout.str());
|
||||||
|
|
||||||
// "trivial" initialization => do not warn
|
// "trivial" initialization => do not warn
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
|
|
Loading…
Reference in New Issue