From 0d316af4f2f3e09bc77ac2502a146dd042adc51e Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 16 Feb 2013 02:50:25 -0800 Subject: [PATCH] Fixed false positive stlSize for code like "foo + 1 > bar.size()" (#4584) --- lib/checkstl.cpp | 4 ++-- test/teststl.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index c8afa07e4..09867009d 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -951,8 +951,8 @@ void CheckStl::size() } // 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 ((tok->previous() && !tok->previous()->isArithmeticalOp() && Token::Match(end, ">=|< 1") && !end->tokAt(2)->isArithmeticalOp()) || + (end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "1 <=|>") && !tok->tokAt(-3)->isArithmeticalOp())) { if (isContainerSizeSlow(tok1)) sizeError(tok1); } diff --git a/test/teststl.cpp b/test/teststl.cpp index 02d4b9167..4c13e03f8 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1572,6 +1572,18 @@ private: " fun(width % x.size() != 0);\n" "}"); ASSERT_EQUALS("", errout.str()); + + // #4584 + check("void f() {\n" + " std::list x;\n" + " if (foo + 1 > x.size()) {}\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("void f() {\n" + " std::list x;\n" + " if (x.size() < 1 + foo) {}\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void redundantCondition1() {