You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix #12431 (Style warnings are reported when only --enable=warning is used) (#5974)
* do not show knownConditionTrueFalse, virtualCallInConstructor, duplicateExpression style messages unless --enable=style is configured.
* change selfAssignment from warning to style => it's not about undefined behavior
* don't claim that code such as `a = b|c;` is a condition.
const std::string& op = opTok ? opTok->str() : "&&";
2612
2620
std::string msg = "Same expression " + (hasMultipleExpr ? "\'" + expr1 + "\'" + " found multiple times in chain of \'" + op + "\' operators" : "on both sides of \'" + op + "\'");
2613
2621
constchar *id = "duplicateExpression";
2614
-
if (expr1 != expr2 && (!opTok || !opTok->isArithmeticalOp())) {
2622
+
if (expr1 != expr2 && (!opTok || Token::Match(opTok, "%oror%|%comp%|&&|?|!"))) {
2615
2623
id = "knownConditionTrueFalse";
2616
2624
std::string exprMsg = "The comparison \'" + expr1 + "" + op + "" + expr2 + "\' is always ";
2617
2625
if (Token::Match(opTok, "==|>=|<="))
2618
2626
msg = exprMsg + "true";
2619
2627
elseif (Token::Match(opTok, "!=|>|<"))
2620
2628
msg = exprMsg + "false";
2621
-
if (!Token::Match(tok1, "%num%|NULL|nullptr") && !Token::Match(tok2, "%num%|NULL|nullptr"))
2622
-
msg += " because '" + expr1 + "' and '" + expr2 + "' represent the same value";
2623
2629
}
2624
2630
2631
+
if (expr1 != expr2 && !Token::Match(tok1, "%num%|NULL|nullptr") && !Token::Match(tok2, "%num%|NULL|nullptr"))
2632
+
msg += " because '" + expr1 + "' and '" + expr2 + "' represent the same value";
2633
+
2625
2634
reportError(errors, Severity::style, id, msg +
2626
2635
(std::string(".\nFinding the same expression ") + (hasMultipleExpr ? "more than once in a condition" : "on both sides of an operator")) +
2627
2636
" is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to "
Copy file name to clipboardExpand all lines: test/testother.cpp
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -5467,26 +5467,26 @@ class TestOther : public TestFixture {
5467
5467
" x = x;\n"
5468
5468
" return 0;\n"
5469
5469
"}");
5470
-
ASSERT_EQUALS("[test.cpp:4]: (warning) Redundant assignment of 'x' to itself.\n", errout.str());
5470
+
ASSERT_EQUALS("[test.cpp:4]: (style) Redundant assignment of 'x' to itself.\n", errout.str());
5471
5471
5472
5472
check("void foo()\n"
5473
5473
"{\n"
5474
5474
" int x = x;\n"
5475
5475
"}");
5476
-
ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'x' to itself.\n", errout.str());
5476
+
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant assignment of 'x' to itself.\n", errout.str());
5477
5477
5478
5478
check("struct A { int b; };\n"
5479
5479
"void foo(A* a1, A* a2) {\n"
5480
5480
" a1->b = a1->b;\n"
5481
5481
"}");
5482
-
ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'a1->b' to itself.\n", errout.str());
5482
+
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant assignment of 'a1->b' to itself.\n", errout.str());
5483
5483
5484
5484
check("int x;\n"
5485
5485
"void f()\n"
5486
5486
"{\n"
5487
5487
" x = x = 3;\n"
5488
5488
"}");
5489
-
ASSERT_EQUALS("[test.cpp:4]: (warning) Redundant assignment of 'x' to itself.\n", errout.str());
5489
+
ASSERT_EQUALS("[test.cpp:4]: (style) Redundant assignment of 'x' to itself.\n", errout.str());
5490
5490
5491
5491
// #4073 (segmentation fault)
5492
5492
check("void Foo::myFunc( int a )\n"
@@ -5514,7 +5514,7 @@ class TestOther : public TestFixture {
5514
5514
" BAR *x = getx();\n"
5515
5515
" x = x;\n"
5516
5516
"}");
5517
-
ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'x' to itself.\n", errout.str());
5517
+
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant assignment of 'x' to itself.\n", errout.str());
5518
5518
5519
5519
// #2502 - non-primitive type -> there might be some side effects
5520
5520
check("void foo()\n"
@@ -5546,7 +5546,7 @@ class TestOther : public TestFixture {
5546
5546
"void f() {\n"
5547
5547
" i = i;\n"
5548
5548
"}");
5549
-
ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'i' to itself.\n", errout.str());
5549
+
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant assignment of 'i' to itself.\n", errout.str());
5550
5550
5551
5551
// #4291 - id for variables accessed through 'this'
5552
5552
check("class Foo {\n"
@@ -5556,7 +5556,7 @@ class TestOther : public TestFixture {
5556
5556
"void Foo::func() {\n"
5557
5557
" this->var = var;\n"
5558
5558
"}");
5559
-
ASSERT_EQUALS("[test.cpp:6]: (warning) Redundant assignment of 'this->var' to itself.\n", errout.str());
5559
+
ASSERT_EQUALS("[test.cpp:6]: (style) Redundant assignment of 'this->var' to itself.\n", errout.str());
5560
5560
5561
5561
check("class Foo {\n"
5562
5562
" int var;\n"
@@ -5575,7 +5575,7 @@ class TestOther : public TestFixture {
5575
5575
"void f() {\n"
5576
5576
" struct callbacks ops = { .s = ops.s };\n"
5577
5577
"}");
5578
-
TODO_ASSERT_EQUALS("[test.cpp:6]: (warning) Redundant assignment of 'something' to itself.\n", "", errout.str());
5578
+
TODO_ASSERT_EQUALS("[test.cpp:6]: (style) Redundant assignment of 'something' to itself.\n", "", errout.str());
5579
5579
5580
5580
check("class V\n"
5581
5581
"{\n"
@@ -5590,9 +5590,9 @@ class TestOther : public TestFixture {
5590
5590
" }\n"
5591
5591
" double x, y, z;\n"
5592
5592
"};");
5593
-
ASSERT_EQUALS("[test.cpp:10]: (warning) Redundant assignment of 'x' to itself.\n"
5594
-
"[test.cpp:10]: (warning) Redundant assignment of 'y' to itself.\n"
5595
-
"[test.cpp:10]: (warning) Redundant assignment of 'z' to itself.\n", errout.str());
5593
+
ASSERT_EQUALS("[test.cpp:10]: (style) Redundant assignment of 'x' to itself.\n"
5594
+
"[test.cpp:10]: (style) Redundant assignment of 'y' to itself.\n"
5595
+
"[test.cpp:10]: (style) Redundant assignment of 'z' to itself.\n", errout.str());
5596
5596
5597
5597
check("void f(int i) { i = !!i; }");
5598
5598
ASSERT_EQUALS("", errout.str());
@@ -5602,7 +5602,7 @@ class TestOther : public TestFixture {
5602
5602
" int &ref = x;\n"
5603
5603
" ref = x;\n"
5604
5604
"}\n");
5605
-
ASSERT_EQUALS("[test.cpp:4]: (warning) Redundant assignment of 'ref' to itself.\n", errout.str());
5605
+
ASSERT_EQUALS("[test.cpp:4]: (style) Redundant assignment of 'ref' to itself.\n", errout.str());
5606
5606
5607
5607
check("class Foo {\n"// #9850
5608
5608
" int i{};\n"
@@ -7057,7 +7057,7 @@ class TestOther : public TestFixture {
7057
7057
" int var = buffer[index - 1];\n"
7058
7058
" return buffer[index - 1] - var;\n"// <<
7059
7059
"}");
7060
-
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Same expression on both sides of '-'.\n", errout.str());
7060
+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Same expression on both sides of '-' because 'buffer[index-1]' and 'var' represent the same value.\n", errout.str());
7061
7061
}
7062
7062
7063
7063
voidduplicateExpression13() { //#7899
@@ -10750,8 +10750,8 @@ class TestOther : public TestFixture {
10750
10750
" int x = x = y + 1;\n"
10751
10751
"}", "test.c");
10752
10752
ASSERT_EQUALS(
10753
-
"[test.c:2]: (warning) Redundant assignment of 'x' to itself.\n"
10754
-
"[test.c:2]: (warning) Redundant assignment of 'x' to itself.\n", // duplicate
10753
+
"[test.c:2]: (style) Redundant assignment of 'x' to itself.\n"
10754
+
"[test.c:2]: (style) Redundant assignment of 'x' to itself.\n", // duplicate
0 commit comments