Fixed #4862 (False positive: Comma is used in return statement (template))

This commit is contained in:
Daniel Marjamäki 2013-06-18 00:13:45 +02:00
parent c5d33e163c
commit 2fb8133e90
2 changed files with 39 additions and 4 deletions

View File

@ -21,6 +21,7 @@
#include "checkother.h" #include "checkother.h"
#include "mathlib.h" #include "mathlib.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "templatesimplifier.h"
#include <cmath> // fabs() #include <cmath> // fabs()
#include <stack> #include <stack>
@ -1890,11 +1891,25 @@ void CheckOther::checkCommaSeparatedReturn()
if (tok->str() == "(") if (tok->str() == "(")
tok=tok->link(); tok=tok->link();
if (!tok->isExpandedMacro() && tok->str() == ",") // Skip template parameters
if (tok->str() == "<" && TemplateSimplifier::templateParameters(tok) > 0U) {
unsigned int level = 1U;
while (level > 0U && NULL != (tok = tok->next())) {
if (tok->str() == "<")
level++;
else if (tok->str() == ">")
level--;
else if (tok->str() == ">>")
level = level - ((level > 1U) ? 2 : 1);
else if (tok->str() == ";")
break;
}
}
if (!tok->isExpandedMacro() && tok->str() == "," && tok->linenr() != tok->next()->linenr())
commaSeparatedReturnError(tok); commaSeparatedReturnError(tok);
tok=tok->next(); tok=tok->next();
} }
} }
} }

View File

@ -6238,15 +6238,35 @@ private:
void checkCommaSeparatedReturn() { void checkCommaSeparatedReturn() {
check("int fun(int a) {\n" check("int fun(int a) {\n"
" if (a < 0)\n" " if (a < 0)\n"
" return a++ , 0;\n" " return a++,\n"
" do_something();\n"
"}", NULL, false, false, false, false); "}", NULL, false, false, false, false);
ASSERT_EQUALS("[test.cpp:3]: (style) Comma is used in return statement. The comma can easily be misread as a ';'.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (style) Comma is used in return statement. The comma can easily be misread as a ';'.\n", errout.str());
check("int fun(int a) {\n" check("int fun(int a) {\n"
" if (a < 0)\n" " if (a < 0)\n"
" return a=a+5,1; \n" " return a++, do_something();\n"
"}", NULL, false, false, false, false);
ASSERT_EQUALS("", errout.str());
check("int fun(int a) {\n"
" if (a < 0)\n"
" return a+5,\n"
" do_something();\n"
"}", NULL, false, false, false, false); "}", NULL, false, false, false, false);
ASSERT_EQUALS("[test.cpp:3]: (style) Comma is used in return statement. The comma can easily be misread as a ';'.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (style) Comma is used in return statement. The comma can easily be misread as a ';'.\n", errout.str());
check("int fun(int a) {\n"
" if (a < 0)\n"
" return a+5, do_something();\n"
"}", NULL, false, false, false, false);
ASSERT_EQUALS("", errout.str());
check("int fun(int a) {\n"
" if (a < 0)\n"
" return a<int,\nint>::b;\n"
"}", NULL, false, false, false, false);
ASSERT_EQUALS("", errout.str());
} }
}; };