Commit 4b5315bd authored by Joerg Bornemann's avatar Joerg Bornemann

CppEditor: fix "Convert to Stack Variable" for empty initializer lists

The "Convert to Stack Variable" quick fix generated invalid code for
the following snippet: QString s = new QString();

Task-number: QTCREATORBUG-14279
Change-Id: I1f5e33d2089bc2d5713eae1e9f4dc5986523112c
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent 465cf130
......@@ -1559,6 +1559,19 @@ void CppEditorPlugin::test_quickfix_data()
" str.clear();\n"
"}\n");
QTest::newRow("ConvertFromPointer_withEmptyInitializer")
<< CppQuickFixFactoryPtr(new ConvertFromAndToPointer)
<< _("void foo() {\n"
" QString *@str = new QString();\n"
" if (!str->isEmpty())\n"
" str->clear();\n"
"}\n")
<< _("void foo() {\n"
" QString str;\n"
" if (!str.isEmpty())\n"
" str.clear();\n"
"}\n");
QTest::newRow("ConvertFromPointer_structWithPointer")
<< CppQuickFixFactoryPtr(new ConvertFromAndToPointer)
<< _("struct Bar{ QString *str; };\n"
......
......@@ -4059,19 +4059,20 @@ private:
void removeNewExpression(ChangeSet &changes, NewExpressionAST *newExprAST) const
{
if (newExprAST->new_initializer) {
ExpressionListParenAST *exprlist = newExprAST->new_initializer
? newExprAST->new_initializer->asExpressionListParen()
: 0;
if (exprlist && exprlist->expression_list) {
// remove 'new' keyword and type before initializer
changes.remove(m_file->startOf(newExprAST->new_token),
m_file->startOf(newExprAST->new_initializer));
// remove parenthesis around initializer
if (ExpressionListParenAST *exprlist
= newExprAST->new_initializer->asExpressionListParen()) {
int pos = m_file->startOf(exprlist->lparen_token);
changes.remove(pos, pos + 1);
pos = m_file->startOf(exprlist->rparen_token);
changes.remove(pos, pos + 1);
}
int pos = m_file->startOf(exprlist->lparen_token);
changes.remove(pos, pos + 1);
pos = m_file->startOf(exprlist->rparen_token);
changes.remove(pos, pos + 1);
} else {
// remove the whole new expression
changes.remove(m_file->endOf(m_identifierAST->firstToken()),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment