From 1df8f3842313f47ee4af8bc9f435b51b8de7d81e Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Mon, 5 Oct 2009 11:59:28 +0300 Subject: [PATCH] Fix #793 (Improve passedByValue check) http://sourceforge.net/apps/trac/cppcheck/ticket/793 --- src/checkother.cpp | 10 ++++++++++ test/testother.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/checkother.cpp b/src/checkother.cpp index ab4b35ecd..fde9847a1 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -567,6 +567,16 @@ void CheckOther::checkConstantFunctionParameter() passedByValueError(tok, tok->strAt(5)); } + else if (Token::Match(tok, "[,(] const std :: %type% < %type% > %var% [,)]")) + { + passedByValueError(tok, tok->strAt(8)); + } + + else if (Token::Match(tok, "[,(] const std :: %type% < std :: %type% > %var% [,)]")) + { + passedByValueError(tok, tok->strAt(10)); + } + else if (Token::Match(tok, "[,(] const %type% %var% [,)]")) { // Check if type is a struct or class. diff --git a/test/testother.cpp b/test/testother.cpp index b170aeb86..457ea3728 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1015,6 +1015,30 @@ private: ASSERT_EQUALS("", errout.str()); } + + { + testPassedByValue("void f(const std::vector v)\n" + "{\n" + "}\n"); + + ASSERT_EQUALS("[test.cpp:1]: (style) Function parameter 'v' is passed by value. It could be passed by reference instead.\n", errout.str()); + } + + { + testPassedByValue("void f(const std::vector v)\n" + "{\n" + "}\n"); + + ASSERT_EQUALS("[test.cpp:1]: (style) Function parameter 'v' is passed by value. It could be passed by reference instead.\n", errout.str()); + } + + { + testPassedByValue("void f(const std::vector &v)\n" + "{\n" + "}\n"); + + ASSERT_EQUALS("", errout.str()); + } } };