diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 12be4b448..2906ca191 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -21,6 +21,7 @@ #include "checkother.h" #include "mathlib.h" #include "symboldatabase.h" +#include "templatesimplifier.h" #include // fabs() #include @@ -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(); - } } } diff --git a/test/testother.cpp b/test/testother.cpp index 6c67e2668..5670a8493 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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::b;\n" + "}", NULL, false, false, false, false); + ASSERT_EQUALS("", errout.str()); } };