From 80290a15e01b96895b949474ef1ca9e8af763ff5 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 8 Aug 2018 01:31:31 -0500 Subject: [PATCH] Fix FP with duplicate assign (#1330) --- lib/checkother.cpp | 1 + test/testother.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index abc9fec4f..b56f1e9c1 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1965,6 +1965,7 @@ void CheckOther::checkDuplicateExpression() tok->next()->tokType() != Token::eName && isSameExpression(mTokenizer->isCPP(), true, tok->next(), nextAssign->next(), mSettings->library, true) && isSameExpression(mTokenizer->isCPP(), true, tok->astOperand2(), nextAssign->astOperand2(), mSettings->library, true) && + tok->astOperand2()->expressionString() == nextAssign->astOperand2()->expressionString() && !isUniqueExpression(tok->astOperand2())) { bool assigned = false; const Scope * varScope = var1->scope() ? var1->scope() : &scope; diff --git a/test/testother.cpp b/test/testother.cpp index 2588bef39..8b2998519 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4249,6 +4249,23 @@ private: "}"); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'.\n", errout.str()); + check("int f() __attribute__((pure));\n" + "int g() __attribute__((pure));\n" + "void test() {\n" + " int i = f() + 1;\n" + " int j = 1 + f();\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("int f() __attribute__((pure));\n" + "int g() __attribute__((pure));\n" + "void test() {\n" + " int x = f();\n" + " int i = x + 1;\n" + " int j = f() + 1;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("int f() __attribute__((pure));\n" "int g() __attribute__((pure));\n" "void test() {\n" @@ -4265,6 +4282,15 @@ private: "}"); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'.\n", errout.str()); + check("int f(int) __attribute__((pure));\n" + "int g(int) __attribute__((pure));\n" + "void test() {\n" + " const int x = 0;\n" + " int i = f(0);\n" + " int j = f(x);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("void test(int * p, int * q) {\n" " int i = *p;\n" " int j = *p;\n" @@ -4364,6 +4390,12 @@ private: " int j = x--;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void test(int x) {\n" + " int i = x + 1;\n" + " int j = 1 + x;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void duplicateVarExpressionUnique() {