Fixed #4862 (False positive: Comma is used in return statement (template))
This commit is contained in:
parent
c5d33e163c
commit
2fb8133e90
|
@ -21,6 +21,7 @@
|
|||
#include "checkother.h"
|
||||
#include "mathlib.h"
|
||||
#include "symboldatabase.h"
|
||||
#include "templatesimplifier.h"
|
||||
|
||||
#include <cmath> // fabs()
|
||||
#include <stack>
|
||||
|
@ -1890,11 +1891,25 @@ void CheckOther::checkCommaSeparatedReturn()
|
|||
if (tok->str() == "(")
|
||||
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);
|
||||
|
||||
tok=tok->next();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6238,15 +6238,35 @@ private:
|
|||
void checkCommaSeparatedReturn() {
|
||||
check("int fun(int a) {\n"
|
||||
" if (a < 0)\n"
|
||||
" return a++ , 0;\n"
|
||||
" return a++,\n"
|
||||
" do_something();\n"
|
||||
"}", 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());
|
||||
|
||||
check("int fun(int a) {\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);
|
||||
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());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue