Skip to content

Commit 648f09f

Browse files
authored
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.
1 parent ac48167 commit 648f09f

4 files changed

Lines changed: 43 additions & 31 deletions

File tree

lib/checkclass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,6 +2891,9 @@ void CheckClass::virtualFunctionCallInConstructorError(
28912891
const std::list<const Token *> & tokStack,
28922892
const std::string &funcname)
28932893
{
2894+
if (scopeFunction && !mSettings->severity.isEnabled(Severity::style) && !mSettings->isPremiumEnabled("virtualCallInConstructor"))
2895+
return;
2896+
28942897
const char * scopeFunctionTypeName = scopeFunction ? getFunctionTypeName(scopeFunction->type) : "constructor";
28952898

28962899
ErrorPath errorPath;

lib/checkother.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,10 +2402,19 @@ namespace {
24022402

24032403
void CheckOther::checkDuplicateExpression()
24042404
{
2405-
const bool styleEnabled = mSettings->severity.isEnabled(Severity::style);
2406-
const bool warningEnabled = mSettings->severity.isEnabled(Severity::warning);
2407-
if (!styleEnabled && !warningEnabled)
2408-
return;
2405+
{
2406+
const bool styleEnabled = mSettings->severity.isEnabled(Severity::style);
2407+
const bool premiumEnabled = mSettings->isPremiumEnabled("oppositeExpression") ||
2408+
mSettings->isPremiumEnabled("duplicateExpression") ||
2409+
mSettings->isPremiumEnabled("duplicateAssignExpression") ||
2410+
mSettings->isPremiumEnabled("duplicateExpressionTernary") ||
2411+
mSettings->isPremiumEnabled("duplicateValueTernary") ||
2412+
mSettings->isPremiumEnabled("selfAssignment") ||
2413+
mSettings->isPremiumEnabled("knownConditionTrueFalse");
2414+
2415+
if (!styleEnabled && !premiumEnabled)
2416+
return;
2417+
}
24092418

24102419
logChecker("CheckOther::checkDuplicateExpression"); // style,warning
24112420

@@ -2511,9 +2520,9 @@ void CheckOther::checkDuplicateExpression()
25112520
!findExpressionChanged(tok, tok, loopTok->link()->next()->link(), mSettings, cpp)) {
25122521
const bool isEnum = tok->scope()->type == Scope::eEnum;
25132522
const bool assignment = !isEnum && tok->str() == "=";
2514-
if (assignment && warningEnabled)
2523+
if (assignment)
25152524
selfAssignmentError(tok, tok->astOperand1()->expressionString());
2516-
else if (styleEnabled && !isEnum) {
2525+
else if (!isEnum) {
25172526
if (cpp && mSettings->standards.cpp >= Standards::CPP11 && tok->str() == "==") {
25182527
const Token* parent = tok->astParent();
25192528
while (parent && parent->astParent()) {
@@ -2535,11 +2544,10 @@ void CheckOther::checkDuplicateExpression()
25352544
mSettings->library,
25362545
true,
25372546
false)) {
2538-
if (warningEnabled && isWithoutSideEffects(cpp, tok->astOperand1())) {
2547+
if (isWithoutSideEffects(cpp, tok->astOperand1())) {
25392548
selfAssignmentError(tok, tok->astOperand1()->expressionString());
25402549
}
2541-
} else if (styleEnabled &&
2542-
isOppositeExpression(cpp,
2550+
} else if (isOppositeExpression(cpp,
25432551
tok->astOperand1(),
25442552
tok->astOperand2(),
25452553
mSettings->library,
@@ -2550,7 +2558,7 @@ void CheckOther::checkDuplicateExpression()
25502558
isWithoutSideEffects(cpp, tok->astOperand1())) {
25512559
oppositeExpressionError(tok, std::move(errorPath));
25522560
} else if (!Token::Match(tok, "[-/%]")) { // These operators are not associative
2553-
if (styleEnabled && tok->astOperand2() && tok->str() == tok->astOperand1()->str() &&
2561+
if (tok->astOperand2() && tok->str() == tok->astOperand1()->str() &&
25542562
isSameExpression(cpp,
25552563
true,
25562564
tok->astOperand2(),
@@ -2577,7 +2585,7 @@ void CheckOther::checkDuplicateExpression()
25772585
}
25782586
}
25792587
}
2580-
} else if (styleEnabled && tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") {
2588+
} else if (tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") {
25812589
if (!tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()) &&
25822590
!isVariableChanged(tok->astParent(), /*indirect*/ 0, mSettings, cpp) &&
25832591
isConstStatement(tok->astOperand1(), cpp) && isConstStatement(tok->astOperand2(), cpp))
@@ -2611,17 +2619,18 @@ void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2,
26112619
const std::string& op = opTok ? opTok->str() : "&&";
26122620
std::string msg = "Same expression " + (hasMultipleExpr ? "\'" + expr1 + "\'" + " found multiple times in chain of \'" + op + "\' operators" : "on both sides of \'" + op + "\'");
26132621
const char *id = "duplicateExpression";
2614-
if (expr1 != expr2 && (!opTok || !opTok->isArithmeticalOp())) {
2622+
if (expr1 != expr2 && (!opTok || Token::Match(opTok, "%oror%|%comp%|&&|?|!"))) {
26152623
id = "knownConditionTrueFalse";
26162624
std::string exprMsg = "The comparison \'" + expr1 + " " + op + " " + expr2 + "\' is always ";
26172625
if (Token::Match(opTok, "==|>=|<="))
26182626
msg = exprMsg + "true";
26192627
else if (Token::Match(opTok, "!=|>|<"))
26202628
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";
26232629
}
26242630

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+
26252634
reportError(errors, Severity::style, id, msg +
26262635
(std::string(".\nFinding the same expression ") + (hasMultipleExpr ? "more than once in a condition" : "on both sides of an operator")) +
26272636
" is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to "
@@ -2659,7 +2668,7 @@ void CheckOther::duplicateValueTernaryError(const Token *tok)
26592668

26602669
void CheckOther::selfAssignmentError(const Token *tok, const std::string &varname)
26612670
{
2662-
reportError(tok, Severity::warning,
2671+
reportError(tok, Severity::style,
26632672
"selfAssignment",
26642673
"$symbol:" + varname + "\n"
26652674
"Redundant assignment of '$symbol' to itself.", CWE398, Certainty::normal);

test/testclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8027,7 +8027,7 @@ class TestClass : public TestFixture {
80278027
errout.str("");
80288028

80298029
// Check..
8030-
const Settings settings = settingsBuilder().severity(Severity::warning).certainty(Certainty::inconclusive, inconclusive).build();
8030+
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).certainty(Certainty::inconclusive, inconclusive).build();
80318031

80328032
Preprocessor preprocessor(settings);
80338033

test/testother.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5467,26 +5467,26 @@ class TestOther : public TestFixture {
54675467
" x = x;\n"
54685468
" return 0;\n"
54695469
"}");
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());
54715471

54725472
check("void foo()\n"
54735473
"{\n"
54745474
" int x = x;\n"
54755475
"}");
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());
54775477

54785478
check("struct A { int b; };\n"
54795479
"void foo(A* a1, A* a2) {\n"
54805480
" a1->b = a1->b;\n"
54815481
"}");
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());
54835483

54845484
check("int x;\n"
54855485
"void f()\n"
54865486
"{\n"
54875487
" x = x = 3;\n"
54885488
"}");
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());
54905490

54915491
// #4073 (segmentation fault)
54925492
check("void Foo::myFunc( int a )\n"
@@ -5514,7 +5514,7 @@ class TestOther : public TestFixture {
55145514
" BAR *x = getx();\n"
55155515
" x = x;\n"
55165516
"}");
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());
55185518

55195519
// #2502 - non-primitive type -> there might be some side effects
55205520
check("void foo()\n"
@@ -5546,7 +5546,7 @@ class TestOther : public TestFixture {
55465546
"void f() {\n"
55475547
" i = i;\n"
55485548
"}");
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());
55505550

55515551
// #4291 - id for variables accessed through 'this'
55525552
check("class Foo {\n"
@@ -5556,7 +5556,7 @@ class TestOther : public TestFixture {
55565556
"void Foo::func() {\n"
55575557
" this->var = var;\n"
55585558
"}");
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());
55605560

55615561
check("class Foo {\n"
55625562
" int var;\n"
@@ -5575,7 +5575,7 @@ class TestOther : public TestFixture {
55755575
"void f() {\n"
55765576
" struct callbacks ops = { .s = ops.s };\n"
55775577
"}");
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());
55795579

55805580
check("class V\n"
55815581
"{\n"
@@ -5590,9 +5590,9 @@ class TestOther : public TestFixture {
55905590
" }\n"
55915591
" double x, y, z;\n"
55925592
"};");
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());
55965596

55975597
check("void f(int i) { i = !!i; }");
55985598
ASSERT_EQUALS("", errout.str());
@@ -5602,7 +5602,7 @@ class TestOther : public TestFixture {
56025602
" int &ref = x;\n"
56035603
" ref = x;\n"
56045604
"}\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());
56065606

56075607
check("class Foo {\n" // #9850
56085608
" int i{};\n"
@@ -7057,7 +7057,7 @@ class TestOther : public TestFixture {
70577057
" int var = buffer[index - 1];\n"
70587058
" return buffer[index - 1] - var;\n" // <<
70597059
"}");
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());
70617061
}
70627062

70637063
void duplicateExpression13() { //#7899
@@ -10750,8 +10750,8 @@ class TestOther : public TestFixture {
1075010750
" int x = x = y + 1;\n"
1075110751
"}", "test.c");
1075210752
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
1075510755
errout.str());
1075610756
}
1075710757

0 commit comments

Comments
 (0)