diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 393f13779..cf903fb50 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -87,7 +87,7 @@ void CheckStl::outOfBounds() } if (!container->arrayLike_indexOp && !container->stdStringLike) continue; - if (value.intvalue == 0 && Token::Match(parent, "[")) { + if (value.intvalue == 0 && Token::Match(parent, "[") && tok == parent->astOperand1()) { outOfBoundsError(parent, tok->expressionString(), &value, "", nullptr); continue; } diff --git a/test/teststl.cpp b/test/teststl.cpp index 4f2c495d0..105e7c4f4 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -305,6 +305,12 @@ private: " return (*pv)[42];\n" "}\n"); ASSERT_EQUALS("test.cpp:4:error:Out of bounds access in expression '(*pv)[42]' because '*pv' is empty.\n", errout.str()); + + checkNormal("void f() {\n" + " std::string s;\n" + " ++abc[s];\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void outOfBoundsIndexExpression() {