From 7c370ec87399d2e91654c0c30440eb3e01f7d7cc Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Sun, 4 Nov 2012 16:15:26 +0100 Subject: [PATCH] Fixed #4305 (improve check: 'vector.size() < 1' should result in 'inefficient checking for '...' emptiness.') --- lib/checkstl.cpp | 7 +++++++ test/teststl.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 29e5c0e3a..3840812ab 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -954,6 +954,13 @@ void CheckStl::size() sizeError(tok1); } + // check for comparison to one + if ((tok->previous() && !tok->previous()->isArithmeticalOp() && Token::Match(end, ">=|< 1")) || + (end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "1 <=|>"))) { + if (isStlContainer(varid)) + sizeError(tok1); + } + // check for using as boolean expression else if ((Token::Match(tok->tokAt(-2), "if|while (") && end->str() == ")") || (tok->previous()->type() == Token::eLogicalOp && Token::Match(end, "&&|)|,|;|%oror%"))) { diff --git a/test/teststl.cpp b/test/teststl.cpp index 076093d46..07109f66a 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1441,6 +1441,34 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str()); + check("void f()\n" + "{\n" + " std::list x;\n" + " if (x.size() >= 1) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str()); + + check("void f()\n" + "{\n" + " std::list x;\n" + " if (x.size() < 1) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str()); + + check("void f()\n" + "{\n" + " std::list x;\n" + " if (1 <= x.size()) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str()); + + check("void f()\n" + "{\n" + " std::list x;\n" + " if (1 > x.size()) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str()); + check("void f()\n" "{\n" " std::list x;\n"