See More

/* * Cppcheck - A tool for static C/C++ code analysis * Copyright (C) 2007-2026 Cppcheck team. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "checkother.h" #include "errortypes.h" #include "fixture.h" #include "helpers.h" #include "platform.h" #include "settings.h" #include "standards.h" #include #include #include static std::string unionZeroInitMessage(int lno, int cno, const std::string &varName, const std::string &largestMemberName) { std::stringstream ss; ss << "[test.cpp:" << lno << ":" << cno << "]: (portability) "; ss << "Zero initializing union '" << varName << "' "; ss << "does not guarantee its complete storage to be zero initialized as its largest member is not declared as the first member. "; ss << "Consider making " << largestMemberName << " the first member or favor memset(). [UnionZeroInit]"; ss << std::endl; return ss.str(); } class TestOther : public TestFixture { public: TestOther() : TestFixture("TestOther") {} private: const Settings settings0 = settingsBuilder().library("std.cfg").build(); /*const*/ Settings settings1 = settingsBuilder().library("std.cfg").severity(Severity::style).severity(Severity::warning).severity(Severity::portability).severity(Severity::performance).build(); const Settings settings2 = settingsBuilder(settings1).certainty(Certainty::inconclusive).build(); void run() override { mNewTemplate = true; TEST_CASE(emptyBrackets); TEST_CASE(zeroDiv1); TEST_CASE(zeroDiv2); TEST_CASE(zeroDiv3); TEST_CASE(zeroDiv4); TEST_CASE(zeroDiv5); TEST_CASE(zeroDiv6); TEST_CASE(zeroDiv7); // #4930 TEST_CASE(zeroDiv8); TEST_CASE(zeroDiv9); TEST_CASE(zeroDiv10); TEST_CASE(zeroDiv11); TEST_CASE(zeroDiv12); TEST_CASE(zeroDiv13); TEST_CASE(zeroDiv14); // #1169 TEST_CASE(zeroDiv15); // #8319 TEST_CASE(zeroDiv16); // #11158 TEST_CASE(zeroDiv17); // #9931 TEST_CASE(zeroDiv18); TEST_CASE(zeroDiv19); TEST_CASE(zeroDiv20); // #11175 TEST_CASE(zeroDiv21); TEST_CASE(zeroDiv22); TEST_CASE(zeroDivCond); // division by zero / useless condition TEST_CASE(zeroDivErrorPath); TEST_CASE(nanInArithmeticExpression); TEST_CASE(varScope1); TEST_CASE(varScope2); TEST_CASE(varScope3); TEST_CASE(varScope4); TEST_CASE(varScope5); TEST_CASE(varScope6); TEST_CASE(varScope7); TEST_CASE(varScope8); TEST_CASE(varScope9); // classes may have extra side-effects TEST_CASE(varScope10); // Undefined macro FOR TEST_CASE(varScope11); // #2475 - struct initialization is not inner scope TEST_CASE(varScope12); TEST_CASE(varScope13); // variable usage in inner loop TEST_CASE(varScope14); TEST_CASE(varScope15); // #4573 if-else-if TEST_CASE(varScope16); TEST_CASE(varScope17); TEST_CASE(varScope18); TEST_CASE(varScope20); // Ticket #5103 TEST_CASE(varScope21); // Ticket #5382 TEST_CASE(varScope22); // Ticket #5684 TEST_CASE(varScope23); // Ticket #6154 TEST_CASE(varScope24); // pointer / reference TEST_CASE(varScope25); // time_t TEST_CASE(varScope26); // range for loop, map TEST_CASE(varScope27); // #7733 - #if TEST_CASE(varScope28); // #10527 TEST_CASE(varScope29); // #10888 TEST_CASE(varScope30); // #8541 TEST_CASE(varScope31); // #11099 TEST_CASE(varScope32); // #11441 TEST_CASE(varScope33); TEST_CASE(varScope34); TEST_CASE(varScope35); TEST_CASE(varScope36); // #12158 TEST_CASE(varScope37); // #12158 TEST_CASE(varScope38); TEST_CASE(varScope39); TEST_CASE(varScope40); TEST_CASE(varScope41); // #11845 TEST_CASE(varScope42); TEST_CASE(varScope43); TEST_CASE(varScope44); TEST_CASE(varScope45); TEST_CASE(varScope46); TEST_CASE(oldStylePointerCast); TEST_CASE(intToPointerCast); TEST_CASE(invalidPointerCast); TEST_CASE(passedByValue); TEST_CASE(passedByValue_nonConst); TEST_CASE(passedByValue_externC); TEST_CASE(constVariable); TEST_CASE(constParameterCallback); TEST_CASE(constPointer); TEST_CASE(constArray); TEST_CASE(switchRedundantAssignmentTest); TEST_CASE(switchRedundantOperationTest); TEST_CASE(switchRedundantBitwiseOperationTest); TEST_CASE(unreachableCode); TEST_CASE(unreachableSwitchCase); // #8442 TEST_CASE(redundantContinue); TEST_CASE(suspiciousCase); TEST_CASE(suspiciousEqualityComparison); TEST_CASE(suspiciousUnaryPlusMinus); // #8004 TEST_CASE(suspiciousFloatingPointCast); TEST_CASE(selfAssignment); TEST_CASE(trac1132); TEST_CASE(testMisusedScopeObjectDoesNotPickFunction1); TEST_CASE(testMisusedScopeObjectDoesNotPickFunction2); TEST_CASE(testMisusedScopeObjectPicksClass); TEST_CASE(testMisusedScopeObjectPicksStruct); TEST_CASE(testMisusedScopeObjectDoesNotPickIf); TEST_CASE(testMisusedScopeObjectDoesNotPickConstructorDeclaration); TEST_CASE(testMisusedScopeObjectDoesNotPickFunctor); TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassConstructors); TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject); TEST_CASE(testMisusedScopeObjectDoesNotPickPureC); TEST_CASE(testMisusedScopeObjectDoesNotPickNestedClass); TEST_CASE(testMisusedScopeObjectInConstructor); TEST_CASE(testMisusedScopeObjectStandardType); TEST_CASE(testMisusedScopeObjectNamespace); TEST_CASE(testMisusedScopeObjectAssignment); // #11371 TEST_CASE(trac2071); TEST_CASE(trac2084); TEST_CASE(trac3693); TEST_CASE(clarifyCalculation); TEST_CASE(clarifyStatement); TEST_CASE(duplicateBranch); TEST_CASE(duplicateBranch1); // tests extracted by http://www.viva64.com/en/b/0149/ ( Comparison between PVS-Studio and cppcheck ): Errors detected in Quake 3: Arena by PVS-Studio: Fragment 2 TEST_CASE(duplicateBranch2); // empty macro TEST_CASE(duplicateBranch3); TEST_CASE(duplicateBranch4); TEST_CASE(duplicateBranch5); // make sure the Token attributes are compared TEST_CASE(duplicateBranch6); TEST_CASE(duplicateExpression1); TEST_CASE(duplicateExpression2); // ticket #2730 TEST_CASE(duplicateExpression3); // ticket #3317 TEST_CASE(duplicateExpression4); // ticket #3354 (++) TEST_CASE(duplicateExpression5); // ticket #3749 (macros with same values) TEST_CASE(duplicateExpression6); // ticket #4639 TEST_CASE(duplicateExpression7); TEST_CASE(duplicateExpression8); TEST_CASE(duplicateExpression9); // #9320 TEST_CASE(duplicateExpression10); // #9485 TEST_CASE(duplicateExpression11); // #8916 (function call) TEST_CASE(duplicateExpression12); // #10026 TEST_CASE(duplicateExpression13); // #7899 TEST_CASE(duplicateExpression14); // #9871 TEST_CASE(duplicateExpression15); // #10650 TEST_CASE(duplicateExpression16); // #10569 TEST_CASE(duplicateExpression17); // #12036 TEST_CASE(duplicateExpression18); TEST_CASE(duplicateExpression19); TEST_CASE(duplicateExpression20); TEST_CASE(duplicateExpression21); TEST_CASE(duplicateExpression22); TEST_CASE(duplicateExpressionLoop); TEST_CASE(duplicateValueTernary); TEST_CASE(duplicateValueTernarySizeof); // #13773 TEST_CASE(duplicateExpressionTernary); // #6391 TEST_CASE(duplicateExpressionTemplate); // #6930 TEST_CASE(duplicateExpressionCompareWithZero); TEST_CASE(oppositeExpression); TEST_CASE(duplicateVarExpression); TEST_CASE(duplicateVarExpressionUnique); TEST_CASE(duplicateVarExpressionAssign); TEST_CASE(duplicateVarExpressionCrash); TEST_CASE(multiConditionSameExpression); TEST_CASE(checkSignOfUnsignedVariable); TEST_CASE(checkSignOfPointer); TEST_CASE(checkSuspiciousSemicolon1); TEST_CASE(checkSuspiciousSemicolon2); TEST_CASE(checkSuspiciousSemicolon3); TEST_CASE(checkSuspiciousComparison); TEST_CASE(checkInvalidFree); TEST_CASE(checkRedundantCopy); TEST_CASE(checkNegativeShift); TEST_CASE(incompleteArrayFill); TEST_CASE(redundantVarAssignment); TEST_CASE(redundantVarAssignment_trivial); TEST_CASE(redundantVarAssignment_struct); TEST_CASE(redundantVarAssignment_union); TEST_CASE(redundantVarAssignment_7133); TEST_CASE(redundantVarAssignment_stackoverflow); TEST_CASE(redundantVarAssignment_lambda); TEST_CASE(redundantVarAssignment_loop); TEST_CASE(redundantVarAssignment_after_switch); TEST_CASE(redundantVarAssignment_pointer); TEST_CASE(redundantVarAssignment_pointer_parameter); TEST_CASE(redundantVarAssignment_array); TEST_CASE(redundantVarAssignment_switch_break); TEST_CASE(redundantInitialization); //TEST_CASE(redundantMemWrite); // FIXME: temporary hack TEST_CASE(redundantAssignmentSameValue); TEST_CASE(varFuncNullUB); TEST_CASE(checkCastIntToCharAndBack); // ticket #160 TEST_CASE(checkCommaSeparatedReturn); TEST_CASE(checkPassByReference); TEST_CASE(checkComparisonFunctionIsAlwaysTrueOrFalse); TEST_CASE(integerOverflow); // #5895 TEST_CASE(redundantPointerOp); TEST_CASE(test_isSameExpression); TEST_CASE(raceAfterInterlockedDecrement); TEST_CASE(testUnusedLabel); TEST_CASE(testUnusedLabelConfiguration); TEST_CASE(testUnusedLabelSwitchConfiguration); TEST_CASE(testUnusedLabelPremiumMisra); TEST_CASE(testEvaluationOrder); TEST_CASE(testEvaluationOrderSelfAssignment); TEST_CASE(testEvaluationOrderMacro); TEST_CASE(testEvaluationOrderSequencePointsFunctionCall); TEST_CASE(testEvaluationOrderSequencePointsComma); TEST_CASE(testEvaluationOrderSizeof); TEST_CASE(testUnsignedLessThanZero); TEST_CASE(doubleMove1); TEST_CASE(doubleMoveMemberInitialization1); TEST_CASE(doubleMoveMemberInitialization2); TEST_CASE(doubleMoveMemberInitialization3); // #9974 TEST_CASE(doubleMoveMemberInitialization4); TEST_CASE(moveAndAssign1); TEST_CASE(moveAndAssign2); TEST_CASE(moveAssignMoveAssign); TEST_CASE(moveAndReset1); TEST_CASE(moveAndReset2); TEST_CASE(moveResetMoveReset); TEST_CASE(moveAndFunctionParameter); TEST_CASE(moveAndFunctionParameterReference); TEST_CASE(moveAndFunctionParameterConstReference); TEST_CASE(moveAndFunctionParameterUnknown); TEST_CASE(moveAndReturn); TEST_CASE(moveAndClear); TEST_CASE(movedPointer); TEST_CASE(moveAndAddressOf); TEST_CASE(partiallyMoved); TEST_CASE(moveAndLambda); TEST_CASE(moveInLoop); TEST_CASE(moveCallback); TEST_CASE(moveClassVariable); TEST_CASE(forwardAndUsed); TEST_CASE(moveAndReference); TEST_CASE(moveForRange); TEST_CASE(moveTernary); TEST_CASE(movePointerAlias); TEST_CASE(moveOutparam); TEST_CASE(moveTryEmplace); TEST_CASE(funcArgNamesDifferent); TEST_CASE(funcArgOrderDifferent); TEST_CASE(cpp11FunctionArgInit); // #7846 - "void foo(int declaration = {}) {" TEST_CASE(shadowVariables); TEST_CASE(knownArgument); TEST_CASE(knownArgumentHiddenVariableExpression); TEST_CASE(knownArgumentTernaryOperator); TEST_CASE(checkComparePointers); TEST_CASE(unusedVariableValueTemplate); // #8994 TEST_CASE(moduloOfOne); TEST_CASE(sameExpressionPointers); TEST_CASE(checkOverlappingWrite); TEST_CASE(constVariableArrayMember); // #10371 TEST_CASE(knownPointerToBool); TEST_CASE(iterateByValue); TEST_CASE(knownConditionFloating); TEST_CASE(knownConditionPrefixed); TEST_CASE(unionZeroInitBasic); TEST_CASE(unionZeroInitArrayMember); TEST_CASE(unionZeroInitStructMember); TEST_CASE(unionZeroInitUnknownType); TEST_CASE(unionZeroInitBitfields); } struct CheckOptions { bool cpp = true; bool inconclusive = true; bool verbose = false; Settings* settings = nullptr; // TODO: split from this }; #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template void check_(const char* file, int line, const char (&code)[size], const CheckOptions& opt = make_default_obj{}) { // TODO: do not modify object passed into Settings* settings; if (!opt.settings) { settings = &settings1; } else { settings = opt.settings; } settings->certainty.setEnabled(Certainty::inconclusive, opt.inconclusive); settings->verbose = opt.verbose; // Tokenize.. SimpleTokenizer tokenizer(*settings, *this, opt.cpp); ASSERT_LOC(tokenizer.tokenize(code), file, line); CheckOther check; runChecks(check, tokenizer, *this); } struct CheckPOptions { bool cpp = true; }; #define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__) template void checkP_(const char* file, int line, const char (&code)[size], const CheckPOptions& options = make_default_obj()) { SimpleTokenizer2 tokenizer(settings2, *this, code, options.cpp ? "test.cpp" : "test.c"); // Tokenizer.. ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line); CheckOther check; runChecks(check, tokenizer, *this); } template void checkInterlockedDecrement(const char (&code)[size]) { /*const*/ Settings settings = settingsBuilder().platform(Platform::Type::Win32A).build(); check(code, dinit(CheckOptions, $.inconclusive = false, $.settings = &settings)); } void emptyBrackets() { check("{\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void zeroDiv1() { // floating point division by zero => no error check("void foo() {\n" " cout << 1. / 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " cout << 42 / (double)0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " cout << 42 / (float)0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " cout << 42 / (int)0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDiv2() { check("void foo()\n" "{\n" " int sum = 0;\n" " for(int i = 0; i < n; i ++)\n" " {\n" " sum += i;\n" " }\n" " cout< do not warn check("void f() {\n" " int a = x/2*3/0;\n" " int b = y/2*3%0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x, int y) {\n" " int a = x/2*3/0;\n" " int b = y/2*3%0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (error) Division by zero. [zerodiv]\n" "[test.cpp:3:16]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDiv8() { // #5584 - FP when function is unknown check("void f() {\n" " int a = 0;\n" " do_something(a);\n" " return 4 / a;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:12]: (error, inconclusive) Division by zero. [zerodiv]\n", errout_str()); } void zeroDiv9() { // #6403 FP zerodiv - inside protecting if-clause check("void foo() {\n" " double fStepHelp = 0;\n" " if( (rOuterValue >>= fStepHelp) ) {\n" " if( fStepHelp != 0.0) {\n" " double fStepMain = 1;\n" " sal_Int32 nIntervalCount = static_cast< sal_Int32 >(fStepMain / fStepHelp);\n" " }\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void zeroDiv10() { // #5402 false positive: (error) Division by zero -- with boost::format check("int main() {\n" " std::cout\n" " << boost::format(\" %d :: %s <> %s\") % 0 % \"a\" % \"b\"\n" " << std::endl;\n" " return 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void zeroDiv11() { check("void f(int a) {\n" " int res = (a+2)/0;\n" " int res = (a*2)/0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:18]: (error) Division by zero. [zerodiv]\n" "[test.cpp:3:18]: (error) Division by zero. [zerodiv]\n", errout_str()); check("void f() {\n" " int res = (a+2)/0;\n" " int res = (a*2)/0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void zeroDiv12() { // #8141 check("intmax_t f() {\n" " return 1 / imaxabs(0);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:12]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDiv13() { // #7324 check("int f () {\n" " int dividend = 10;\n" " int divisor = 1;\n" " dividend = dividend / (--divisor);\n" " return dividend;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:25]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDiv14() { check("void f() {\n" // #1169 " double dx = 1.;\n" " int ix = 1;\n" " int i = 1;\n" " std::cout << ix / (i >> 1) << std::endl;\n" " std::cout << dx / (i >> 1) << std::endl;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:21]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDiv15() { // #8319 check("int f(int i) { return i - 1; }\n" "int f() {\n" " const int d = 1;\n" " const int r = 1 / f(d);\n" " return r;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:21]: (error) Division by zero. [zerodiv]\n", errout_str()); } // #11158 void zeroDiv16() { check("int f(int i) {\n" " int number = 10, a = 0;\n" " for (int count = 0; count < 2; count++) {\n" " a += (i / number) % 10;\n" " number = number / 10;\n" " }\n" " return a;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int i) {\n" " int number = 10, a = 0;\n" " for (int count = 0; count < 2; count++) {\n" " int x = number / 10;\n" " a += (i / number) % 10;\n" " number = x;\n" " }\n" " return a;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void zeroDiv17() { // #9931 check("int f(int len) {\n" " int sz = sizeof(void*[255]) / 255;\n" " int x = len % sz;\n" " return x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void zeroDiv18() { check("int f(int x, int y) {\n" " if (x == y) {}\n" " return 1 / (x-y);\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:11] -> [test.cpp:3:14]: (warning) Either the condition 'x==y' is redundant or there is division by zero at line 3. [zerodivcond]\n", errout_str()); } void zeroDiv19() { check("void f() {\n" // #2456 " for (int i = 0;;)\n" " int j = 10 / i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:20]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDiv20() { check("uint16_t f(void)\n" // #11175 "{\n" " uint16_t x = 0xFFFFU;\n" // UINT16_MAX=0xFFFF " return 42/(++x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:14]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDiv21() { check("int f(int n) {\n" " return 1 / ((1 / n) - 1);\n" "}\n" "int g() {\n" " return f(1);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:14]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDiv22() { check("int main() {\n" " return _Generic((0), int: (42) / (0), default: 0);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:36]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDivCond() { check("void f(unsigned int x) {\n" " int y = 17 / x;\n" " if (x > 0) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:9] -> [test.cpp:2:14]: (warning) Either the condition 'x>0' is redundant or there is division by zero at line 2. [zerodivcond]\n", errout_str()); check("void f(unsigned int x) {\n" " int y = 17 / x;\n" " if (x >= 1) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:9] -> [test.cpp:2:14]: (warning) Either the condition 'x>=1' is redundant or there is division by zero at line 2. [zerodivcond]\n", errout_str()); check("void f(int x) {\n" " int y = 17 / x;\n" " if (x == 0) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:9] -> [test.cpp:2:14]: (warning) Either the condition 'x==0' is redundant or there is division by zero at line 2. [zerodivcond]\n", errout_str()); check("void f(unsigned int x) {\n" " int y = 17 / x;\n" " if (x != 0) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:9] -> [test.cpp:2:14]: (warning) Either the condition 'x!=0' is redundant or there is division by zero at line 2. [zerodivcond]\n", errout_str()); // function call check("void f1(int x, int y) { c=x/y; }\n" "void f2(unsigned int y) {\n" " f1(123,y);\n" " if (y>0){}\n" "}\n"); ASSERT_EQUALS( "[test.cpp:4:10] -> [test.cpp:1:28]: (warning) Either the condition 'y>0' is redundant or there is division by zero at line 1. [zerodivcond]\n", errout_str()); // avoid false positives when variable is changed after division check("void f() {\n" " unsigned int x = do_something();\n" " int y = 17 / x;\n" " x = some+calculation;\n" " if (x != 0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); { // function is called that might modify global variable check("void do_something();\n" "int x;\n" "void f() {\n" " int y = 17 / x;\n" " do_something();\n" " if (x != 0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); // function is called. but don't care, variable is local check("void do_something();\n" "void f() {\n" " int x = some + calculation;\n" " int y = 17 / x;\n" " do_something();\n" " if (x != 0) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:9] -> [test.cpp:4:14]: (warning) Either the condition 'x!=0' is redundant or there is division by zero at line 4. [zerodivcond]\n", errout_str()); } check("void do_something(int value);\n" "void f(int x) {\n" " int y = 17 / x;\n" " do_something(x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int x;\n" "void f() {\n" " int y = 17 / x;\n" " while (y || x == 0) { x--; }\n" "}\n"); ASSERT_EQUALS("", errout_str()); // ticket 5033 segmentation fault (valid code) in CheckOther::checkZeroDivisionOrUselessCondition check("void f() {\n" "double* p1= new double[1];\n" "double* p2= new double[1];\n" "double* p3= new double[1];\n" "double* pp[3] = {p1,p2,p3};\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #5105 - FP check("int f(int a, int b) {\n" " int r = a / b;\n" " if (func(b)) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); // Unknown types for b and c --> do not warn check("int f(int d) {\n" " int r = (a?b:c) / d;\n" " if (d == 0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int a) {\n" " int r = a ? 1 / a : 0;\n" " if (a == 0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int a) {\n" " int r = (a == 0) ? 0 : 1 / a;\n" " if (a == 0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int g();\n" "void f(int b) {\n" " int x = g();\n" " if (x == 0) {}\n" " else if (x > 0) {}\n" " else\n" " a = b / -x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A {\n" " int x;\n" "};\n" "int f(A* a) {\n" " if (a->x == 0) \n" " a->x = 1;\n" " return 1/a->x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #10049 check("int f(int argc) {\n" " int quotient, remainder;\n" " remainder = argc % 2;\n" " argc = 2;\n" " quotient = argc;\n" " if (quotient != 0) \n" " return quotient;\n" " return remainder;\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #11315 checkP("#define STATIC_ASSERT(c) \\\n" "do { enum { sa = 1/(int)(!!(c)) }; } while (0)\n" "void f() {\n" " STATIC_ASSERT(sizeof(int) == sizeof(FOO));\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #11505 check("void f(uint16_t num, uint8_t radix) {\n" " int c = num % radix;\n" " num /= radix;\n" " if (!num) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void zeroDivErrorPath() { setMultiline(); Settings s = settings0; s.templateLocation = "{file}:{line}:note:{info}\n"; check("int f1(int i, bool b) {\n" " int j = b ? i : 0;\n" " return 1 / j;\n" "}\n" "int f2(int i, bool b) {\n" " int j = b ? 0 : i;\n" " return 1 / j;\n" "}\n" "int f3(int i, bool b) {\n" " int j = 1;\n" " if (b)\n" " j = 0;\n" " return 1 / j;\n" "}\n", dinit(CheckOptions, $.settings = &s)); ASSERT_EQUALS("[test.cpp:3:14]: warning: Division by zero. [zerodivcond]\n" "[test.cpp:2:13]: note: Assuming condition 'b' is false\n" "[test.cpp:2:15]: note: Assignment 'j=b?i:0', assigned value is 0\n" "[test.cpp:3:14]: note: Division by zero\n" "[test.cpp:7:14]: warning: Division by zero. [zerodivcond]\n" "[test.cpp:6:13]: note: Assuming condition 'b' is true\n" "[test.cpp:6:15]: note: Assignment 'j=b?0:i', assigned value is 0\n" "[test.cpp:7:14]: note: Division by zero\n" "[test.cpp:13:14]: warning: Division by zero. [zerodivcond]\n" "[test.cpp:12:13]: note: Assignment 'j=0', assigned value is 0\n" "[test.cpp:11:9]: note: Assuming condition is true\n" "[test.cpp:13:14]: note: Division by zero\n", errout_str()); } void nanInArithmeticExpression() { check("void f()\n" "{\n" " double x = 3.0 / 0.0 + 1.0;\n" " printf(\"%f\", x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:19]: (style) Using NaN/Inf in a computation. [nanInArithmeticExpression]\n", errout_str()); check("void f()\n" "{\n" " double x = 3.0 / 0.0 - 1.0;\n" " printf(\"%f\", x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:19]: (style) Using NaN/Inf in a computation. [nanInArithmeticExpression]\n", errout_str()); check("void f()\n" "{\n" " double x = 1.0 + 3.0 / 0.0;\n" " printf(\"%f\", x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:25]: (style) Using NaN/Inf in a computation. [nanInArithmeticExpression]\n", errout_str()); check("void f()\n" "{\n" " double x = 1.0 - 3.0 / 0.0;\n" " printf(\"%f\", x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:25]: (style) Using NaN/Inf in a computation. [nanInArithmeticExpression]\n", errout_str()); check("void f()\n" "{\n" " double x = 3.0 / 0.0;\n" " printf(\"%f\", x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope1() { check("unsigned short foo()\n" "{\n" " test_client CClient;\n" " try\n" " {\n" " if (CClient.Open())\n" " {\n" " return 0;\n" " }\n" " }\n" " catch (...)\n" " {\n" " return 2;\n" " }\n" "\n" " try\n" " {\n" " CClient.Close();\n" " }\n" " catch (...)\n" " {\n" " return 2;\n" " }\n" "\n" " return 1;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope2() { check("int foo()\n" "{\n" " Error e;\n" " e.SetValue(12);\n" " throw e;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope3() { check("void foo()\n" "{\n" " int i;\n" " int *p = 0;\n" " if (abc)\n" " {\n" " p = &i;\n" " }\n" " *p = 1;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope4() { check("void foo()\n" "{\n" " int i;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope5() { check("void f(int x)\n" "{\n" " int i = 0;\n" " if (x) {\n" " for ( ; i < 10; ++i) ;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:9]: (style) The scope of the variable 'i' can be reduced. [variableScope]\n", errout_str()); check("void f(int x) {\n" " const unsigned char i = 0;\n" " if (x) {\n" " for ( ; i < 10; ++i) ;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x)\n" "{\n" " int i = 0;\n" " if (x) {b()}\n" " else {\n" " for ( ; i < 10; ++i) ;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:9]: (style) The scope of the variable 'i' can be reduced. [variableScope]\n", errout_str()); } void varScope6() { check("void f(int x)\n" "{\n" " int i = x;\n" " if (a) {\n" " x++;\n" " }\n" " if (b) {\n" " c(i);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #5398 " bool success = false;\n" " int notReducable(someClass.getX(&success));\n" " if (success) {\n" " foo(notReducable);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(Test &test) {\n" " int& x = test.getData();\n" " if (test.process())\n" " x = 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f()\n" "{\n" "int foo = 0;\n" "std::vector vec(10);\n" "BOOST_FOREACH(int& i, vec)\n" "{\n" " foo += 1;\n" " if(foo == 10)\n" " {\n" " return 0;\n" " }\n" "}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int &x)\n" "{\n" " int n = 1;\n" " do\n" " {\n" " ++n;\n" " ++x;\n" " } while (x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope7() { check("void f(int x)\n" "{\n" " int y = 0;\n" " b(y);\n" " if (x) {\n" " y++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope8() { check("void test() {\n" " float edgeResistance=1;\n" " std::vector edges;\n" " BOOST_FOREACH(int edge, edges) {\n" " edgeResistance = (edge+1) / 2.0;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) The scope of the variable 'edgeResistance' can be reduced. [variableScope]\n", errout_str()); } void varScope9() { // classes may have extra side effects check("class fred {\n" "public:\n" " fred();\n" " void x();\n" "};\n" "void test(int a) {\n" " fred f;\n" " if (a == 2) {\n" " f.x();\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class fred {\n" // #2062 "public:\n" " void x();\n" "};\n" "void test(int a) {\n" " fred f;\n" " if (a == 2) {\n" " f.x();\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:10]: (style) The scope of the variable 'f' can be reduced. [variableScope]\n", errout_str()); check("struct S { int a, b; };\n" "bool f() {\n" " S s{};\n" " {\n" " bool b = s.a && a.b;\n" " return b;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:7]: (style) The scope of the variable 's' can be reduced. [variableScope]\n", errout_str()); } void varScope10() { check("int f()\n" "{\n" " int x = 0;\n" " FOR {\n" " foo(x++);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope11() { check("int f() {\n" " int x = 0;\n" " AB ab = { x, 0 };\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f() {\n" " int x = 0;\n" " if (a == 0) { ++x; }\n" " AB ab = { x, 0 };\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f() {\n" " int x = 0;\n" " if (a == 0) { ++x; }\n" " if (a == 1) { AB ab = { x, 0 }; }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope12() { check("void f(int x) {\n" " int i[5];\n" " int* j = y;\n" " if (x)\n" " foo(i);\n" " foo(j);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (style) The scope of the variable 'i' can be reduced. [variableScope]\n", errout_str()); check("void f(int x) {\n" " int i[5];\n" " int* j;\n" " if (x)\n" " j = i;\n" " foo(j);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x) {\n" " const bool b = true;\n" " x++;\n" " if (x == 5)\n" " foo(b);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x) {\n" " const bool b = x;\n" " x++;\n" " if (x == 5)\n" " foo(b);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope13() { // #2770 check("void f() {\n" " int i = 0;\n" " forever {\n" " if (i++ == 42) { break; }\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope14() { // #3941 check("void f() {\n" " const int i( foo());\n" " if(a) {\n" " for ( ; i < 10; ++i) ;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope15() { // #4573 check("void f() {\n" " int a,b,c;\n" " if (a);\n" " else if(b);\n" " else if(c);\n" " else;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); } void varScope16() { check("void f() {\n" " int a = 0;\n" " while((++a) < 56) {\n" " foo();\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int a = 0;\n" " do {\n" " foo();\n" " } while((++a) < 56);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int a = 0;\n" " do {\n" " a = 64;\n" " foo(a);\n" " } while((++a) < 56);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int a = 0;\n" " do {\n" " a = 64;\n" " foo(a);\n" " } while(z());\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (style) The scope of the variable 'a' can be reduced. [variableScope]\n", errout_str()); } void varScope17() { check("void f() {\n" " int x;\n" " if (a) {\n" " x = stuff(x);\n" " morestuff(x);\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (style) The scope of the variable 'x' can be reduced. [variableScope]\n", errout_str()); check("void f() {\n" " int x;\n" " if (a) {\n" " x = stuff(x);\n" " morestuff(x);\n" " }\n" " if (b) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (style) The scope of the variable 'x' can be reduced. [variableScope]\n", errout_str()); } void varScope18() { check("void f() {\n" " short x;\n" "\n" " switch (ab) {\n" " case A:\n" " break;\n" " case B:\n" " default:\n" " break;\n" " }\n" "\n" " if (c) {\n" " x = foo();\n" " do_something(x);\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) The scope of the variable 'x' can be reduced. [variableScope]\n", errout_str()); check("void f() {\n" " short x;\n" "\n" " switch (ab) {\n" " case A:\n" " x = 10;\n" " break;\n" " case B:\n" " default:\n" " break;\n" " }\n" "\n" " if (c) {\n" " x = foo();\n" " do_something(x);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " short x;\n" "\n" " switch (ab) {\n" " case A:\n" " if(c)\n" " do_something(x);\n" " break;\n" " case B:\n" " default:\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) The scope of the variable 'x' can be reduced. [variableScope]\n", errout_str()); check("void f() {\n" " short x;\n" "\n" " switch (ab) {\n" " case A:\n" " if(c)\n" " do_something(x);\n" " break;\n" " case B:\n" " default:\n" " if(d)\n" " do_something(x);\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope20() { // Ticket #5103 - constant variable only used in inner scope check("int f(int a) {\n" " const int x = 234;\n" " int b = a;\n" " if (b > 32) b = x;\n" " return b;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope21() { // Ticket #5382 - initializing two-dimensional array check("int test() {\n" " int test_value = 3;\n" " int test_array[1][1] = { { test_value } };\n" " return sizeof(test_array);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope22() { // Ticket #5684 - "The scope of the variable 'p' can be reduced" - But it can not. check("void foo() {\n" " int* p( 42 );\n" " int i = 0;\n" " while ( i != 100 ) {\n" " *p = i;\n" " ++p;\n" " ++i;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); // try to avoid an obvious false negative after applying the fix for the example above: check("void foo() {\n" " int* p( 42 );\n" " int i = 0;\n" " int dummy = 0;\n" " while ( i != 100 ) {\n" " p = & dummy;\n" " *p = i;\n" " ++p;\n" " ++i;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (style) The scope of the variable 'p' can be reduced. [variableScope]\n", errout_str()); } void varScope23() { // #6154: Don't suggest to reduce scope if inner scope is a lambda check("int main() {\n" " size_t myCounter = 0;\n" " Test myTest([&](size_t aX){\n" " std::cout << myCounter += aX << std::endl;\n" " });\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope24() { check("void f(Foo x) {\n" " Foo &r = x;\n" " if (cond) {\n" " r.dostuff();\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (style) The scope of the variable 'r' can be reduced. [variableScope]\n", errout_str()); check("void f(Foo x) {\n" " Foo foo = x;\n" " if (cond) {\n" " foo.dostuff();\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope25() { check("void f() {\n" " time_t currtime;\n" " if (a) {\n" " currtime = time(&dummy);\n" " if (currtime > t) {}\n" " }\n" "}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:2:12]: (style) The scope of the variable 'currtime' can be reduced. [variableScope]\n", errout_str()); } void varScope26() { check("void f(const std::map &m) {\n" " for (auto it : m) {\n" " if (cond1) {\n" " int& key = it.first;\n" " if (cond2) { dostuff(key); }\n" " }\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope27() { checkP("void f() {\n" " int x = 0;\n" "#ifdef X\n" "#endif\n" " if (id == ABC) { return x; }\n" "}\n"); ASSERT_EQUALS("", errout_str()); checkP("void f() {\n" "#ifdef X\n" "#endif\n" " int x = 0;\n" " if (id == ABC) { return x; }\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:7]: (style) The scope of the variable 'x' can be reduced. [variableScope]\n", errout_str()); } void varScope28() { check("void f() {\n" // #10527 " int i{};\n" " if (double d = g(i); d == 1.0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope29() { // #10888 check("enum E { E0 };\n" "struct S { int i; };\n" "void f(int b) {\n" " enum E e;\n" " struct S s;\n" " if (b) {\n" " e = E0;\n" " s.i = 0;\n" " g(e, s);\n" " }\n" "}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("[test.c:4:12]: (style) The scope of the variable 'e' can be reduced. [variableScope]\n" "[test.c:5:14]: (style) The scope of the variable 's' can be reduced. [variableScope]\n", errout_str()); check("void f(bool b) {\n" " std::string s;\n" " if (b) {\n" " s = \"abc\";\n" " g(s);\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: (style) The scope of the variable 's' can be reduced. [variableScope]\n", errout_str()); check("auto foo(std::vector& vec, bool flag) {\n" " std::vector dummy;\n" " std::vector::iterator iter;\n" " if (flag)\n" " iter = vec.begin();\n" " else {\n" " dummy.push_back(42);\n" " iter = dummy.begin();\n" " }\n" " return *iter;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:28]: (style) Parameter 'vec' can be declared as reference to const [constParameterReference]\n", errout_str()); check("auto& foo(std::vector& vec, bool flag) {\n" " std::vector dummy;\n" " std::vector::iterator iter;\n" " if (flag)\n" " iter = vec.begin();\n" " else {\n" " dummy.push_back(42);\n" " iter = dummy.begin();\n" " }\n" " return *iter;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope30() { // #8541 check("bool f(std::vector& v, int i) {\n" " int n = 0;\n" " bool b = false;\n" " std::for_each(v.begin(), v.end(), [&](int j) {\n" " if (j == i) {\n" " ++n;\n" " if (n > 5)\n" " b = true;\n" " }\n" " });\n" " return b;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope31() { // #11099 check("bool g(std::vector&);\n" "void h(std::vector);\n" "void f0(std::vector v) {\n" " std::vector w{ v };\n" " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f1(std::vector v) {\n" " std::vector w{ v.begin(), v.end() };\n" " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f2(std::vector v) {\n" " std::vector w{ 10, 0, std::allocator() };\n" // FN " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f3(std::vector v) {\n" " std::vector w{ 10, 0 };\n" // warn " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f4(std::vector v) {\n" " std::vector w{ 10 };\n" // warn " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f5(std::vector v) {\n" " std::vector w(v);\n" " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f6(std::vector v) {\n" " std::vector w(v.begin(), v.end());\n" " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f7(std::vector v) {\n" " std::vector w(10, 0, std::allocator);\n" // FN " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f8(std::vector v) {\n" " std::vector w(10, 0);\n" // warn " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f9(std::vector v) {\n" " std::vector w(10);\n" // warn " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n" "void f10(std::vector v) {\n" " std::vector w{};\n" // warn " bool b = g(v);\n" " if (b)\n" " h(w);\n" " h(v);\n" "}\n"); ASSERT_EQUALS("[test.cpp:25:22]: (style) The scope of the variable 'w' can be reduced. [variableScope]\n" "[test.cpp:32:22]: (style) The scope of the variable 'w' can be reduced. [variableScope]\n" "[test.cpp:60:22]: (style) The scope of the variable 'w' can be reduced. [variableScope]\n" "[test.cpp:67:22]: (style) The scope of the variable 'w' can be reduced. [variableScope]\n" "[test.cpp:74:22]: (style) The scope of the variable 'w' can be reduced. [variableScope]\n", errout_str()); } void varScope32() { // #11441 check("template \n" "std::vector g(F, const std::vector&);\n" "void f(const std::vector&v) {\n" " std::vector w;\n" " for (auto x : v)\n" " w = g([&]() { x; }, w);\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:23]: (warning) Unused variable value 'x' [constStatement]\n", errout_str()); } void varScope33() { // #11131 check("struct S {\n" " const std::string& getStr() const;\n" " void mutate();\n" " bool getB() const;\n" "};\n" "void g(S& s) {\n" " std::string str = s.getStr();\n" " s.mutate();\n" " if (s.getB()) {\n" " if (str == \"abc\") {}\n" " }\n" "}\n" "void g(char* s, bool b) {\n" " int i = strlen(s);\n" " s[0] = '\\0';\n" " if (b) {\n" " if (i == 5) {}\n" " }\n" "}\n" "void f(const S& s) {\n" " std::string str = s.getStr();\n" " std::string str2{ s.getStr() };\n" " std::string str3(s.getStr());\n" " if (s.getB()) {\n" " if (str == \"abc\") {}\n" " if (str2 == \"abc\") {}\n" " if (str3 == \"abc\") {}\n" " }\n" "}\n" "void f(const char* s, bool b) {\n" " int i = strlen(s);\n" " if (b) {\n" " if (i == 5) {}\n" " }\n" "}\n" "void f(int j, bool b) {\n" " int k = j;\n" " if (b) {\n" " if (k == 5) {}\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:21:17]: (style) The scope of the variable 'str' can be reduced. [variableScope]\n" "[test.cpp:22:17]: (style) The scope of the variable 'str2' can be reduced. [variableScope]\n" "[test.cpp:23:17]: (style) The scope of the variable 'str3' can be reduced. [variableScope]\n" "[test.cpp:31:9]: (style) The scope of the variable 'i' can be reduced. [variableScope]\n" "[test.cpp:37:9]: (style) The scope of the variable 'k' can be reduced. [variableScope]\n", errout_str()); } void varScope34() { // #11742 check("void f() {\n" " bool b = false;\n" " int i = 1;\n" " for (int k = 0; k < 20; ++k) {\n" " b = !b;\n" " if (b)\n" " i++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope35() { // #11845 check("void f(int err, const char* src) {\n" " const char* msg = \"Success\";\n" " char buf[42];\n" " if (err != 0)\n" " msg = strcpy(buf, src);\n" " printf(\"%d: %s\\n\", err, msg);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("char* g(char* dst, const char* src);\n" "void f(int err, const char* src) {\n" " const char* msg = \"Success\";\n" " char buf[42];\n" " if (err != 0)\n" " msg = g(buf, src);\n" " printf(\"%d: %s\\n\", err, msg);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("char* g(char* dst, const char* src);\n" "void f(int err, const char* src) {\n" " const char* msg = \"Success\";\n" " char buf[42];\n" " if (err != 0)\n" " g(buf, src);\n" " printf(\"%d: %s\\n\", err, msg);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:10]: (style) The scope of the variable 'buf' can be reduced. [variableScope]\n", errout_str()); } void varScope36() { // #12158 check("void f( uint32_t value ) {\n" " uint32_t i = 0U;\n" " if ( value > 100U ) { }\n" " else if( value > 50U ) { }\n" " else{\n" " for( i = 0U; i < 5U; i++ ) {}\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:2:14]: (style) The scope of the variable 'i' can be reduced. [variableScope]\n", errout_str()); } void varScope37() { // #12158 check("void f( uint32_t value ) {\n" " uint32_t i = 0U;\n" " if ( value > 100U ) { }\n" " else {\n" " if( value > 50U ) { }\n" " else{\n" " for( i = 0U; i < 5U; i++ ) {}\n" " }\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:2:14]: (style) The scope of the variable 'i' can be reduced. [variableScope]\n", errout_str()); } void varScope38() { checkP("bool dostuff();\n" // #12519 "#define DOSTUFF(c) if (c < 5) { if (c) b = dostuff(); }\n" "#define DOSTUFFEX(c) { bool b = false; DOSTUFF(c); }\n" "void f(int a) {\n" " DOSTUFFEX(a);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope39() { check("struct S {\n" // #12405 " void f(const std::string& s) const;\n" " const int* g(std::string&) const;\n" "};\n" "void h(int);\n" "void S::f(const std::string& s) const {\n" " std::string n = s;\n" " const int* a = g(n);\n" " if (n == \"abc\") {\n" " h(a[0]);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void varScope40() { checkP("#define NUM (-999.9)\n" // #8862 "double f(int i) {\n" " double a = NUM;\n" " double b = -NUM;\n" " double c = -1.0 * NUM;\n" " if (i == 1) {\n" " return a;\n" " }\n" " if (i == 2) {\n" " return b;\n" " }\n" " if (i == 3) {\n" " return c;\n" " }\n" " return 0.0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:12]: (style) The scope of the variable 'a' can be reduced. [variableScope]\n" "[test.cpp:4:12]: (style) The scope of the variable 'b' can be reduced. [variableScope]\n" "[test.cpp:5:12]: (style) The scope of the variable 'c' can be reduced. [variableScope]\n", errout_str()); check("struct S { int a; };\n" // #12618 "int f(const S* s, int i) {\n" " int x = s->a;\n" " const int b[] = { 1, 2, 3 };\n" " int y = b[1];\n" " if (i)\n" " return x + y;\n" " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:9]: (style) The scope of the variable 'x' can be reduced. [variableScope]\n" "[test.cpp:5:9]: (style) The scope of the variable 'y' can be reduced. [variableScope]\n", errout_str()); } void varScope41() { // #11845 check("void get_errmsg(const char **msg, char *buf, size_t bufsiz, int err);\n" "void test(int err)\n" "{\n" " const char *msg = \"Success\";\n" " char buf[42];\n" " if (err != 0)\n" " get_errmsg(&msg, buf, sizeof(buf), err);\n" " printf(\"%d: %s\\n\", err, msg);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void get_errmsg(char *buf, size_t bufsiz, int err);\n" "void test(int err)\n" "{\n" " const char *msg = \"Success\";\n" " char buf[42];\n" " if (err != 0)\n" " get_errmsg(buf, sizeof(buf), err);\n" " printf(\"%d: %s\\n\", err, msg);\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:10]: (style) The scope of the variable 'buf' can be reduced. [variableScope]\n", errout_str()); } void varScope42() { check("void f(const char **, char *);\n" "void g(int e) {\n" " const char *msg = \"Something\";\n" " char buf[42];\n" " if (e != 0)\n" " f(&msg, buf);\n" " printf(\"result: %s\\n\", msg);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(char *, char *);\n" "void g(int e) {\n" " char msg [42] = \"Something\";\n" " char buf[42];\n" " if (e != 0)\n" " f(msg, buf);\n" " printf(\"result: %s\\n\", msg);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const char *, char *);\n" "void g(int e) {\n" " const char *msg = \"Something\";\n" " char buf[42];\n" " if (e != 0)\n" " f(msg, buf);\n" " printf(\"result: %s\\n\", msg);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:10]: (style) The scope of the variable 'buf' can be reduced. [variableScope]\n", errout_str()); check("void f(int **, char *);\n" "void g(int e) {\n" " int *msg = calloc(0, sizeof(*msg));\n" " char buf[42];\n" " if (e != 0)\n" " f(&msg, buf);\n" " printf(\"result: %d\\n\", *msg);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:10]: (style) The scope of the variable 'buf' can be reduced. [variableScope]\n", errout_str()); check("void f(const char *&, const char *&);\n" "void g(int e) {\n" " const char *msg = \"Something\";\n" " char *buf = malloc(42);\n" " if (e != 0)\n" " f(msg, buf);\n" " printf(\"result: %d\\n\", msg);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void g(const char* format, ...);\n" "void f(bool b) {\n" " const char* s = \"abc\";\n" " if (b)\n" " g(\"%d %s\", 1, s);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:17]: (style) The scope of the variable 's' can be reduced. [variableScope]\n", errout_str()); } void varScope43() { check("struct S { int a, b; };\n" // #13838 "int f(S s) {\n" " auto& [x, y] = s;\n" " if (x < 5) {\n" " return y;\n" " }\n" " return 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int a; };\n" "int f(S s) {\n" " auto& [x] = s;\n" " if (y) {\n" " return x;\n" " }\n" " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:12]: (style) The scope of the variable 'x' can be reduced. [variableScope]\n", errout_str()); } void varScope44() { // #14496 check("char* f() {\n" " char* p = nullptr;\n" " {\n" " p = strdup(\"abc\");\n" " if (p) {\n" " return p;\n" " }\n" " }\n" " return nullptr;\n" "}\n" "char* g() {\n" " char* q = NULL;\n" " {\n" " q = strdup(\"abc\");\n" " if (q) {\n" " return q;\n" " }\n" " }\n" " return nullptr;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) The scope of the variable 'p' can be reduced. [variableScope]\n" "[test.cpp:12:11]: (style) The scope of the variable 'q' can be reduced. [variableScope]\n", errout_str()); } void varScope45() { check("void g(int x, int y) {\n" // #14497 " int a = x, b = y;\n" " if (a) {}\n" " else {\n" " if (b) {}\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) The scope of the variable 'b' can be reduced. [variableScope]\n", errout_str()); } void varScope46() { check("void f() {\n" // #7091 " int y1;\n" " for (int i = 0; i < 3; ++i) {\n" " for(int j = 0; j < 3; ++j) {\n" " y1 = 2 * 1;\n" " y1 += 1;\n" " }\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (style) The scope of the variable 'y1' can be reduced. [variableScope]\n", errout_str()); check("bool f() {\n" "bool b = false;\n" "do {\n" " switch (g()) {\n" " case 0:\n" " b = true;\n" " break;\n" " case 1:\n" " return b;\n" " break;\n" " default:\n" " break;\n" " }\n" "}\n" "while (true);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int y1 = 0;\n" " for (int i = 0; i < 3; ++i) {\n" " for(int j = 0; j < 3; ++j) {\n" " y1 = y1 + 1;\n" " dostuff(y1);\n" " }\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int& r) {\n" // #14566 " int i = 0;\n" " while (g()) {\n" " {\n" " if (g()) {\n" " i = 0;" " std::swap(i, r);\n" " }\n" " }\n" " }\n" " use(i);" "}\n"); ASSERT_EQUALS("", errout_str()); } #define checkOldStylePointerCast(...) checkOldStylePointerCast_(__FILE__, __LINE__, __VA_ARGS__) template void checkOldStylePointerCast_(const char* file, int line, const char (&code)[size], Standards::cppstd_t std = Standards::CPPLatest) { const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).cpp(std).build(); // Tokenize.. SimpleTokenizer tokenizerCpp(settings, *this); ASSERT_LOC(tokenizerCpp.tokenize(code), file, line); CheckOtherImpl checkOtherCpp(&tokenizerCpp, settings, *this); checkOtherCpp.warningOldStylePointerCast(); checkOtherCpp.warningDangerousTypeCast(); } void oldStylePointerCast() { checkOldStylePointerCast("class Base{};\n" "class Derived: public Base {};\n" "void foo(Base* base)\n" "{\n" " Derived * d = (Derived *) base;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:19]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); checkOldStylePointerCast("class Base{};\n" "class Derived: public Base {};\n" "void foo(Derived* derived)\n" "{\n" " Base * b = (Base *) derived;\n" // <- cast from derived to base is safe => cstyleCast "}\n"); ASSERT_EQUALS("[test.cpp:5:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); checkOldStylePointerCast("void foo(Base* base)\n" "{\n" " Derived * d = (Derived *) base;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:19]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); checkOldStylePointerCast("class Base{};\n" "class Derived: public Base {};\n" "void foo(Base* base)\n" "{\n" " Derived * d = (const Derived *) base;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:19]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); checkOldStylePointerCast("class Base{};\n" "class Derived: public Base {};\n" "void foo()\n" "{\n" " Derived * d = (const Derived *) ( new Base() );\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:19]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); checkOldStylePointerCast("class Base{};\n" "class Derived: public Base {};\n" "void foo()\n" "{\n" " Derived * d = (const Derived *) new Base();\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:19]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); checkOldStylePointerCast("class Base{};\n" "void foo()\n" "{\n" " Base * b = (Base *) new short[10];\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:16]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); checkOldStylePointerCast("class Base;\n" "void foo()\n" "{\n" " Base * b = (volatile Base *) derived;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); checkOldStylePointerCast("class Base;\n" "void foo()\n" "{\n" " Base * b = (volatile Base * const) derived;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); checkOldStylePointerCast("class Base;\n" "void foo()\n" "{\n" " Base * b = (const volatile Base *) derived;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); checkOldStylePointerCast("class Base;\n" "void foo()\n" "{\n" " Base * b = (const volatile Base * const) derived;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); checkOldStylePointerCast("class Base;\n" "void foo()\n" "{\n" " Base * b = (const Base *) ( new Derived() );\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); checkOldStylePointerCast("class Base;\n" "void foo()\n" "{\n" " Base * b = (const Base *) new Derived();\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); checkOldStylePointerCast("class Base;\n" "void foo()\n" "{\n" " Base * b = (const Base *) new short[10];\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:16]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); checkOldStylePointerCast("class B;\n" "class A\n" "{\n" " virtual void abc(B *) const = 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); checkOldStylePointerCast("class B;\n" "class A\n" "{\n" " virtual void abc(const B *) const = 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #3630 checkOldStylePointerCast("class SomeType{};\n" "class X : public Base {\n" " X() : Base((SomeType*)7) {}\n" // <- intToPointerCast "};\n"); ASSERT_EQUALS("", errout_str()); checkOldStylePointerCast("class SomeType{};\n" "class X : public Base {\n" " X() : Base((SomeType*)0x7000) {}\n" // <- it's common in embedded code to cast address "};\n"); ASSERT_EQUALS("", errout_str()); checkOldStylePointerCast("class SomeType;\n" "class X : public Base {\n" " X() : Base((SomeType*)var) {}\n" "};\n"); ASSERT_EQUALS("[test.cpp:3:16]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); checkOldStylePointerCast("class SomeType;\n" "class X : public Base {\n" " X() : Base((SomeType*)0) {}\n" "};\n"); ASSERT_EQUALS("", errout_str()); // #5560 checkOldStylePointerCast("class C;\n" "\n" "class B\n" "{ virtual G* createGui(S*, C*) const = 0; };\n" "\n" "class MS : public M\n" "{ virtual void addController(C*) override {} };\n", Standards::CPP03); ASSERT_EQUALS("", errout_str()); // #6164 checkOldStylePointerCast("class Base {};\n" "class Derived: public Base {};\n" "void testCC() {\n" " std::vector v;\n" " v.push_back((Base*)new Derived);\n" "}\n"); // FIXME write a dangerousTypeCast warning instead ASSERT_EQUALS("[test.cpp:5:15]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); // #7709 checkOldStylePointerCast("typedef struct S S;\n" "typedef struct S SS;\n" "typedef class C C;\n" "typedef long LONG;\n" "typedef long* LONGP;\n" "struct T {};\n" "typedef struct T TT;\n" "typedef struct T2 {} TT2;\n" "void f(int* i) {\n" " S* s = (S*)i;\n" " SS* ss = (SS*)i;\n" " struct S2* s2 = (struct S2*)i;\n" " C* c = (C*)i;\n" " class C2* c2 = (class C2*)i;\n" " long* l = (long*)i;\n" " LONG* l2 = (LONG*)i;\n" " LONGP l3 = (LONGP)i;\n" " TT* tt = (TT*)i;\n" " TT2* tt2 = (TT2*)i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:10:12]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:11:14]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:12:21]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:13:12]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:14:20]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:15:15]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:16:16]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:17:16]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:18:14]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:19:16]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); // #8649 checkOldStylePointerCast("struct S {};\n" "void g(S*& s);\n" "void f(uintptr_t i) {\n" " g((S*&)i);\n" " S*& r = (S*&)i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:7]: (style) C-style pointer casting [cstyleCast]\n" "[test.cpp:5:13]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); checkOldStylePointerCast("struct S {};\n" "void g(S*& s);\n" "void f(uint8_t i) {\n" " g((S*&)i);\n" " S*& r = (S*&)i;\n" "}\n"); // TODO: these conversions are dangerous, but it's a different issue not covered by cstyleCast. A separate checker can be added which is executed for both C and C++ code. // clang says: 1.cpp:5:18: warning: cast to 'unsigned char *' from smaller integer type 'uint8_t' (aka 'unsigned char') [-Wint-to-pointer-cast] ASSERT_EQUALS("[test.cpp:4:7]: (style) C-style pointer casting [cstyleCast]\n" "[test.cpp:5:13]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); // #10823 checkOldStylePointerCast("void f(void* p) {\n" " auto h = reinterpret_cast(p);\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #5210 checkOldStylePointerCast("class Base {};\n" "class Derived: public Base {};\n" "void f(Base** b1, Base*** b2) {\n" " Derived** p1 = (Derived**)b1;\n" " Derived*** p2 = (Derived***)b2;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:20]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:5:21]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); // #12446 checkOldStylePointerCast("namespace N { struct S {}; }\n" "union U {\n" " int i;\n" " char c[4];\n" "};\n" "void f(void* p) {\n" " auto ps = (N::S*)p;\n" " auto pu = (union U*)p;\n" " auto pv = (std::vector*)(p);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:15]: (style) C-style pointer casting [cstyleCast]\n" "[test.cpp:8:15]: (style) C-style pointer casting [cstyleCast]\n" "[test.cpp:9:15]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); // #12447 checkOldStylePointerCast("class Base {};\n" "class Derived: public Base {};\n" "void f(const Base& base) {\n" " d = (const Derived&)base;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:7]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); // #11430 checkOldStylePointerCast("struct B {\n" " float* data() const;\n" "};\n" "namespace N {\n" " bool f(float* v);\n" "}\n" "bool g(B& b) {\n" " using float_ptr = float*;\n" " return N::f(float_ptr(b.data()));\n" // <- the cast is safe "}\n"); ASSERT_EQUALS("[test.cpp:9:17]: (style) C-style pointer casting [cstyleCast]\n", errout_str()); } #define checkIntToPointerCast(...) checkIntToPointerCast_(__FILE__, __LINE__, __VA_ARGS__) template void checkIntToPointerCast_(const char* file, int line, const char (&code)[size]) { const Settings settings = settingsBuilder().severity(Severity::portability).build(); // Tokenize.. SimpleTokenizer tokenizerCpp(settings, *this); ASSERT_LOC(tokenizerCpp.tokenize(code), file, line); CheckOtherImpl checkOtherCpp(&tokenizerCpp, settings, *this); checkOtherCpp.warningIntToPointerCast(); } void intToPointerCast() { // #3630 checkIntToPointerCast("uint8_t* ptr = (uint8_t*)7;\n"); ASSERT_EQUALS("[test.cpp:1:16]: (portability) Casting non-zero decimal integer literal to pointer. [intToPointerCast]\n", errout_str()); checkIntToPointerCast("void* ptr = (void*)7;\n"); ASSERT_EQUALS("[test.cpp:1:13]: (portability) Casting non-zero decimal integer literal to pointer. [intToPointerCast]\n", errout_str()); checkIntToPointerCast("uint8_t* ptr = (uint8_t*)0;\n"); ASSERT_EQUALS("", errout_str()); checkIntToPointerCast("uint8_t* ptr = (uint8_t*)0x7000;\n"); // <- it's common in embedded code to cast address ASSERT_EQUALS("", errout_str()); checkIntToPointerCast("struct S { int i; };\n" // #13886, don't crash "int f() { return sizeof(((struct S*)0)->i); }\n"); ASSERT_EQUALS("", errout_str()); checkIntToPointerCast("auto p = (int*)0b10;\n"); // #14180 ASSERT_EQUALS("[test.cpp:1:10]: (portability) Casting non-zero binary integer literal to pointer. [intToPointerCast]\n", errout_str()); } struct CheckInvalidPointerCastOptions { bool portability = true; bool inconclusive = false; }; #define checkInvalidPointerCast(...) checkInvalidPointerCast_(__FILE__, __LINE__, __VA_ARGS__) template void checkInvalidPointerCast_(const char* file, int line, const char (&code)[size], const CheckInvalidPointerCastOptions& opt = make_default_obj{}) { /*const*/ Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability, opt.portability).certainty(Certainty::inconclusive, opt.inconclusive).build(); settings.platform.defaultSign = 's'; // Tokenize.. SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); CheckOtherImpl checkOtherCpp(&tokenizer, settings, *this); checkOtherCpp.invalidPointerCast(); } void invalidPointerCast() { checkInvalidPointerCast("void test() {\n" " float *f = new float[10];\n" " delete [] (double*)f;\n" " delete [] (long double const*)(new float[10]);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:15]: (portability) Casting between float * and double * which have an incompatible binary data representation. [invalidPointerCast]\n" "[test.cpp:4:15]: (portability) Casting between float * and const long double * which have an incompatible binary data representation. [invalidPointerCast]\n", errout_str()); checkInvalidPointerCast("void test(const float* f) {\n" " double *d = (double*)f;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: (portability) Casting between const float * and double * which have an incompatible binary data representation. [invalidPointerCast]\n", errout_str()); checkInvalidPointerCast("void test(double* d1) {\n" " long double *ld = (long double*)d1;\n" " double *d2 = (double*)ld;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:23]: (portability) Casting between double * and long double * which have an incompatible binary data representation. [invalidPointerCast]\n" "[test.cpp:3:18]: (portability) Casting between long double * and double * which have an incompatible binary data representation. [invalidPointerCast]\n", errout_str()); checkInvalidPointerCast("char* test(int* i) {\n" " long double *d = (long double*)(i);\n" " double *d = (double*)(i);\n" " float *f = reinterpret_cast(i);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:22]: (portability) Casting between signed int * and long double * which have an incompatible binary data representation. [invalidPointerCast]\n" "[test.cpp:3:17]: (portability) Casting between signed int * and double * which have an incompatible binary data representation. [invalidPointerCast]\n" "[test.cpp:4:16]: (portability) Casting between signed int * and float * which have an incompatible binary data representation. [invalidPointerCast]\n", errout_str()); checkInvalidPointerCast("float* test(unsigned int* i) {\n" " return (float*)i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:12]: (portability) Casting between unsigned int * and float * which have an incompatible binary data representation. [invalidPointerCast]\n", errout_str()); checkInvalidPointerCast("float* test(unsigned int* i) {\n" " return (float*)i[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); checkInvalidPointerCast("float* test(double& d) {\n" " return (float*)&d;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:12]: (portability) Casting between double * and float * which have an incompatible binary data representation. [invalidPointerCast]\n", errout_str()); checkInvalidPointerCast("void test(float* data) {\n" " f.write((char*)data,sizeof(float));\n" "}\n"); ASSERT_EQUALS("", errout_str()); checkInvalidPointerCast("void test(float* data) {\n" " f.write((char*)data,sizeof(float));\n" "}\n", dinit(CheckInvalidPointerCastOptions, $.inconclusive = true)); // #3639 ASSERT_EQUALS("[test.cpp:2:13]: (portability, inconclusive) Casting from float * to char * is not portable due to different binary data representations on different platforms. [invalidPointerCast]\n", errout_str()); checkInvalidPointerCast("long long* test(float* f) {\n" " return (long long*)f;\n" "}\n", dinit(CheckInvalidPointerCastOptions, $.portability = false)); ASSERT_EQUALS("", errout_str()); checkInvalidPointerCast("long long* test(float* f, char* c) {\n" " foo((long long*)f);\n" " return reinterpret_cast(c);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (portability) Casting from float * to signed long long * is not portable due to different binary data representations on different platforms. [invalidPointerCast]\n", errout_str()); checkInvalidPointerCast("Q_DECLARE_METATYPE(int*)\n"); // #4135 - don't crash } void passedByValue() { check("void f(const std::string str) {}\n"); ASSERT_EQUALS("[test.cpp:1:26]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(std::unique_ptr<:string> ptr) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::shared_ptr<:string> ptr) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::function ptr) {}\n"); ASSERT_EQUALS("", errout_str()); { check("void f(const std::pair x) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::pair<:string> x) {}\n"); TODO_ASSERT_EQUALS("error", "", errout_str()); } check("void f(const std::string::size_type x) {}\n"); ASSERT_EQUALS("", errout_str()); check("class Foo;\nvoid f(const Foo foo) {}\n"); // Unknown class ASSERT_EQUALS("[test.cpp:2:18]: (performance, inconclusive) Function parameter 'foo' should be passed by const reference. [passedByValue]\n", errout_str()); check("class Foo { std::vector v; };\nvoid f(const Foo foo) {}\n"); // Large class (STL member) ASSERT_EQUALS("[test.cpp:2:18]: (performance) Function parameter 'foo' should be passed by const reference. [passedByValue]\n", errout_str()); check("class Foo { int i; };\nvoid f(const Foo foo) {}\n"); // Small class ASSERT_EQUALS("", errout_str()); check("class Foo { int i[6]; };\nvoid f(const Foo foo) {}\n"); // Large class (array) ASSERT_EQUALS("[test.cpp:2:18]: (performance) Function parameter 'foo' should be passed by const reference. [passedByValue]\n", errout_str()); check("class Foo { std::string* s; };\nvoid f(const Foo foo) {}\n"); // Small class (pointer) ASSERT_EQUALS("", errout_str()); check("class Foo { static std::string s; };\nvoid f(const Foo foo) {}\n"); // Small class (static member) ASSERT_EQUALS("", errout_str()); check("class X { std::string s; }; class Foo : X { };\nvoid f(const Foo foo) {}\n"); // Large class (inherited) ASSERT_EQUALS("[test.cpp:2:18]: (performance) Function parameter 'foo' should be passed by const reference. [passedByValue]\n", errout_str()); check("class X { std::string s; }; class Foo { X x; };\nvoid f(const Foo foo) {}\n"); // Large class (inherited) ASSERT_EQUALS("[test.cpp:2:18]: (performance) Function parameter 'foo' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(const std::string &str) {}\n"); ASSERT_EQUALS("", errout_str()); // The idiomatic way of passing a std::string_view is by value check("void f(const std::string_view str) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::string_view str) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::string_view &str) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::vector v) {}\n"); ASSERT_EQUALS("[test.cpp:1:31]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(const std::vector<:string> v) {}\n"); ASSERT_EQUALS("[test.cpp:1:39]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(const std::vector<:string>::size_type s) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::vector &v) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::map &v) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::map v) {}\n"); ASSERT_EQUALS("[test.cpp:1:32]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(const std::map<:string> v) {}\n"); ASSERT_EQUALS("[test.cpp:1:48]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(const std::map v) {}\n"); ASSERT_EQUALS("[test.cpp:1:40]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(const std::map<:string> v) {}\n"); ASSERT_EQUALS("[test.cpp:1:40]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(const std::streamoff pos) {}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::initializer_list i) {}\n"); ASSERT_EQUALS("", errout_str()); // #5824 check("void log(const std::string& file, int line, const std::string& function, const std::string str, ...) {}\n"); ASSERT_EQUALS("", errout_str()); // #5534 check("struct float3 { };\n" "typedef float3 vec;\n" "class Plane {\n" " vec Refract(vec &vec) const;\n" " bool IntersectLinePlane(const vec &planeNormal);\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("class X {\n" " virtual void func(const std::string str) {}\n" "};\n"); ASSERT_EQUALS("[test.cpp:2:41]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("enum X;\n" "void foo(X x1){}\n"); ASSERT_EQUALS("", errout_str()); check("enum X { a, b, c };\n" "void foo(X x2){}\n"); ASSERT_EQUALS("", errout_str()); check("enum X { a, b, c };\n" "enum X;" "void foo(X x3){}\n"); ASSERT_EQUALS("", errout_str()); check("enum X;\n" "enum X { a, b, c };\n" "void foo(X x4){}\n"); ASSERT_EQUALS("", errout_str()); check("union U {\n" " char* pc;\n" " short* ps;\n" " int* pi;\n" "};\n" "void f(U u) {}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { char A[8][8]; };\n" "void f(S s) {}\n"); ASSERT_EQUALS("[test.cpp:2:10]: (performance) Function parameter 's' should be passed by const reference. [passedByValue]\n", errout_str()); check("union U {\n" // don't crash " int a;\n" " decltype(nullptr) b;\n" "};\n" "int* f(U u) { return u.b; }\n"); ASSERT_EQUALS("", errout_str()); check("struct B { virtual int f(std::string s) = 0; };\n" // #11432 "struct D1 : B {\n" " int f(std::string s) override { s += 'a'; return s.size(); }\n" "}\n" "struct D2 : B {\n" " int f(std::string s) override { return s.size(); }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int x(int);\n" "void f(std::vector v, int& j) {\n" " for (int i : v)\n" " j = i;\n" "}\n" "void fn(std::vector v) {\n" " for (int& i : v)\n" " i = x(i);\n" "}\n" "void g(std::vector v, int& j) {\n" " for (int i = 0; i < v.size(); ++i)\n" " j = v[i];\n" "}\n" "void gn(std::vector v) {\n" " for (int i = 0; i < v.size(); ++i)\n" " v[i] = x(i);\n" "}\n" "void h(std::vector<:vector>> v, int& j) {\n" " for (int i = 0; i < v.size(); ++i)\n" " j = v[i][0];\n" "}\n" "void hn(std::vector<:vector>> v) {\n" " for (int i = 0; i < v.size(); ++i)\n" " v[i][0] = x(i);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:25]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n" "[test.cpp:10:25]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n" "[test.cpp:18:38]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("struct S {\n" // #11995 " explicit S(std::string s) noexcept;\n" " std::string m;\n" "};\n" "S::S(std::string s) noexcept : m(std::move(s)) {}\n" "struct T {\n" " explicit S(std::string s) noexcept(true);\n" " std::string m;\n" "};\n" "T::T(std::string s) noexcept(true) : m(std::move(s)) {}\n"); ASSERT_EQUALS("", errout_str()); check("namespace N {\n" // #12086 " void g(int);\n" "}\n" "void f(std::vector v) {\n" " N::g(v[0]);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:25]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(const std::string& s, std::string t) {\n" // #12083 " const std::string& v = !s.empty() ? s : t;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:42]: (performance) Function parameter 't' should be passed by const reference. [passedByValue]\n", errout_str()); /*const*/ Settings settingsUnix64 = settingsBuilder(settings0).platform(Platform::Type::Unix64).build(); check("struct S {\n" // #12138 " union {\n" " int a = 0;\n" " int x;\n" " };\n" " union {\n" " int b = 0;\n" " int y;\n" " };\n" " union {\n" " int c = 0;\n" " int z;\n" " };\n" "};\n" "void f(S s) {\n" " if (s.x > s.y) {}\n" "}\n", dinit(CheckOptions, $.settings = &settingsUnix64)); ASSERT_EQUALS("", errout_str()); check("struct S { std::list l; };\n" // #12147 "class C { public: std::list l; };\n" "bool f(S s) {\n" " return s.l.empty();\n" "}\n" "bool f(C c) {\n" " return c.l.empty();\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:10]: (performance) Function parameter 's' should be passed by const reference. [passedByValue]\n" "[test.cpp:6:10]: (performance) Function parameter 'c' should be passed by const reference. [passedByValue]\n", errout_str()); check("struct S { std::list a[1][1]; };\n" "bool f(S s) {\n" " return s.a[0][0].empty();\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:10]: (performance) Function parameter 's' should be passed by const reference. [passedByValue]\n", errout_str()); check("struct S {\n" " enum class E : std::uint8_t { E0 };\n" " static void f(S::E e) {\n" " if (e == S::E::E0) {}\n" " }\n" " char a[20];\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::vector v[2]);\n" // #13052 "void g(const std::vector v[2]);\n" "void g(const std::vector v[2]) {}\n" "int h(const std::array<:vector>, 2> a) { return a[0][0]; }\n"); ASSERT_EQUALS("[test.cpp:4:45]: (performance) Function parameter 'a' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(const std::array a[]) {}\n"); // #13524 ASSERT_EQUALS("", errout_str()); check("struct S {\n" // #10733 " std::vector v;\n" " using F = void (*)();\n" " void func(F f);\n" "};\n" "void S::func(S::F f) {}\n"); ASSERT_EQUALS("", errout_str()); /*const*/ Settings settingsWin64 = settingsBuilder().platform(Platform::Type::Win64).build(); check("using ui64 = unsigned __int64;\n" "ui64 Test(ui64 one, ui64 two) { return one + two; }\n", dinit(CheckOptions, $.settings = &settingsWin64)); ASSERT_EQUALS("", errout_str()); } void passedByValue_nonConst() { check("void f(std::string str) {}\n"); ASSERT_EQUALS("[test.cpp:1:20]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(std::string str) {\n" " return str + x;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:20]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(std::string str) {\n" " std::cout << str;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:20]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(std::string str) {\n" " std::cin >> str;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::string str) {\n" " std::string s2 = str;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: (performance, inconclusive) Use const reference for 's2' to avoid unnecessary data copying. [redundantCopyLocalConst]\n" "[test.cpp:1:20]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(std::string str) {\n" " std::string& s2 = str;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:20]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n" "[test.cpp:2:18]: (style) Variable 's2' can be declared as reference to const [constVariableReference]\n", errout_str()); check("void f(std::string str) {\n" " const std::string& s2 = str;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:20]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("void f(std::string str) {\n" " str = \"\";\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::string str) {\n" " foo(str);\n" // It could be that foo takes str as non-const-reference "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(const std::string& str);\n" "void f(std::string str) {\n" " foo(str);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:20]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("void foo(std::string str);\n" "void f(std::string str) {\n" " foo(str);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:20]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("void foo(std::string& str);\n" "void f(std::string str) {\n" " foo(str);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(std::string* str);\n" "void f(std::string str) {\n" " foo(&str);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int& i1, const std::string& str, int& i2);\n" "void f(std::string str) {\n" " foo((a+b)*c, str, x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:20]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("std::string f(std::string str) {\n" " str += x;\n" " return str;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class X {\n" " std::string s;\n" " void func() const;\n" "};\n" "Y f(X x) {\n" " x.func();\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:7]: (performance) Function parameter 'x' should be passed by const reference. [passedByValue]\n", errout_str()); check("class X {\n" " void func();\n" "};\n" "Y f(X x) {\n" " x.func();\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class X {\n" " void func(std::string str) {}\n" "};\n"); ASSERT_EQUALS("[test.cpp:2:27]: (performance) Function parameter 'str' should be passed by const reference. [passedByValue]\n", errout_str()); check("class X {\n" " virtual void func(std::string str) {}\n" // Do not warn about virtual functions, if 'str' is not declared as const "};\n"); ASSERT_EQUALS("", errout_str()); check("class X {\n" " char a[1024];\n" "};\n" "class Y : X {\n" " char b;\n" "};\n" "void f(Y y) {\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:10]: (performance) Function parameter 'y' should be passed by const reference. [passedByValue]\n", errout_str()); check("class X {\n" " void* a;\n" " void* b;\n" "};\n" "class Y {\n" " void* a;\n" " void* b;\n" " char c;\n" "};\n" "void f(X x, Y y) {\n" "}\n"); ASSERT_EQUALS("[test.cpp:10:15]: (performance) Function parameter 'y' should be passed by const reference. [passedByValue]\n", errout_str()); { // 8-byte data should be passed by const reference on 32-bit platform but not on 64-bit platform const char code[] = "class X {\n" " uint64_t a;\n" " uint64_t b;\n" "};\n" "void f(X x) {}\n"; /*const*/ Settings s32 = settingsBuilder(settings1).platform(Platform::Type::Unix32).build(); check(code, dinit(CheckOptions, $.settings = &s32)); ASSERT_EQUALS("[test.cpp:5:10]: (performance) Function parameter 'x' should be passed by const reference. [passedByValue]\n", errout_str()); /*const*/ Settings s64 = settingsBuilder(settings1).platform(Platform::Type::Unix64).build(); check(code, dinit(CheckOptions, $.settings = &s64)); ASSERT_EQUALS("", errout_str()); } check("Writer* getWriter();\n" "\n" "void foo(Buffer& buffer) {\n" " getWriter()->operator<<(buffer);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void passedByValue_externC() { check("struct X { int a[5]; }; void f(X v) { }\n"); ASSERT_EQUALS("[test.cpp:1:34]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("extern \"C\" { struct X { int a[5]; }; void f(X v) { } }\n"); ASSERT_EQUALS("", errout_str()); check("struct X { int a[5]; }; extern \"C\" void f(X v) { }\n"); ASSERT_EQUALS("", errout_str()); check("struct X { int a[5]; }; void f(const X v);\n"); ASSERT_EQUALS("", errout_str()); check("struct X { int a[5]; }; void f(const X v) { (void) v; }\n"); ASSERT_EQUALS("[test.cpp:1:40]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("extern \"C\" { struct X { int a[5]; }; void f(const X v); }\n"); ASSERT_EQUALS("", errout_str()); check("struct X { int a[5]; }; extern \"C\" void f(const X v) { }\n"); ASSERT_EQUALS("", errout_str()); } void constVariable() { check("int f(std::vector x) {\n" " int& i = x[0];\n" " return i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:24]: (performance) Function parameter 'x' should be passed by const reference. [passedByValue]\n" "[test.cpp:2:10]: (style) Variable 'i' can be declared as reference to const [constVariableReference]\n", errout_str()); check("int f(std::vector& x) {\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:25]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("int f(std::vector x) {\n" " const int& i = x[0];\n" " return i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:24]: (performance) Function parameter 'x' should be passed by const reference. [passedByValue]\n", errout_str()); check("int f(std::vector x) {\n" " static int& i = x[0];\n" " return i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:24]: (performance) Function parameter 'x' should be passed by const reference. [passedByValue]\n", errout_str()); check("int f(std::vector x) {\n" " int& i = x[0];\n" " i++;\n" " return i;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int& f(std::vector& x) {\n" " x.push_back(1);\n" " int& i = x[0];\n" " return i;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(const std::vector& x) {\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int& f(std::vector& x) {\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("const int& f(std::vector& x) {\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:32]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("int f(std::vector& x) {\n" " x[0]++;\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A { int a; };\n" "A f(std::vector& x) {\n" " x[0].a = 1;\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A { int a(); };\n" "A f(std::vector& x) {\n" " x[0].a();\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int g(int& x);\n" "int f(std::vector& x) {\n" " g(x[0]);\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("template\n" "T f(T& x) {\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("template\n" "T f(T&& x) {\n" " return x[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("template\n" "T f(T& x) {\n" " return x[0];\n" "}\n" "void h() { std::vector v; h(v); }\n"); ASSERT_EQUALS("", errout_str()); check("int f(int& x) {\n" " return std::move(x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::ostream& os) {\n" " os << \"Hello\";\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void g(int*);\n" "void f(int& x) {\n" " g(&x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A { A(int*); };\n" "A f(int& x) {\n" " return A(&x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A { A(int*); };\n" "A f(int& x) {\n" " return A{&x};\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int& x, int& y) {\n" " y++;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:13]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct A {\n" " explicit A(int& y) : x(&y) {}\n" " int * x = nullptr;\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("struct A {\n" " std::vector v;\n" " void swap(A& a) {\n" " v.swap(a.v);\n" " }\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("struct A {\n" " template\n" " void f();\n" " template\n" " void f() const;\n" "};\n" "void g(A& a) {\n" " a.f();\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::vector& v) {\n" " for(auto&& x:v)\n" " x = 1;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::vector& v) {\n" " for(auto x:v)\n" " x = 1;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:26]: (style) Parameter 'v' can be declared as reference to const [constParameterReference]\n", errout_str()); check("void f(std::vector& v) {\n" " for(auto& x:v) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'x' can be declared as reference to const [constVariableReference]\n", errout_str()); check("void f(std::vector& v) {\n" // #10980 " for (int& i : v)\n" " if (i == 0) {}\n" " for (const int& i : v)\n" " if (i == 0) {}\n" " for (auto& i : v)\n" " if (i == 0) {}\n" " for (const auto& i : v)\n" " if (i == 0) {}\n" " v.clear();\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'i' can be declared as reference to const [constVariableReference]\n" "[test.cpp:6:16]: (style) Variable 'i' can be declared as reference to const [constVariableReference]\n", errout_str()); check("void f(std::vector& v) {\n" " for(const auto& x:v) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:26]: (style) Parameter 'v' can be declared as reference to const [constParameterReference]\n", errout_str()); check("void f(int& i) {\n" " int& j = i;\n" " j++;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::vector& v) {\n" " int& i = v[0];\n" " i++;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::map >& m, unsigned int i) {\n" " std::map<:string unsigned int>& members = m[i];\n" " members.clear();\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A {\n" " int& x;\n" " A(int& y) : x(y)\n" " {}\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("struct A {\n" " A(int& x);\n" "};\n" "struct B : A {\n" " B(int& x) : A(x)\n" " {}\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("void f(bool b, int& x, int& y) {\n" " auto& z = x;\n" " auto& w = b ? y : z;\n" " w = 1;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S {\n" " int i;\n" "};\n" "int& f(S& s) {\n" " return s.i;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int* f(std::list& x, unsigned int y) {\n" " for (int& m : x) {\n" " if (m == y)\n" " return &m;\n" " }\n" " return nullptr;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int& f(std::list& x, int& y) {\n" " for (int& m : x) {\n" " if (m == y)\n" " return m;\n" " }\n" " return y;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("bool from_string(int& t, const std::string& s) {\n" " std::istringstream iss(s);\n" " return !(iss >> t).fail();\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #9710 check("class a {\n" " void operator()(int& i) const {\n" " i++;\n" " }\n" "};\n" "void f(int& i) {\n" " a()(i);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class a {\n" " void operator()(int& i) const {\n" " i++;\n" " }\n" "};\n" "void f(int& i) {\n" " a x;\n" " x(i);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class a {\n" " void operator()(const int& i) const;\n" "};\n" "void f(int& i) {\n" " a x;\n" " x(i);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:13]: (style) Parameter 'i' can be declared as reference to const [constParameterReference]\n", errout_str()); //cast or assignment to a non-const reference should prevent the warning check("struct T { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const T& z = x;\n" // Make sure we find all assignments " T& y = x;\n" " y.mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U& y = x\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " U& y = x;\n" " y.mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " my::type& y = x;\n" // we don't know if y is const or not " y.mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U& y = static_cast(x);\n" " y.mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " U& y = static_cast(x);\n" " y.mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U& y = dynamic_cast(x)\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U& y = dynamic_cast(x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U& y = dynamic_cast(x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " U& y = dynamic_cast(x);\n" " y.mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U& y = dynamic_cast(x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " U& y = dynamic_cast(x);\n" " y.mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " U* y = dynamic_cast(&x);\n" " y->mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U * y = dynamic_cast(&x);\n" " y->mutate();\n" // to avoid warnings that y can be const "}\n"); TODO_ASSERT_EQUALS("can be const", errout_str(), ""); //Currently taking the address is treated as a non-const operation when it should depend on what we do with it check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " U const * y = dynamic_cast(&x);\n" " y->mutate();\n" // to avoid warnings that y can be const "}\n"); TODO_ASSERT_EQUALS("can be const", errout_str(), ""); //Currently taking the address is treated as a non-const operation when it should depend on what we do with it check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U const * const * const * const y = dynamic_cast(&x);\n" " y->mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U const * const * const * const y = dynamic_cast(&x);\n" " y->mutate();\n" // to avoid warnings that y can be const "}\n"); TODO_ASSERT_EQUALS("can be const", errout_str(), ""); //Currently taking the address is treated as a non-const operation when it should depend on what we do with it check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U const * const * * const y = dynamic_cast(&x);\n" " y->mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " my::fancy const * const * const y = dynamic_cast<:fancy type const> const * const * const>(&x);\n" " y->mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U& y = (const U&)(x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:18]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:2:11]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " U& y = (U&)(x);\n" " y.mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("[test.cpp:4:12]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " const U& y = (typename const U&)(x);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:18]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:2:11]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " U& y = (typename U&)(x);\n" " y.mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("[test.cpp:4:12]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" " x.dostuff();\n" " U* y = (U*)(&x);\n" " y->mutate();\n" // to avoid warnings that y can be const "}\n"); ASSERT_EQUALS("[test.cpp:4:12]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str()); check("struct C { void f() const; };\n" // #9875 - crash "\n" "void foo(C& x) {\n" " x.f();\n" " foo( static_cast(0) );\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:13]: (style) Parameter 'x' can be declared as reference to const [constParameterReference]\n", errout_str()); check("class a {\n" " void foo(const int& i) const;\n" " void operator()(int& i) const;\n" "};\n" "void f(int& i) {\n" " a()(i);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class a {\n" " void operator()(const int& i) const;\n" "};\n" "void f(int& i) {\n" " a()(i);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:13]: (style) Parameter 'i' can be declared as reference to const [constParameterReference]\n", errout_str()); // #9767 check("void fct1(MyClass& object) {\n" " fct2([&](void){}, object);\n" "}\n" "bool fct2(std::function lambdaExpression, MyClass& object) {\n" " object.modify();\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #9778 check("struct A {};\n" "struct B : A {};\n" "B& f(A& x) {\n" " return static_cast(x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #10002 check("using A = int*;\n" "void f(const A& x) {\n" " ++(*x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #10086 check("struct V {\n" " V& get(typename std::vector::size_type i) {\n" " std::vector& arr = v;\n" " return arr[i];\n" " }\n" " std::vector v;\n" "};\n"); ASSERT_EQUALS("", errout_str()); // #14136 check("void f(int& x) { (void)x; }\n"); ASSERT_EQUALS("", errout_str()); check("void e();\n" "void g(void);\n" "void h(void);\n" "void ah(void);\n" "void ai(void);\n" "void j(void);\n" "void e(void);\n" "void k(void);\n" "void l(void);\n" "void m(void);\n" "void n(void);\n" "void o(void);\n" "void q(void);\n" "void r(void);\n" "void t(void);\n" "void u(void);\n" "void v(void);\n" "void w(void);\n" "void z(void);\n" "void aj(void);\n" "void am(void);\n" "void g(void);\n" "void h(void);\n" "void ah(void);\n" "void an(void);\n" "void e(void);\n" "void k(void);\n" "void ao(wchar_t *d);\n" "void ah(void);\n" "void e(void);\n" "void an(void);\n" "void e(void);\n" "void k(void);\n" "void g(void);\n" "void ah(void);\n" "void an(void);\n" "void e(void);\n" "void e(void);\n" "void e(void);\n" "void k(void);\n" "void g(void);\n" "void ah(void);\n" "void an(void);\n" "void e(void);\n" "void e(void);\n" "void k(void);\n" "void g(void);\n" "void h(void);\n" "void ah(void);\n" "void an(void);\n" "void e(void);\n" "void k(void);\n" "void e(void);\n" "void g(void);\n" "void ah(void);\n" "void k(void);\n" "void an(void);\n" "void e(void);\n" "void e(void);\n" "void e(void);\n" "void k(void);\n" "void g(void);\n" "void h(void);\n" "void ah(void);\n" "void k(void);\n" "void an(void);\n" "void k(void);\n" "void e(void);\n" "void g(void);\n" "void ah(void);\n" "void e(void);\n" "void k(void);\n" "void g(void);\n" "void h(void);\n" "void ah(void);\n" "void an(void);\n" "void an(void);\n" "void k(void);\n" "void e(void);\n" "void e(void);\n" "void e(void);\n" "void g(void);\n" "void k(void);\n" "void g(void);\n" "void h(void);\n" "void ah(void);\n" "void an(void);\n" "void k(void);\n" "void k(void);\n" "void e(void);\n" "void g(void);\n" "void g(void);\n" "void ah(void);\n" "void an(void);\n" "void e(void);\n" "void k(void);\n" "void e(void);\n" "void ap(wchar_t *c, int d);\n" "void ah(void);\n" "void an(void);\n" "void g(void);\n" "void h(void);\n" "void ah(void);\n" "void aq(char *b, size_t d, char *c, int a);\n" "void ar(char *b, size_t d, char *c, va_list a);\n" "void k(void);\n" "void g(void);\n" "void g(void);\n" "void h(void);\n" "void ah(void);\n" "void an(void);\n" "void k(void);\n" "void k(void);\n" "void e(void);\n" "void g(void);\n" "void g(void);\n" "void as(std::string s);\n" "void at(std::ifstream &f);\n" "void au(std::istream &f);\n" "void av(std::string &aa, std::wstring &ab);\n" "void aw(bool b, double x, double y);\n" "void ax(int i);\n" "void ay(std::string c, std::wstring a);\n" "void az(const std::locale &ac);\n" "void an();\n" "void ba(std::ifstream &f);\n" "void bb(std::istream &f) {\n" "f.read(NULL, 0);\n" "}\n" "void h(void) {\n" "struct tm *tm = 0;\n" "(void)std::asctime(tm);\n" "(void)std::asctime(0);\n" "}\n" "void bc(size_t ae) {\n" "wchar_t *ad = 0, *af = 0;\n" "struct tm *ag = 0;\n" "(void)std::wcsftime(ad, ae, af, ag);\n" "(void)std::wcsftime(0, ae, 0, 0);\n" "}\n" "void k(void) {}\n" "void bd(void);\n" "void be(void);\n" "void bf(int b);\n" "void e(void);\n" "void e(void);\n" "void bg(wchar_t *p);\n" "void bh(const std::list &ak, const std::list &al);\n" "void ah();\n" "void an();\n" "void h();\n"); ASSERT_EQUALS("[test.cpp:131:12]: (style) Variable 'tm' can be declared as pointer to const [constVariablePointer]\n" "[test.cpp:136:19]: (style) Variable 'af' can be declared as pointer to const [constVariablePointer]\n" "[test.cpp:137:12]: (style) Variable 'ag' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("class C\n" "{\n" "public:\n" " explicit C(int&);\n" "};\n" "\n" "class D\n" "{\n" "public:\n" " explicit D(int& i);\n" "\n" "private:\n" " C c;\n" "};\n" "\n" "D::D(int& i)\n" " : c(i)\n" "{\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class C\n" "{\n" "public:\n" " explicit C(int&);\n" "};\n" "\n" "class D\n" "{\n" "public:\n" " explicit D(int& i) noexcept;\n" "\n" "private:\n" " C c;\n" "};\n" "\n" "D::D(int& i) noexcept\n" " : c(i)\n" "{}\n"); ASSERT_EQUALS("", errout_str()); check("class C\n" "{\n" "public:\n" " explicit C(const int&);\n" "};\n" "\n" "class D\n" "{\n" "public:\n" " explicit D(int& i);\n" "\n" "private:\n" " C c;\n" "};\n" "\n" "D::D(int& i)\n" " : c(i)\n" "{\n" "}\n"); TODO_ASSERT_EQUALS("[test.cpp:16]: (style) Parameter 'i' can be declared as reference to const\n", "", errout_str()); check("class C\n" "{\n" "public:\n" " explicit C(int);\n" "};\n" "\n" "class D\n" "{\n" "public:\n" " explicit D(int& i);\n" "\n" "private:\n" " C c;\n" "};\n" "\n" "D::D(int& i)\n" " : c(i)\n" "{\n" "}\n"); TODO_ASSERT_EQUALS("[test.cpp:16]: (style) Parameter 'i' can be declared as reference to const\n", "", errout_str()); check("class C\n" "{\n" "public:\n" " explicit C(int, int);\n" "};\n" "\n" "class D\n" "{\n" "public:\n" " explicit D(int& i);\n" "\n" "private:\n" " C c;\n" "};\n" "\n" "D::D(int& i)\n" " : c(0, i)\n" "{\n" "}\n"); TODO_ASSERT_EQUALS("[test.cpp:16]: (style) Parameter 'i' can be declared as reference to const\n", "", errout_str()); check("void f(std::map> &map) {\n" // #10266 " for (auto &[slave, panels] : map)\n" " panels.erase(it);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { void f(); int i; };\n" "void call_f(S& s) { (s.*(&S::f))(); }\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int a[1]; };\n" "void f(S& s) { int* p = s.a; *p = 0; }\n"); ASSERT_EQUALS("", errout_str()); check("struct Foo {\n" // #9910 " int* p{};\n" " int* get() { return p; }\n" " const int* get() const { return p; }\n" "};\n" "struct Bar {\n" " int j{};\n" " void f(Foo& foo) const { int* q = foo.get(); *q = j; }\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("struct S {\n" // #10679 " void g(long L, const C*& PC) const;\n" " void g(long L, C*& PC);\n" "};\n" "void f(S& s) {\n" " C* PC{};\n" " s.g(0, PC);\n" "};\n"); ASSERT_EQUALS("", errout_str()); // #10785 check("template \n" "struct d {\n" " T& g(C& c, T C::*f) { return c.*f; }\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::map& m) {\n" " std::cout << m[0] << std::endl;\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::vector<:map int>>& v) {\n" // #11607 " for (auto& m : v)\n" " std::cout << m[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int i; };\n" // #11473 "void f(std::vector<:vector>>&m, int*& p) {\n" " auto& a = m[0];\n" " for (auto& s : a) {\n" " p = &s.i;\n" " return;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int& g(int* p, int& r) {\n" // #11625 " if (p)\n" " return *p;\n" " return r;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("template void f(std::vector& d, const std::vector& s) {\n" // #11632 " for (const auto& e : s) {\n" " T* newE = new T(*e);\n" " d.push_back(newE);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #11682 check("struct b {\n" " void mutate();\n" "};\n" "struct c {\n" " const b& get() const;\n" " b get();\n" "};\n" "struct d {\n" " void f(c& e) const {\n" " e.get().mutate();\n" " }\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("struct B { virtual void f() const {} };\n" // #11528 "struct D : B {};\n" "void g(B* b) {\n" " D* d = dynamic_cast(b);\n" " if (d)\n" " d->f();\n" "}\n"); ASSERT_EQUALS( "[test.cpp:4:8]: (style) Variable 'd' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("void g(const int*);\n" "void f(const std::vector&v) {\n" " for (int* i : v)\n" " g(i);\n" "}\n"); ASSERT_EQUALS( "[test.cpp:3:15]: (style) Variable 'i' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("struct A {\n" // #11225 " A();\n" " virtual ~A();\n" "};\n" "struct B : A {};\n" "void f(A* a) {\n" " const B* b = dynamic_cast(a);\n" "}\n" "void g(A* a) {\n" " const B* b = (const B*)a;\n" "}\n"); ASSERT_EQUALS("[test.cpp:10:18]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n" "[test.cpp:6:11]: (style) Parameter 'a' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:9:11]: (style) Parameter 'a' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void g(int*);\n" "void f(std::vector& v) {\n" " g(v.data());\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void g(const int*);\n" "void f(std::vector& v) {\n" " g(v.data());\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:26]: (style) Parameter 'v' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct a {\n" " template \n" " void mutate();\n" "};\n" "struct b {};\n" "template \n" "void f(a& x) {\n" " x.mutate();\n" "}\n" "template \n" "void f(const b&)\n" "{}\n" "void g(a& c) { f(c); }\n"); ASSERT_EQUALS("", errout_str()); check("struct S {\n" " template \n" " T* g() {\n" " return reinterpret_cast(m);\n" " }\n" " template \n" " const T* g() const {\n" " return reinterpret_cast(m);\n" " }\n" " char* m;\n" "};\n" "void f(S& s) {\n" " const int* p = s.g();\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int x; };\n" // #11818 "std::istream& f(std::istream& is, S& s) {\n" " return is >> s.x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("bool f(std::string& s1, std::string& s2) {\n" // #12203 " return &s1 == &s2;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:21]: (style) Parameter 's1' can be declared as reference to const [constParameterReference]\n" "[test.cpp:1:38]: (style) Parameter 's2' can be declared as reference to const [constParameterReference]\n", errout_str()); check("void f(int& r) {\n" // #12214 " (void)(true);\n" " if (r) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found unused cast in expression '(void)(true)'. [constStatement]\n" "[test.cpp:1:13]: (style) Parameter 'r' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct S { void f(int&); };\n" // #12216 "void g(S& s, int& r, void (S::* p2m)(int&)) {\n" " (s.*p2m)(r);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S {\n" " void f(int& r) { p = &r; }\n" " int* p;\n" "};\n" "void g(std::vector& v1, std::vector& v2) {\n" " std::transform(v1.begin(), v1.end(), v2.begin(), [](auto& x) { return &x; });\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class T;\n" // #11869 "class E {\n" "public:\n" " class F {\n" " public:\n" " explicit F(const T* t);\n" " };\n" "};\n" "void f(T& t) {\n" " std::list<:f> c(1, E::F(&t));\n" "}\n"); ASSERT_EQUALS("[test.cpp:9:11]: (style) Parameter 't' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct T;\n" "struct U {\n" " struct V { explicit V(const T* p); };\n" "};\n" "void g(U::V v);\n" "void f(T& t) {\n" " g(U::V(&t));\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:11]: (style) Parameter 't' can be declared as reference to const [constParameterReference]\n", errout_str()); check("void f1(std::vector& v) {\n" // #11207 " auto it = v.cbegin();\n" " while (it != v.cend()) {\n" " if (*it > 12) {}\n" " ++it;\n" " }\n" "}\n" "void f2(std::vector& v) {\n" " auto it = v.begin();\n" " while (it != v.end()) {\n" " if (*it > 12) {}\n" " ++it;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:27]: (style) Parameter 'v' can be declared as reference to const [constParameterReference]\n" "[test.cpp:8:27]: (style) Parameter 'v' can be declared as reference to const [constParameterReference]\n", errout_str()); check("void cb(const std::string&);\n" // #12349, #12350, #12351 "void f(std::string& s) {\n" " const std::string& str(s);\n" " cb(str);\n" "}\n" "void g(std::string& s) {\n" " const std::string& str{ s };\n" " cb(str);\n" "}\n" "void h(std::string* s) {\n" " const std::string& str(*s);\n" " cb(str);\n" "}\n" "void k(std::string* s) {\n" " const std::string& str = *s;\n" " cb(str);\n" "}\n" "void m(std::string& s) {\n" " const std::string str(s);\n" " cb(str);\n" "}\n" "void n(std::string* s) {\n" " const std::string& str(*s);\n" " cb(str);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:21]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n" "[test.cpp:6:21]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n" "[test.cpp:18:21]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n" "[test.cpp:10:21]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:14:21]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:22:21]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S {\n" " S(std::string& r);\n" "};\n" "void f(std::string& str) {\n" " const S& s(str);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct C {\n" // #10052 " int& operator()(int);\n" "};\n" "void f(std::vector& c) {\n" " c[0](5) = 12;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int& t) {\n" // #11713 " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:12]: (style) Parameter 't' can be declared as reference to const [constParameterReference]\n", errout_str()); check("void f(std::list<:string>& v) {\n" // #12202 " v.remove_if([](std::string& s) {\n" " return true;\n" " });\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:33]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct S {\n" // #12762 " std::vector m;\n" " void f();\n" "};\n" "void S::f() {\n" " std::vector& r = m;\n" " g(r[0] * 2);\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:23]: (style) Variable 'r' can be declared as reference to const [constVariableReference]\n", errout_str()); check("std::iostream& get();\n" // #12940 "std::iostream& Fun() {\n" " auto lam = []() -> std::iostream& {\n" " std::iostream& ios = get();\n" " return ios;\n" " };\n" " return lam();\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int x[3]; };\n" // #13226 "void g(int a, int* b);\n" "void f(int a, S& s) {\n" " return g(a, s.x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { std::vector v; };\n" // #13317 "struct T { S s; };\n" "int f(S& s) {\n" " for (std::vector::const_iterator it = s.v.cbegin(); it != s.v.cend(); ++it) {}\n" " return *s.v.cbegin();\n" "}\n" "int f(T& t) {\n" " return *t.s.v.cbegin();\n" "}\n" "int f(std::vector& v) {\n" " return *v.cbegin();\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:10]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n" "[test.cpp:7:10]: (style) Parameter 't' can be declared as reference to const [constParameterReference]\n" "[test.cpp:10:25]: (style) Parameter 'v' can be declared as reference to const [constParameterReference]\n", errout_str()); check("void push(V& v) { v.push_back({ .x = 1 }); }\n"); // #14010 ASSERT_EQUALS("", errout_str()); check("size_t* f(std::array& a) { return reinterpret_cast(a.data()); }\n"); // #14074 ASSERT_EQUALS("", errout_str()); check("struct S { int i; };\n" // #14231 "void* f(S& s, int& v) {\n" " v = s.i;\n" " return (void*)&s;\n" "}\n"); // don't crash ASSERT_EQUALS("", errout_str()); check("struct S { int i; };\n" // #14251 "struct T { std::optional s; };\n" "void f(T& t) {\n" " t.s->i = 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct B {};\n" // #13877 "struct D : B { int i; };\n" "void f(B& b) {\n" " static_cast(b).i = 0;\n" "}\n" "void g(B& b) {\n" " std::cin >> static_cast(b).i;\n" "}\n" "int h(B& b) {\n" " return static_cast(b).i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:9:10]: (style) Parameter 'b' can be declared as reference to const [constParameterReference]\n", errout_str()); check("void f(int *p) {\n" // #14409 " int*& pp{ p };\n" " if (*pp) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class C {\n" "public:\n" " explicit C(const std::string s);\n" "private:\n" " std::string _s;\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("void f(int& r) {\n" // #9761 " o1 = r;\n" "}\n" "boost::optional o2;\n" "void g(int& r) {\n" " o2 = r;\n" "}\n" "struct T {\n" " int* p;\n" " T& operator=(int& rhs) { p = &rhs; return *this; }\n" "};\n" "void h(T& t, int& r) {\n" " t = r;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::optional& o) {\n" " *o = 1;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f() {\n" " int x = 0;\n" " int& r(x);\n" " r = x;\n" " return r;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct Item { int state; };\n" "void foo(std::vector &items) {\n" " for (auto &item : items) {\n" " switch (auto &s = item.state) {\n" " case 0: s = 1; break;\n" " default: break;\n" " }\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void constParameterCallback() { check("int callback(std::vector& x) { return x[0]; }\n" "void f() { dostuff(callback); }\n"); ASSERT_EQUALS("[test.cpp:2:20] -> [test.cpp:1:32]: (style) Parameter 'x' can be declared as reference to const. However it seems that 'callback' is a callback function, if 'x' is declared with const you might also need to cast function pointer(s). [constParameterCallback]\n", errout_str()); // #9906 check("class EventEngine : public IEventEngine {\n" "public:\n" " EventEngine();\n" "\n" "private:\n" " void signalEvent(ev::sig& signal, int revents);\n" "};\n" "\n" "EventEngine::EventEngine() {\n" " mSigWatcher.set(this);\n" "}\n" "\n" "void EventEngine::signalEvent(ev::sig& signal, int revents) {\n" " switch (signal.signum) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:10:48] -> [test.cpp:13:40]: (style) Parameter 'signal' can be declared as reference to const. However it seems that 'signalEvent' is a callback function, if 'signal' is declared with const you might also need to cast function pointer(s). [constParameterCallback]\n", errout_str()); check("void f(int* p) {}\n" // 12843 "void g(std::map&m) {\n" " m[&f] = 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:8] -> [test.cpp:1:13]: (style) Parameter 'p' can be declared as pointer to const. " "However it seems that 'f' is a callback function, if 'p' is declared with const you might also need to cast function pointer(s). [constParameterCallback]\n", errout_str()); check("struct S { explicit S(std::function); };\n" // #13338 "void cb(std::string s) {\n" " (void)s.empty();\n" "}\n" "void f() {\n" " S s2{ cb };\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:11] -> [test.cpp:2:21]: (performance) Function parameter 's' should be passed by const reference. However it seems that 'cb' is a callback function. [passedByValueCallback]\n", errout_str()); check("struct S {\n" // #14696 " explicit S(std::string s = {}) {}\n" "};\n" "struct T {\n" " explicit T(std::string* s = {}) {}\n" "};\n"); ASSERT_EQUALS("[test.cpp:2:28]: (performance) Function parameter 's' should be passed by const reference. [passedByValue]\n" "[test.cpp:5:29]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); } void constPointer() { check("void foo(int *p) { return *p; }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { x = *p; }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { int &ref = *p; ref = 12; }\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int *p) { x = *p + 10; }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { return p[10]; }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { int &ref = p[0]; ref = 12; }\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int *p) { x[*p] = 12; }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { if (p) {} }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { if (p || x) {} }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { if (p == 0) {} }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { if (!p) {} }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { if (*p > 123) {} }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { return *p + 1; }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(int *p) { return *p > 1; }\n"); ASSERT_EQUALS("[test.cpp:1:15]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void foo(const int* c) { if (c == 0) {}; }\n"); ASSERT_EQUALS("", errout_str()); check("struct a { void b(); };\n" "struct c {\n" " a* d;\n" " a& g() { return *d; }\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("struct a { void b(); };\n" "struct c { a* d; };\n" "void e(c);\n"); ASSERT_EQUALS("", errout_str()); check("struct V {\n" " V& get(typename std::vector::size_type i, std::vector* arr) {\n" " return arr->at(i);\n" " }\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("struct A {};\n" "struct B : A {};\n" "B* f(A* x) {\n" " return static_cast(x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(std::vector* x) {\n" " int& i = (*x)[0];\n" " i++;\n" " return i;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A { int a; };\n" "A f(std::vector* x) {\n" " x->front().a = 1;\n" " return x->front();\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::vector* v) {\n" " for(auto&& x:*v)\n" " x = 1;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A {\n" " int* x;\n" " A(int* y) : x(y)\n" " {}\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("void f(bool b, int* x, int* y) {\n" " int* z = x;\n" " int* w = b ? y : z;\n" " *w = 1;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(bool b, int* x, int* y) {\n" " int& z = *x;\n" " int& w = b ? *y : z;\n" " w = 1;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class Base { virtual void dostuff(int *p) = 0; };\n" // #10397 "class Derived: public Base { int x; void dostuff(int *p) override { x = *p; } };\n"); ASSERT_EQUALS("", errout_str()); check("struct Data { char buf[128]; };\n" // #10483 "void encrypt(Data& data) {\n" " const char a[] = \"asfasd\";\n" " memcpy(data.buf, &a, sizeof(a));\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #10547 check("void foo(std::istream &istr) {\n" " unsigned char x[2];\n" " istr >> x[0];\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #10744 check("S& f() {\n" " static S* p = new S();\n" " return *p;\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #10471 check("void f(std::array const& i) {\n" " if (i[0] == 0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #10466 check("typedef void* HWND;\n" "void f(const std::vector&v) {\n" " for (const auto* h : v)\n" " if (h) {}\n" " for (const auto& h : v)\n" " if (h) {}\n" "}\n"); ASSERT_EQUALS( "[test.cpp:5:22]: (style) Variable 'h' can be declared as pointer to const [constVariableReference]\n", errout_str()); check("void f(const std::vector& v) {\n" " for (const auto& p : v)\n" " if (p == nullptr) {}\n" " for (const auto* p : v)\n" " if (p == nullptr) {}\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:22]: (style) Variable 'p' can be declared as pointer to const [constVariableReference]\n", errout_str()); check("void f(std::vector& v) {\n" " for (const auto& p : v)\n" " if (p == nullptr) {}\n" " for (const auto* p : v)\n" " if (p == nullptr) {}\n" " for (const int* const& p : v)\n" " if (p == nullptr) {}\n" " for (const int* p : v)\n" " if (p == nullptr) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:27]: (style) Parameter 'v' can be declared as reference to const [constParameterReference]\n" "[test.cpp:2:22]: (style) Variable 'p' can be declared as pointer to const [constVariableReference]\n", errout_str()); check("void f(std::vector& v) {\n" " for (const auto& p : v)\n" " if (p == nullptr) {}\n" " for (const auto* p : v)\n" " if (p == nullptr) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:33]: (style) Parameter 'v' can be declared as reference to const [constParameterReference]\n", errout_str()); check("void f(const std::vector& v) {\n" " for (const auto& p : v)\n" " if (p == nullptr) {}\n" " for (const auto* p : v)\n" " if (p == nullptr) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const int* const p) {\n" " if (p == nullptr) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void g(int*);\n" "void f(int* const* pp) {\n" " int* p = pp[0];\n" " g(p);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("template \n" "struct S {\n" " static bool f(const T& t) { return t != nullptr; }\n" "};\n" "S s;\n"); ASSERT_EQUALS("", errout_str()); check("typedef void* HWND;\n" // #11084 "void f(const HWND h) {\n" " if (h == nullptr) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("using HWND = void*;\n" "void f(const HWND h) {\n" " if (h == nullptr) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("typedef int A;\n" "void f(A* x) {\n" " if (x == nullptr) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'x' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("using A = int;\n" "void f(A* x) {\n" " if (x == nullptr) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'x' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S { void v(); };\n" // #11095 "void f(S* s) {\n" " (s - 1)->v();\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::vector& v) {\n" // #11085 " for (int* p : v) {\n" " if (p) {}\n" " }\n" " for (auto* p : v) {\n" " if (p) {}\n" " }\n" " v.clear();\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n" "[test.cpp:5:16]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("void f() {\n" " char a[1][1];\n" " char* b[1];\n" " b[0] = a[0];\n" " **b = 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("ptrdiff_t f(int *p0, int *p1) {\n" // #11148 " return p0 - p1;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:18]: (style) Parameter 'p0' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:1:27]: (style) Parameter 'p1' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void f() {\n" " std::array a{}, b{};\n" " const std::array& r = a;\n" " if (r == b) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S {};\n" // #11599 "void g(S);\n" "void h(const S&);\n" "void h(int, int, const S&);\n" "void i(S&);\n" "void j(const S*);\n" "void j(int, int, const S*);\n" "void f1(S* s) {\n" " g(*s);\n" "}\n" "void f2(S* s) {\n" " h(*s);\n" "}\n" "void f3(S* s) {\n" " h(1, 2, *s);\n" "}\n" "void f4(S* s) {\n" " i(*s);\n" "}\n" "void f5(S& s) {\n" " j(&s);\n" "}\n" "void f6(S& s) {\n" " j(1, 2, &s);\n" "}\n"); ASSERT_EQUALS("[test.cpp:20:12]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n" "[test.cpp:23:12]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n" "[test.cpp:8:12]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:11:12]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:14:12]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void g(int, const int*);\n" "void h(const int*);\n" "void f(int* p) {\n" " g(1, p);\n" " h(p);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:13]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void f(int, const int*);\n" "void f(int i, int* p) {\n" " f(i, const_cast(p));\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int a; };\n" "void f(std::vector& v, int b) {\n" " size_t n = v.size();\n" " for (size_t i = 0; i < n; i++) {\n" " S& s = v[i];\n" " if (!(b & s.a))\n" " continue;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:12]: (style) Variable 's' can be declared as reference to const [constVariableReference]\n", errout_str()); // don't crash check("void f(int& i) {\n" " new (&i) int();\n" "}\n"); ASSERT_EQUALS("", errout_str()); // don't crash check("void f(int& i) {\n" " int& r = i;\n" " if (!&r) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:10]: (style) Variable 'r' can be declared as reference to const [constVariableReference]\n", errout_str()); // don't crash check("class C;\n" // #11646 "void g(const C* const p);\n" "void f(C* c) {\n" " g(c);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:11]: (style) Parameter 'c' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("typedef void (*cb_t)(int*);\n" // #11674 "void cb(int* p) {\n" " if (*p) {}\n" "}\n" "void g(cb_t);\n" "void f() {\n" " g(cb);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:7] -> [test.cpp:2:14]: (style) Parameter 'p' can be declared as pointer to const. " "However it seems that 'cb' is a callback function, if 'p' is declared with const you might also need to cast function pointer(s). [constParameterCallback]\n", errout_str()); check("typedef void (*cb_t)(int*);\n" "void cb(int* p) {\n" " if (*p) {}\n" "}\n" "void g(cb_t);\n" "void f() {\n" " g(::cb);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:9] -> [test.cpp:2:14]: (style) Parameter 'p' can be declared as pointer to const. " "However it seems that 'cb' is a callback function, if 'p' is declared with const you might also need to cast function pointer(s). [constParameterCallback]\n", errout_str()); check("void f1(std::vector* p) {\n" // #11681 " if (p->empty()) {}\n" // warn "}\n" "void f2(std::vector* p) {\n" " p->resize(0);\n" "}\n" "struct S {\n" " void h1() const;\n" " void h2();\n" " int i;\n" "};\n" "void k(int&);\n" "void g1(S* s) {\n" " s->h1();\n" // warn "}\n" "void g1(S* s) {\n" " s->h2();\n" "}\n" "void g1(S* s) {\n" " if (s->i) {}\n" // warn "}\n" "void g2(S* s) {\n" " s->i = 0;\n" "}\n" "void g3(S* s) {\n" " k(s->i);\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:27]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:13:12]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:19:12]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S {\n" // #11573 " const char* g() const {\n" " return m;\n" " }\n" " const char* m;\n" "};\n" "struct T { std::vector v; };\n" "void f(T* t, const char* n) {\n" " for (const auto* p : t->v)\n" " if (strcmp(p->g(), n) == 0) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:8:11]: (style) Parameter 't' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void f(int*& p, int* q) {\n" " p = q;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int a[1]; };\n" "void f(S* s) {\n" " if (s->a[0]) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("size_t f(char* p) {\n" // #11842 " return strlen(p);\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:16]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void f(int* p) {\n" // #11862 " long long j = *(p++);\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:13]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void f(void *p, size_t nmemb, size_t size, int (*cmp)(const void *, const void *)) {\n" " qsort(p, nmemb, size, cmp);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void g(bool *r, std::size_t *b) {\n" // #12129 " if (*r && *b >= 5) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:14]: (style) Parameter 'r' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:1:30]: (style) Parameter 'b' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void f(int i) {\n" // #12185 " void* p = &i;\n" " std::cout << p << '\\n';\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("struct S { const T* t; };\n" // #12206 "void f(S* s) {\n" " if (s->t.i) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void f(char *a1, char *a2) {\n" // #12252 " char* b = new char[strlen(a1) + strlen(a2) + 2];\n" " sprintf(b, \"%s_%s\", a1, a2);\n" " delete[] b;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:14]: (style) Parameter 'a1' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:1:24]: (style) Parameter 'a2' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("int f(int* p) {\n" // #11713 " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:12]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void f(int *src, int* dst) {\n" // #12518 " *dst++ = (int)*src++;\n" " *dst++ = static_cast(*src++);\n" " *dst = (int)*src;\n" "}\n" "void g(int* dst) {\n" " (int&)*dst = 5;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:13]: (style) Parameter 'src' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S {};\n" "void f(T* t) {\n" " S* s = (S*)t->p;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:12]: (style) C-style pointer casting [cstyleCast]\n" "[test.cpp:3:8]: (style) Variable 's' can be declared as pointer to const [constVariablePointer]\n", errout_str()); // don't crash check("struct S { int i; };\n" // #12205 "void f(S* s) {\n" " (void)s->i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("void f(int* a, int* b, int i) {\n" // #13072 " a[b[i]] = 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:21]: (style) Parameter 'b' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("int f(int* a, int* b, int i) {\n" // #13085 " a[*(b + i)] = 0;\n" " return *(b + i);\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:20]: (style) Parameter 'b' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S { int a; };\n" // #13286 "void f(struct S* s) {\n" " if ((--s)->a >= 0) {}\n" "}\n" "void g(struct S* s) {\n" " --s;\n" " if (s->a >= 0) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:5:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct T;\n" "void use(const T*);\n" "void f(T* tok0) {\n" " T *tok1 = tok0;\n" " const T *tok2 = tok1;\n" " use(tok2);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:8]: (style) Variable 'tok1' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("struct S { S* next; };\n" // #14119 "void f(S* s) {\n" " for (S* p = s->next; p != nullptr; p = p->next) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:13]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("void f(int* p) {\n" " for (int* q = p; q;)\n" " break;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'q' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("void g(const int*);\n" // #14148 "void f() {\n" " int a[] = {1, 2, 3};\n" " for (int* p = a; *p != 3; p++) {\n" " g(p);\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("uintptr_t f(int* p) {\n" " return (uintptr_t)p;\n" "}\n" "uintptr_t g(int* p) {\n" " return static_cast(p);\n" "}\n" "U h(int* p) {\n" " return (U)p;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:4:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("using fp_t = int (*)(int*);\n" // #14510 "fp_t g_fp;\n" "struct S { fp_t m_fp; };\n" "void g(fp_t);\n" "S f(S* s) {\n" " g_fp = [](int* p) { return *p; };\n" " s->m_fp = [](int* p) { return *p; };\n" " g([](int* p) { return *p; });\n" " auto x = [](int* p) { return *p; };\n" " g(x);\n" " return { [](int* p) { return *p; } };\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int i = 0;\n" " auto x = [&]() { int* p = &i; if (*p) {} };\n" " x();\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:27]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", errout_str()); check("int f() {\n" " int i = 0;\n" " return [](int* p) { return *p; }(&i);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:20]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S {\n" // #14571 " char* c;\n" "};\n" "struct T {\n" " S s;\n" "};\n" "void f(std::string* p, T& t) {\n" " S& r = t.s;\n" " strcpy(r.c, p->c_str());\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:21]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S {\n" // #14559 " int gc() const;\n" " int gnc();\n" "};\n" "int f1(S* s) {\n" " return h(s ? s->gc() : 1);\n" "}\n" "int f2(S* s) {\n" " return h(s ? s->gnc() : 1);\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:11]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("using IntPtr = int *;\n" "int* foo(IntPtr bar) {\n" " return bar = 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int x; };\n" // #14700 "int f(S* s) {\n" " return s->x ? 1 : 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:10]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S { int a[1][1]; };\n" // #14714 "int f(S* s) {\n" " return s->a[0][0] ? 1 : 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:10]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("int f(int *p, int *q) {\n" // #14748 " return p ? *p : *q;\n" "}\n" "void g(int *p, int *q) {\n" " int& r = p ? *p : *q;\n" " r = 0;\n" "}\n" "void h(int *p, int *q) {\n" " i(p ? *p : *q);\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:12]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:1:20]: (style) Parameter 'q' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("int f(std::vector* p) {\n" // #14810 " return *p->cbegin();\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:25]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S {\n" // #14817 " explicit S(int *a) : m{ a[0], a[1] } {}\n" " int m[2];\n" "}\n" "struct T {\n" " explicit T(int *a) : m{ &a[0], &a[1] } {}\n" " int* m[2];\n" "};\n"); ASSERT_EQUALS("[test.cpp:2:21]: (style) Parameter 'a' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("class A {\n" // #11471 "public:\n" " A(const int& i, int) : m_i(&i) {}\n" " const int* m_i;\n" "};\n" "A f(int& s) {\n" " return A(s, 0);\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:10]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n", errout_str()); check("struct S { std::string a; };\n" // #13678 "struct T { S s; };\n" "bool f(S* s) {\n" " return s->a.empty();\n" "}\n" "bool g(T* t) {\n" " return t->s.a.empty();\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:11]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:6:11]: (style) Parameter 't' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S { int i; };\n" // #13099 "double f(S * s, int n, int a, int b, double* p) {\n" " return (s + (n * (a + 1) + b))->i / *(p + b);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:14]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:2:46]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct S : U {\n" // #13944 " void f(int* p) const {\n" " if (m == p) {}\n" " }\n" " void g(int* p) final {\n" " if (m == p) {}\n" " }\n" " int* m;\n" "};\n"); ASSERT_EQUALS("[test.cpp:2:17]: (style) Either there is a missing override/final keyword, or the parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); } void constArray() { check("void f(std::array& a) {\n" " if (a[0]) {}\n" "}\n" "void g(std::array& a) {\n" " a.fill(0);\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:28]: (style) Parameter 'a' can be declared as const array [constParameterReference]\n", errout_str()); check("int f() {\n" " static int i[1] = {};\n" " return i[0];\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) Variable 'i' can be declared as const array [constVariable]\n", errout_str()); check("int f() {\n" " static int i[] = { 0 };\n" " int j = i[0] + 1;\n" " return j;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) Variable 'i' can be declared as const array [constVariable]\n", errout_str()); check("void f(int i) {\n" " const char *tmp;\n" " char* a[] = { \"a\", \"aa\" };\n" " static char* b[] = { \"b\", \"bb\" };\n" " tmp = a[i];\n" " printf(\"%s\", tmp);\n" " tmp = b[i];\n" " printf(\"%s\", tmp);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:11]: (style) Variable 'a' can be declared as const array [constVariable]\n" "[test.cpp:4:18]: (style) Variable 'b' can be declared as const array [constVariable]\n", errout_str()); check("int f(int i, int j) {\n" // #13069 " int a[3][4] = {\n" " { 2, 2, -1, -1 },\n" " { 2, -1, 2, -1 },\n" " { 2, -1, -1, 2 },\n" " };\n" " return a[j][i];\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (style) Variable 'a' can be declared as const array [constVariable]\n", errout_str()); check("void f(int n, int v[42]) {\n" // #12796 " int j = 0;\n" " for (int i = 0; i < n; ++i) {\n" " j += 1;\n" " if (j == 1) {}\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:19]: (style) Parameter 'v' can be declared as const array [constParameter]\n", errout_str()); } void switchRedundantAssignmentTest() { check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y = 2;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:11] -> [test.cpp:9:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " {\n" " y = 2;\n" " }\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:8:11] -> [test.cpp:11:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y = 2;\n" " case 3:\n" " if (x)\n" " {\n" " y = 3;\n" " }\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " {\n" " y = 2;\n" " if (z)\n" " printf(\"%d\", y);\n" " }\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int x = a;\n" " int y = 1;\n" " switch (x)\n" " {\n" " case 2:\n" " x = 2;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (x)\n" " {\n" " case 2:\n" " y = 2;\n" " break;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " while(xyz()) {\n" " switch (x)\n" " {\n" " case 2:\n" " y = 2;\n" " continue;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " while(xyz()) {\n" " switch (x)\n" " {\n" " case 2:\n" " y = 2;\n" " throw e;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (x)\n" " {\n" " case 2:\n" " y = 2;\n" " printf(\"%d\", y);\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (x)\n" " {\n" " case 2:\n" " y = 2;\n" " bar();\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:11] -> [test.cpp:10:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void bar() {}\n" // bar isn't noreturn "void foo()\n" "{\n" " int y = 1;\n" " switch (x)\n" " {\n" " case 2:\n" " y = 2;\n" " bar();\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:8:11] -> [test.cpp:11:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo(int a) {\n" " char str[10];\n" " switch (a)\n" " {\n" " case 2:\n" " strcpy(str, \"a'\");\n" " case 3:\n" " strcpy(str, \"b'\");\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); TODO_ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:8]: (style) Buffer 'str' is being written before its old content has been used. 'break;' missing?\n", "", errout_str()); check("void foo(int a) {\n" " char str[10];\n" " switch (a)\n" " {\n" " case 2:\n" " strncpy(str, \"a'\");\n" " case 3:\n" " strncpy(str, \"b'\");\n" " }\n" "}\n"); TODO_ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:8]: (style) Buffer 'str' is being written before its old content has been used. 'break;' missing?\n", "", errout_str()); check("void foo(int a) {\n" " char str[10];\n" " int z = 0;\n" " switch (a)\n" " {\n" " case 2:\n" " strcpy(str, \"a'\");\n" " z++;\n" " case 3:\n" " strcpy(str, \"b'\");\n" " z++;\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); TODO_ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:10]: (style) Buffer 'str' is being written before its old content has been used. 'break;' missing?\n", "", errout_str()); check("void foo(int a) {\n" " char str[10];\n" " switch (a)\n" " {\n" " case 2:\n" " strcpy(str, \"a'\");\n" " break;\n" " case 3:\n" " strcpy(str, \"b'\");\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a) {\n" " char str[10];\n" " switch (a)\n" " {\n" " case 2:\n" " strcpy(str, \"a'\");\n" " printf(str);\n" " case 3:\n" " strcpy(str, \"b'\");\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); // Ticket #5158 "segmentation fault (valid code)" check("typedef struct ct_data_s {\n" " union {\n" " char freq;\n" " } fc;\n" "} ct_data;\n" "typedef struct internal_state {\n" " struct ct_data_s dyn_ltree[10];\n" "} deflate_state;\n" "void f(deflate_state *s) {\n" " s->dyn_ltree[0].fc.freq++;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); // Ticket #6132 "crash: daca: kvirc CheckOther::checkRedundantAssignment()" check("void HttpFileTransfer :: transferTerminated ( bool bSuccess ) {\n" "if ( m_szCompletionCallback . isNull ( ) ) {\n" "KVS_TRIGGER_EVENT ( KviEvent_OnHTTPGetTerminated , out ? out : ( g_pApp . activeConsole ( ) ) , & vParams )\n" "} else {\n" "KviKvsScript :: run ( m_szCompletionCallback , out ? out : ( g_pApp . activeConsole ( ) ) , & vParams ) ;\n" "}\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int x;\n" " switch (state) {\n" " case 1: x = 3; goto a;\n" " case 1: x = 6; goto a;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void switchRedundantOperationTest() { check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " ++y;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:9] -> [test.cpp:9:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " {\n" " ++y;\n" " }\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:8:9] -> [test.cpp:11:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " (void)y;\n" " case 3:\n" " ++y;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " ++y;\n" " case 3:\n" " ++y;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " --y;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:9] -> [test.cpp:9:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " {\n" " --y;\n" " }\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:8:9] -> [test.cpp:11:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " (void)y;\n" " case 3:\n" " --y;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " --y;\n" " case 3:\n" " --y;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y++;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:10] -> [test.cpp:9:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " {\n" " y++;\n" " }\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:8:10] -> [test.cpp:11:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y = 2;\n" " case 3:\n" " y++;\n" " }\n" " bar(y);\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y++;\n" " case 3:\n" " y++;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y--;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:10] -> [test.cpp:9:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " {\n" " y--;\n" " }\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:8:10] -> [test.cpp:11:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y = 2;\n" " case 3:\n" " y--;\n" " }\n" " bar(y);\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y--;\n" " case 3:\n" " y--;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y++;\n" " case 3:\n" " if (x)\n" " {\n" " y = 3;\n" " }\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " {\n" " y++;\n" " if (y)\n" " printf(\"%d\", y);\n" " }\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int x = a;\n" " int y = 1;\n" " switch (x)\n" " {\n" " case 2:\n" " x++;\n" " case 3:\n" " y++;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (x)\n" " {\n" " case 2:\n" " y++;\n" " break;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " while(xyz()) {\n" " switch (x)\n" " {\n" " case 2:\n" " y++;\n" " continue;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " while(xyz()) {\n" " switch (x)\n" " {\n" " case 2:\n" " y++;\n" " throw e;\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (x)\n" " {\n" " case 2:\n" " y++;\n" " printf(\"%d\", y);\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int y = 1;\n" " switch (x)\n" " {\n" " case 2:\n" " y++;\n" " bar();\n" " case 3:\n" " y = 3;\n" " }\n" " bar(y);\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:10] -> [test.cpp:10:11]: (style) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? [redundantAssignInSwitch]\n", errout_str()); check("bool f() {\n" " bool ret = false;\n" " switch (switchCond) {\n" " case 1:\n" " ret = true;\n" " break;\n" " case 31:\n" " ret = true;\n" " break;\n" " case 54:\n" " ret = true;\n" " break;\n" " };\n" " ret = true;\n" " return ret;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:13] -> [test.cpp:14:9]: (style) Variable 'ret' is reassigned a value before the old one has been used. [redundantAssignment]\n" "[test.cpp:8:13] -> [test.cpp:14:9]: (style) Variable 'ret' is reassigned a value before the old one has been used. [redundantAssignment]\n" "[test.cpp:11:13] -> [test.cpp:14:9]: (style) Variable 'ret' is reassigned a value before the old one has been used. [redundantAssignment]\n", errout_str()); } void switchRedundantBitwiseOperationTest() { check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y |= 3;\n" " case 3:\n" " y |= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:9]: (style) Redundant bitwise operation on 'y' in 'switch' statement. 'break;' missing? [redundantBitwiseOperationInSwitch]\n", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y = y | 3;\n" " case 3:\n" " y = y | 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:9]: (style) Redundant bitwise operation on 'y' in 'switch' statement. 'break;' missing? [redundantBitwiseOperationInSwitch]\n", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y |= 3;\n" " default:\n" " y |= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:9]: (style) Redundant bitwise operation on 'y' in 'switch' statement. 'break;' missing? [redundantBitwiseOperationInSwitch]\n", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y |= 3;\n" " default:\n" " if (z)\n" " y |= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y |= z;\n" " z++;\n" " default:\n" " y |= z;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y |= 3;\n" " bar(y);\n" " case 3:\n" " y |= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y |= 3;\n" " y = 4;\n" " case 3:\n" " y |= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:11] -> [test.cpp:8:11]: (style) Variable 'y' is reassigned a value before the old one has been used. [redundantAssignment]\n", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y &= 3;\n" " case 3:\n" " y &= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:9]: (style) Redundant bitwise operation on 'y' in 'switch' statement. 'break;' missing? [redundantBitwiseOperationInSwitch]\n", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y |= 3;\n" " break;\n" " case 3:\n" " y |= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y ^= 3;\n" " case 3:\n" " y ^= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y |= 2;\n" " case 3:\n" " y |= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y &= 2;\n" " case 3:\n" " y &= 3;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a)\n" "{\n" " int y = 1;\n" " switch (a)\n" " {\n" " case 2:\n" " y |= 2;\n" " case 3:\n" " y &= 2;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void unreachableCode() { check("void foo(int a) {\n" " while(1) {\n" " if (a++ >= 100) {\n" " break;\n" " continue;\n" " }\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:5:13]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("int foo(int a) {\n" " return 0;\n" " return(a-1);\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:3:5]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("int foo(int a) {\n" " A:" " return(0);\n" " goto A;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:3:5]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); constexpr char xmldata[] = "\n" "\n" " \n" " true\n" " \n" " \n" ""; /*const*/ Settings settings = settingsBuilder().libraryxml(xmldata).severity(Severity::style).build(); check("void foo() {\n" " exit(0);\n" " break;\n" "}\n", dinit(CheckOptions, $.inconclusive = false, $.settings = &settings)); ASSERT_EQUALS("[test.cpp:3:5]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("class NeonSession {\n" " void exit();\n" "};\n" "void NeonSession::exit()\n" "{\n" " SAL_INFO(\"ucb.ucp.webdav\", \"neon commands cannot be aborted\");\n" "}\n", dinit(CheckOptions, $.inconclusive = false, $.settings = &settings)); ASSERT_EQUALS("", errout_str()); check("void NeonSession::exit()\n" "{\n" " SAL_INFO(\"ucb.ucp.webdav\", \"neon commands cannot be aborted\");\n" "}\n", dinit(CheckOptions, $.inconclusive = false, $.settings = &settings)); ASSERT_EQUALS("", errout_str()); check("void foo() { xResAccess->exit(); }\n", dinit(CheckOptions, $.inconclusive = false, $.settings = &settings)); ASSERT_EQUALS("", errout_str()); check("void foo(int a)\n" "{\n" " switch(a) {\n" " case 0:\n" " printf(\"case 0\");\n" " break;\n" " break;\n" " case 1:\n" " c++;\n" " break;\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:7:13]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("void foo(int a)\n" "{\n" " switch(a) {\n" " case 0:\n" " printf(\"case 0\");\n" " break;\n" " case 1:\n" " c++;\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a)\n" "{\n" " while(true) {\n" " if (a++ >= 100) {\n" " break;\n" " break;\n" " }\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:6:13]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("void foo(int a)\n" "{\n" " while(true) {\n" " if (a++ >= 100) {\n" " continue;\n" " continue;\n" " }\n" " a+=2;\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:6:13]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("void foo(int a)\n" "{\n" " while(true) {\n" " if (a++ >= 100) {\n" " continue;\n" " }\n" " a+=2;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int foo() {\n" " throw 0;\n" " return 1;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:3:5]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("void foo() {\n" " throw 0;\n" " return;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:3:5]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("int foo() {\n" " throw = 0;\n" " return 1;\n" "}\n", dinit(CheckOptions, $.cpp = false, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("int foo() {\n" " return 0;\n" " return 1;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:3:5]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("int foo() {\n" " return 0;\n" " foo();\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:3:5]: (style) Statements following 'return' will never be executed. [unreachableCode]\n", errout_str()); check("int foo(int unused) {\n" " return 0;\n" " (void)unused;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("int foo(int unused1, int unused2) {\n" " return 0;\n" " (void)unused1;\n" " (void)unused2;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("int foo(int unused1, int unused2) {\n" " return 0;\n" " (void)unused1;\n" " (void)unused2;\n" " foo();\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:5:5]: (style) Statements following 'return' will never be executed. [unreachableCode]\n", errout_str()); check("int foo() {\n" " if(bar)\n" " return 0;\n" " return 124;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int foo() {\n" " while(bar) {\n" " return 0;\n" " return 0;\n" " return 0;\n" " return 0;\n" " }\n" " return 124;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:4:9]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); check("void foo() {\n" " while(bar) {\n" " return;\n" " break;\n" " }\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:4:9]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); // #5707 check("extern int i,j;\n" "int foo() {\n" " switch(i) {\n" " default: j=1; break;\n" " }\n" " return 0;\n" " j=2;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:7:5]: (style) Statements following 'return' will never be executed. [unreachableCode]\n", errout_str()); check("int foo() {\n" " return 0;\n" " label:\n" " throw 0;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:3:3]: (style) Label 'label' is not used. [unusedLabel]\n", errout_str()); check("struct A {\n" " virtual void foo (P & Val) throw ();\n" " virtual void foo1 (P & Val) throw ();\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int foo() {\n" " goto label;\n" " while (true) {\n" " bar();\n" " label:\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #3457 check("int foo() {\n" " goto label;\n" " do {\n" " bar();\n" " label:\n" " } while (true);\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #3457 check("int foo() {\n" " goto label;\n" " for (;;) {\n" " bar();\n" " label:\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #3457 // #3383. TODO: Use preprocessor check("int foo() {\n" "\n" // #ifdef A " return 0;\n" "\n" // #endif " return 1;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("int foo() {\n" "\n" // #ifdef A " return 0;\n" "\n" // #endif " return 1;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:5]: (style, inconclusive) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n", errout_str()); // #4711 lambda functions check("int f() {\n" " return g([](int x){(void)x+1; return x;});\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); // #4756 check("template <>\n" "inline uint16_t htobe(uint16_t value) {\n" " return ( __extension__ ({\n" " register unsigned short int __v, __x = (unsigned short int) (value);\n" " if (__builtin_constant_p (__x))\n" " __v = ((unsigned short int) ((((__x) >> 8) & 0xff) | (((__x) & 0xff) << 8)));\n" " else\n" " __asm__ (\"rorw $8, %w0\" : \"=r\" (__v) : \"0\" (__x) : \"cc\");\n" " (void)__v;\n" " }));\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); // #6008 check("static std::function< int ( int, int ) > GetFunctor() {\n" " return [](int a_, int b_) -> int {\n" " int sum = a_ + b_;\n" " return sum;\n" " };\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); // #5789 check("struct per_state_info {\n" " uint64_t enter, exit;\n" " uint64_t events;\n" " per_state_info() : enter(0), exit(0), events(0) {}\n" "};\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); // #6664 check("void foo() {\n" " (beat < 100) ? (void)0 : exit(0);\n" " bar();\n" "}\n", dinit(CheckOptions, $.inconclusive = false, $.settings = &settings)); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " (beat < 100) ? exit(0) : (void)0;\n" " bar();\n" "}\n", dinit(CheckOptions, $.inconclusive = false, $.settings = &settings)); ASSERT_EQUALS("", errout_str()); // #8261 // TODO Do not throw AST validation exception TODO_ASSERT_THROW(check("void foo() {\n" " (beat < 100) ? (void)0 : throw(0);\n" " bar();\n" "}\n", dinit(CheckOptions, $.inconclusive = false, $.settings = &settings)), InternalError); //ASSERT_EQUALS("", errout_str()); check("int foo() {\n" " exit(0);\n" " return 1;\n" // <- clarify for tools that function does not continue.. "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " enum : uint8_t { A, B } var = A;\n" "}\n"); ASSERT_EQUALS("", errout_str()); checkP("#define INB(x) __extension__ ({ u_int tmp = (x); inb(tmp); })\n" // #4739 "static unsigned char cmos_hal_read(unsigned index) {\n" " unsigned short port_0, port_1;\n" " assert(!verify_cmos_byte_index(index));\n" " if (index < 128) {\n" " port_0 = 0x70;\n" " port_1 = 0x71;\n" " }\n" " else {\n" " port_0 = 0x72;\n" " port_1 = 0x73;\n" " }\n" " OUTB(index, port_0);\n" " return INB(port_1);\n" "}\n", dinit(CheckPOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); check("[[noreturn]] void n();\n" "void f() {\n" " n();\n" " g();\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:5]: (style) Statements following noreturn function 'n()' will never be executed. [unreachableCode]\n", errout_str()); check("void f() {\n" " exit(1);\n" " g();\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:5]: (style) Statements following noreturn function 'exit()' will never be executed. [unreachableCode]\n", errout_str()); check("void f() {\n" " do {\n" " break;\n" " g();\n" " } while (0);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:9]: (style) Statements following 'break' will never be executed. [unreachableCode]\n", errout_str()); check("void f() {\n" // #12244 " {\n" " std::cout << \"x\";\n" " return;\n" " }\n" " std::cout << \"y\";\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:5]: (style) Statements following 'return' will never be executed. [unreachableCode]\n", errout_str()); check("void f() {\n" " {\n" " std::cout << \"x\";\n" " exit(1);\n" " }\n" " std::cout << \"y\";\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:5]: (style) Statements following noreturn function 'exit()' will never be executed. [unreachableCode]\n", errout_str()); check("int f() {\n" // #13475 " { return 0; };\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int i) {\n" // #13478 " int x = 0;\n" " switch (i) {\n" " { case 0: x = 5; break; }\n" " { case 1: x = 7; break; }\n" " }\n" " return x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int c) {\n" " switch (c) {\n" " case '\\n':\n" " { return 1; };\n" " default:\n" " { return c; };\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int main(int argc, char *argv[]) {\n" // #11 " switch (argc) {\n" " case 0: {\n" " return 1;\n" " }\n" " break;\n" " }\n" " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:9]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. [duplicateBreak]\n" "[test.cpp:1:26]: (style) Parameter 'argv' can be declared as const array [constParameter]\n", errout_str()); check("int f(int i) {\n" // #13491 " switch (i) {\n" " case 0:\n" " return 0;\n" " int j;\n" " case 1:\n" " case 2:\n" " j = 5;\n" " return j + i;\n" " }\n" " return 3;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int i) {\n" " switch (i) {\n" " {\n" " case 0:\n" " return 0;\n" " }\n" " {\n" " int j;\n" " case 1:\n" " case 2:\n" " j = 5;\n" " return j + i;\n" " }\n" " }\n" " return 3;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int i) {\n" " switch (i) {\n" " case 0:\n" " return 0;\n" " int a[1];\n" " case 1:\n" " case 2:\n" " a[0] = 5;\n" " return a[0] + i;\n" " }\n" " return 3;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int i) {\n" " switch (i) {\n" " case 0:\n" " return 0;\n" " int j;\n" " dostuff();\n" " case 1:\n" " case 2:\n" " j = 5;\n" " return j + i;\n" " }\n" " return 3;\n" "}\n"); TODO_ASSERT_EQUALS("[test.cpp:6:5]: (style) Statements following 'return' will never be executed. [unreachableCode]\n", "", errout_str()); check("int f() {\n" // #13472 " int var;\n" " auto int ret();\n" " int ret() {\n" " return var;\n" " }\n" " var = 42;\n" " return ret();\n" "}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #13516 " io_uring_for_each_cqe(&ring, head, cqe) {\n" " if (cqe->res == -EOPNOTSUPP)\n" " printf(\"error\");\n" " goto ok;\n" " }\n" " usleep(10000);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void unreachableSwitchCase() { check("enum T { A, B};\n" "void f(const T &t) {\n" " if (t == A) {\n" " switch (t) {\n" " case A:\n" " break;\n" " case B:\n" " break;\n" " }\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:9]: (style) Switch case 'B' can never be selected because the switch condition is known to be 0. [unreachableSwitchCase]\n", errout_str()); check("void f(int t) {\n" " if (t == 0) {\n" " switch (t) {\n" " case 0:\n" " break;\n" " case 1:\n" " break;\n" " }\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:9]: (style) Switch case '1' can never be selected because the switch condition is known to be 0. [unreachableSwitchCase]\n", errout_str()); check("void f(int t) {\n" " switch (t) {\n" " case 0:\n" " break;\n" " case 1:\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x, int y) {\n" " if (x == 0) {\n" " switch (x) {\n" " case 0:\n" " switch (y) {\n" " case 1:\n" " break;\n" " case 2:\n" " break;\n" " }\n" " break;\n" " case 1:\n" " break;\n" " }\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:12:9]: (style) Switch case '1' can never be selected because the switch condition is known to be 0. [unreachableSwitchCase]\n", errout_str()); } void redundantContinue() { check("void f() {\n" // #11195 " for (int i = 0; i < 10; ++i) {\n" " printf(\"i = %d\\n\", i);\n" " continue;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:9]: (style) 'continue' is redundant since it is the last statement in a loop. [redundantContinue]\n", errout_str()); check("void f() {\n" " int i = 0;" " do {\n" " ++i;\n" " printf(\"i = %d\\n\", i);\n" " continue;\n" " } while (i < 10);\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:9]: (style) 'continue' is redundant since it is the last statement in a loop. [redundantContinue]\n", errout_str()); check("int f() {\n" // #13475 " { return 0; };\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int i) {\n" // #13478 " int x = 0;\n" " switch (i) {\n" " { case 0: x = 5; break; }\n" " { case 1: x = 7; break; }\n" " }\n" " return x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("bool f(int x, int y) {\n" // #13544 " switch (x) {\n" " case 1: {\n" " return y != 0;\n" " int z = y + 5;\n" " return z != 7;\n" " }\n" " }\n" " return false;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:9]: (style) Statements following 'return' will never be executed. [unreachableCode]\n", errout_str()); } void suspiciousCase() { check("void foo() {\n" " switch(a) {\n" " case A&&B:\n" " foo();\n" " case (A||B):\n" " foo();\n" " case A||B:\n" " foo();\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:15]: (warning, inconclusive) Found suspicious case label in switch(). Operator '&&' probably doesn't work as intended. [suspiciousCase]\n" "[test.cpp:5:16]: (warning, inconclusive) Found suspicious case label in switch(). Operator '||' probably doesn't work as intended. [suspiciousCase]\n" "[test.cpp:7:15]: (warning, inconclusive) Found suspicious case label in switch(). Operator '||' probably doesn't work as intended. [suspiciousCase]\n", errout_str()); check("void foo() {\n" " switch(a) {\n" " case 1:\n" " a=A&&B;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); // TODO Do not throw AST validation exception TODO_ASSERT_THROW(check("void foo() {\n" " switch(a) {\n" " case A&&B?B:A:\n" " foo();\n" " }\n" "}\n"), InternalError); //ASSERT_EQUALS("", errout_str()); } void suspiciousEqualityComparison() { check("void foo(int c) {\n" " if (x) c == 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:14]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str()); check("void foo(const int* c) {\n" " if (x) *c == 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:15]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str()); check("void foo(int c) {\n" " if (c == 1) {\n" " c = 0;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int c) {\n" " c == 1;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:7]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str()); check("void foo(int c) {\n" " for (int i = 0; i == 10; i ++) {\n" " a ++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int c) {\n" " for (i == 0; i < 10; i ++) {\n" " c ++;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:12]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str()); check("void foo(int c) {\n" " for (i == 1; i < 10; i ++) {\n" " c ++;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:12]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str()); check("void foo(int c) {\n" " for (i == 2; i < 10; i ++) {\n" " c ++;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:12]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str()); check("void foo(int c) {\n" " for (int i = 0; i < 10; i == c) {\n" " c ++;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:31]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str()); check("void foo(int c) {\n" " for (; running == 1;) {\n" " c ++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int c) {\n" " printf(\"%i\", ({x==0;}));\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int arg) {\n" " printf(\"%i\", ({int x = do_something(); x == 0;}));\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int x) {\n" " printf(\"%i\", ({x == 0; x > 0 ? 10 : 20}));\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:22]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str()); check("void foo(int x) {\n" " for (const Token* end = tok->link(); tok != end; tok = (tok == end) ? end : tok->next()) {\n" " x++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int x) {\n" " for (int i = (x == 0) ? 0 : 5; i < 10; i ++) {\n" " x++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int x) {\n" " for (int i = 0; i < 10; i += (x == 5) ? 1 : 2) {\n" " x++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void suspiciousUnaryPlusMinus() { // #8004 check("int g() { return 1; }\n" "void f() {\n" " +g();\n" " -g();\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:5]: (warning, inconclusive) Found suspicious operator '+', result is not used. [constStatement]\n" "[test.cpp:4:5]: (warning, inconclusive) Found suspicious operator '-', result is not used. [constStatement]\n", errout_str()); check("void f(int i) {\n" " +i;\n" " -i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:5]: (warning, inconclusive) Found suspicious operator '+', result is not used. [constStatement]\n" "[test.cpp:3:5]: (warning, inconclusive) Found suspicious operator '-', result is not used. [constStatement]\n", errout_str()); } void suspiciousFloatingPointCast() { check("double f(double a, double b, float c) {\n" " return a + (float)b + c;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) Floating-point cast causes loss of precision. [suspiciousFloatingPointCast]\n", errout_str()); check("double f(double a, double b, float c) {\n" " return a + static_cast(b) + c;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:34]: (style) Floating-point cast causes loss of precision. [suspiciousFloatingPointCast]\n", errout_str()); check("long double f(long double a, long double b, float c) {\n" " return a + (double)b + c;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) Floating-point cast causes loss of precision. [suspiciousFloatingPointCast]\n", errout_str()); check("void g(int, double);\n" "void h(double);\n" "void f(double d) {\n" " g(1, (float)d);\n" " h((float)d);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:10]: (style) Floating-point cast causes loss of precision. [suspiciousFloatingPointCast]\n" "[test.cpp:5:7]: (style) Floating-point cast causes loss of precision. [suspiciousFloatingPointCast]\n", errout_str()); } void selfAssignment() { check("void foo()\n" "{\n" " int x = 1;\n" " x = x;\n" " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:7]: (style) Redundant assignment of 'x' to itself. [selfAssignment]\n", errout_str()); check("void foo()\n" "{\n" " int x = x;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:11]: (style) Redundant assignment of 'x' to itself. [selfAssignment]\n", errout_str()); check("struct A { int b; };\n" "void foo(A* a1, A* a2) {\n" " a1->b = a1->b;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:11]: (style) Redundant assignment of 'a1->b' to itself. [selfAssignment]\n", errout_str()); check("int x;\n" "void f()\n" "{\n" " x = x = 3;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:7]: (style) Redundant assignment of 'x' to itself. [selfAssignment]\n", errout_str()); // #4073 (segmentation fault) check("void Foo::myFunc( int a )\n" "{\n" " if (a == 42)\n" " a = a;\n" "}\n"); check("void foo()\n" "{\n" " int x = 1;\n" " x = x + 1;\n" " return 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo()\n" "{\n" " int *x = getx();\n" " *x = x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " BAR *x = getx();\n" " x = x;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:7]: (style) Redundant assignment of 'x' to itself. [selfAssignment]\n", errout_str()); // #2502 - non-primitive type -> there might be some side effects check("void foo()\n" "{\n" " Fred fred; fred = fred;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x) {\n" " x = (x == 0);" " func(x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x) {\n" " x = (x != 0);" " func(x);\n" "}\n"); ASSERT_EQUALS("", errout_str()); // ticket #3001 - false positive check("void foo(int x) {\n" " x = x ? x : 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #3800 - false negative when variable is extern check("extern int i;\n" "void f() {\n" " i = i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:7]: (style) Redundant assignment of 'i' to itself. [selfAssignment]\n", errout_str()); // #4291 - id for variables accessed through 'this' check("class Foo {\n" " int var;\n" " void func();\n" "};\n" "void Foo::func() {\n" " this->var = var;\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:15]: (style) Redundant assignment of 'this->var' to itself. [selfAssignment]\n", errout_str()); check("class Foo {\n" " int var;\n" " Foo(int var);\n" "};\n" "Foo::Foo(int var) {\n" " this->var = var;\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #6406 - designated initializer doing bogus self assignment check("struct callbacks {\n" " void (*s)(void);\n" "};\n" "void something(void) {}\n" "void f() {\n" " struct callbacks ops = { .s = ops.s };\n" "}\n"); TODO_ASSERT_EQUALS("[test.cpp:6]: (style) Redundant assignment of 'something' to itself.\n", "", errout_str()); check("class V\n" "{\n" "public:\n" " V()\n" " {\n" " x = y = z = 0.0;\n" " }\n" " V( double x, const double y_, const double &z_)\n" " {\n" " x = x; y = y; z = z;\n" " }\n" " double x, y, z;\n" "};\n"); ASSERT_EQUALS("[test.cpp:10:11]: (style) Redundant assignment of 'x' to itself. [selfAssignment]\n" "[test.cpp:10:18]: (style) Redundant assignment of 'y' to itself. [selfAssignment]\n" "[test.cpp:10:25]: (style) Redundant assignment of 'z' to itself. [selfAssignment]\n", errout_str()); check("void f(int i) { i = !!i; }\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " int x = 1;\n" " int &ref = x;\n" " ref = x;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:9]: (style) Redundant assignment of 'ref' to itself. [selfAssignment]\n", errout_str()); check("class Foo {\n" // #9850 " int i{};\n" " void modify();\n" " void method() {\n" " Foo copy = *this;\n" " modify();\n" " *this = copy;\n" " }\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("struct S {\n" // #11383 " void f() {\n" " int x = 42;" " auto l2 = [i = i, x, y = 0]() { return i + x + y; };\n" " }\n" " int i;\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #10337 " int b[2] = { 1, 2 };\n" " int idx = 0;\n" " int& i = b[idx];\n" " idx++;\n" " i = b[idx];\n" "};\n"); ASSERT_EQUALS("", errout_str()); check("void g(int*);\n" // #12390 "void f() {\n" " int o = s.i;\n" " g(&s.i);\n" " s.i = o;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int N;\n" // #14234 "void f() { ::N = N; }\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) Redundant assignment of '::N' to itself. [selfAssignment]\n", errout_str()); } void trac1132() { check("class Lock\n" "{\n" "public:\n" " Lock(int i)\n" " {\n" " std::cout << \"Lock \" << i << std::endl;\n" " }\n" " ~Lock()\n" " {\n" " std::cout << \"~Lock\" << std::endl;\n" " }\n" "};\n" "int main()\n" "{\n" " Lock(123);\n" " std::cout << \"hello\" << std::endl;\n" " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:15:5]: (style) Instance of 'Lock' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); } void trac3693() { check("struct A{\n" " enum {\n" " b = 300\n" " };\n" "};\n" "const int DFLT_TIMEOUT = A::b % 1000000 ;\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectDoesNotPickFunction1() { check("int main ( )\n" "{\n" " CouldBeFunction ( 123 ) ;\n" " return 0 ;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectDoesNotPickFunction2() { check("struct error {\n" " error() {}\n" "};\n" "\n" "class parser {\n" "public:\n" " void error() const {}\n" "\n" " void foo() const {\n" " error();\n" " do_something();\n" " }\n" "};\n"); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectPicksClass() { check("class NotAFunction ;\n" "int function ( )\n" "{\n" " NotAFunction ( 123 );\n" " return 0 ;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:5]: (style) Instance of 'NotAFunction' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); } void testMisusedScopeObjectPicksStruct() { check("struct NotAClass;\n" "bool func ( )\n" "{\n" " NotAClass ( 123 ) ;\n" " return true ;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:5]: (style) Instance of 'NotAClass' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); } void testMisusedScopeObjectDoesNotPickIf() { check("bool func( int a , int b , int c )\n" "{\n" " if ( a > b ) return c == a ;\n" " return b == a ;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectDoesNotPickConstructorDeclaration() { check("class Something : public SomethingElse\n" "{\n" "public:\n" "~Something ( ) ;\n" "Something ( ) ;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectDoesNotPickFunctor() { check("class IncrementFunctor\n" "{\n" "public:\n" " void operator()(int &i)\n" " {\n" " ++i;\n" " }\n" "};\n" "\n" "int main()\n" "{\n" " int a = 1;\n" " IncrementFunctor()(a);\n" " return a;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectDoesNotPickLocalClassConstructors() { check("void f() {\n" " class Foo {\n" " Foo() { }\n" " Foo(int a) { }\n" " Foo(int a, int b) { }\n" " };\n" " Foo();\n" " do_something();\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:5]: (style) Instance of 'Foo' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); } void testMisusedScopeObjectDoesNotPickUsedObject() { check("struct Foo {\n" " void bar() {\n" " }\n" "};\n" "\n" "void fn() {\n" " Foo().bar();\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectDoesNotPickPureC() { // Ticket #2352 const char code[] = "struct cb_watch_bool {\n" " int a;\n" "};\n" "\n" "void f()\n" "{\n" " cb_watch_bool();\n" " do_something();\n" "}\n"; check(code); ASSERT_EQUALS("[test.cpp:7:5]: (style) Instance of 'cb_watch_bool' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); check(code, dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); // Ticket #2639 check("struct stat { int a; int b; };\n" "void stat(const char *fn, struct stat *);\n" "\n" "void foo() {\n" " stat(\"file.txt\", &st);\n" " do_something();\n" "}\n"); ASSERT_EQUALS("",errout_str()); check("struct AMethodObject {\n" // #4336 " AMethodObject(double, double, double);\n" "};\n" "struct S {\n" " static void A(double a1, double a2, double a3);\n" "};\n" "void S::A(double const a1, double const a2, double const a3) {\n" " AMethodObject(a1, a2, a3);\n" "}\n"); ASSERT_EQUALS("",errout_str()); } void testMisusedScopeObjectDoesNotPickNestedClass() { const char code[] = "class ios_base {\n" "public:\n" " class Init {\n" " public:\n" " };\n" "};\n" "class foo {\n" "public:\n" " foo();\n" " void Init(int);\n" "};\n" "foo::foo() {\n" " Init(0);\n" " do_something();\n" "}\n"; check(code); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectInConstructor() { const char code[] = "class Foo {\n" "public:\n" " Foo(char x) {\n" " Foo(x, 0);\n" " do_something();\n" " }\n" " Foo(char x, int y) { }\n" "};\n"; check(code); ASSERT_EQUALS("[test.cpp:4:5]: (style) Instance of 'Foo' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); } void testMisusedScopeObjectStandardType() { check("int g();\n" "void f(int i) {\n" " int();\n" " int(0);\n" " int( g() );\n" // don't warn " int{};\n" " int{ 0 };\n" " int{ i };\n" " int{ g() };\n" // don't warn " g();\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:5]: (style) Instance of 'int' object is destroyed immediately. [unusedScopedObject]\n" "[test.cpp:4:5]: (style) Instance of 'int' object is destroyed immediately. [unusedScopedObject]\n" "[test.cpp:6:5]: (style) Instance of 'int' object is destroyed immediately. [unusedScopedObject]\n" "[test.cpp:7:5]: (style) Instance of 'int' object is destroyed immediately. [unusedScopedObject]\n" "[test.cpp:8:5]: (style) Instance of 'int' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); check("void f(int j) {\n" " for (; bool(j); ) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void g() {\n" " float (f);\n" " float (*p);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int i) {\n" " void();\n" " return i;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectNamespace() { check("namespace M {\n" // #4779 " namespace N {\n" " struct S {};\n" " }\n" "}\n" "int f() {\n" " M::N::S();\n" " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:11]: (style) Instance of 'M::N::S' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); check("void f() {\n" // #10057 " std::string(\"abc\");\n" " std::string{ \"abc\" };\n" " std::pair(1, 2);\n" " (void)0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:5]: (warning) Redundant code: Found unused cast in expression '(void)0'. [constStatement]\n" "[test.cpp:2:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n" "[test.cpp:3:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n" "[test.cpp:4:10]: (style) Instance of 'std::pair' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); check("struct S {\n" // #10083 " void f() {\n" " std::lock_guard<:mutex>(m);\n" " }\n" " void g() {\n" " std::scoped_lock<:mutex>(m);\n" " }\n" " void h() {\n" " std::scoped_lock(m);\n" " }\n" " std::mutex m;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:14]: (style) Instance of 'std::lock_guard' object is destroyed immediately. [unusedScopedObject]\n" "[test.cpp:6:14]: (style) Instance of 'std::scoped_lock' object is destroyed immediately. [unusedScopedObject]\n" "[test.cpp:9:14]: (style) Instance of 'std::scoped_lock' object is destroyed immediately. [unusedScopedObject]\n", errout_str()); check("struct S { int i; };\n" "namespace {\n" " S s() { return ::S{42}; }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void testMisusedScopeObjectAssignment() { // #11371 check("struct S;\n" "S f();\n" "S& g();\n" "S&& h();\n" "S* i();\n" "void t0() { f() = {}; }\n" "void t1() { g() = {}; }\n" "void t2() { h() = {}; }\n" "void t3() { *i() = {}; }\n"); ASSERT_EQUALS("[test.cpp:6:19]: (style) Instance of 'S' object is destroyed immediately, assignment has no effect. [unusedScopedObject]\n", errout_str()); } void trac2084() { check("void f()\n" "{\n" " struct sigaction sa;\n" "\n" " { sigaction(SIGHUP, &sa, 0); };\n" " { sigaction(SIGINT, &sa, 0); };\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void trac2071() { check("void f() {\n" " struct AB {\n" " AB(int a) { }\n" " };\n" "\n" " const AB ab[3] = { AB(0), AB(1), AB(2) };\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void clarifyCalculation() { check("int f(char c) {\n" " return 10 * (c == 0) ? 1 : 2;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:26]: (style) Clarify calculation precedence for '*' and '?'. [clarifyCalculation]\n", errout_str()); check("void f(char c) {\n" " printf(\"%i\", 10 * (c == 0) ? 1 : 2);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:32]: (style) Clarify calculation precedence for '*' and '?'. [clarifyCalculation]\n", errout_str()); check("void f() {\n" " return (2*a)?b:c;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(char c) {\n" " printf(\"%i\", a + b ? 1 : 2);\n" "}\n",dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:2:24]: (style) Clarify calculation precedence for '+' and '?'. [clarifyCalculation]\n", errout_str()); check("void f() {\n" " std::cout << x << y ? 2 : 3;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:25]: (style) Clarify calculation precedence for '<<' and '?'. [clarifyCalculation]\n", errout_str()); check("void f() {\n" " int ab = a - b ? 2 : 3;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:20]: (style) Clarify calculation precedence for '-' and '?'. [clarifyCalculation]\n", errout_str()); check("void f() {\n" " int ab = a | b ? 2 : 3;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:20]: (style) Clarify calculation precedence for '|' and '?'. [clarifyCalculation]\n", errout_str()); // ticket #195 check("int f(int x, int y) {\n" " return x >> ! y ? 8 : 2;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:21]: (style) Clarify calculation precedence for '>>' and '?'. [clarifyCalculation]\n", errout_str()); check("int f() {\n" " return shift < sizeof(int64_t)*8 ? 1 : 2;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() { a = *p ? 1 : 2; }\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x) { const char *p = x & 1 ? \"1\" : \"0\"; }\n"); ASSERT_EQUALS("", errout_str()); check("void foo() { x = a % b ? \"1\" : \"0\"; }\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x) { return x & 1 ? '1' : '0'; }\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x) { return x & 16 ? 1 : 0; }\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x) { return x % 16 ? 1 : 0; }\n"); ASSERT_EQUALS("", errout_str()); check("enum {X,Y}; void f(int x) { return x & Y ? 1 : 0; }\n"); ASSERT_EQUALS("", errout_str()); } void clarifyStatement() { check("char* f(char* c) {\n" " *c++;\n" " return c;\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:5]: (warning, inconclusive) Found suspicious operator '*', result is not used. [constStatement]\n" "[test.cpp:2:7]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'? [clarifyStatement]\n", errout_str()); check("char* f(char** c) {\n" " *c[5]--;\n" " return *c;\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:5]: (warning, inconclusive) Found suspicious operator '*', result is not used. [constStatement]\n" "[test.cpp:2:10]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'? [clarifyStatement]\n", errout_str()); check("void f(Foo f) {\n" " *f.a++;\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:5]: (warning, inconclusive) Found suspicious operator '*', result is not used. [constStatement]\n" "[test.cpp:2:9]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'? [clarifyStatement]\n", errout_str()); check("void f(Foo f) {\n" " *f.a[5].v[3]++;\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:5]: (warning, inconclusive) Found suspicious operator '*', result is not used. [constStatement]\n" "[test.cpp:2:17]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'? [clarifyStatement]\n", errout_str()); check("void f(Foo f) {\n" " *f.a(1, 5).v[x + y]++;\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:5]: (warning, inconclusive) Found suspicious operator '*', result is not used. [constStatement]\n" "[test.cpp:2:24]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'? [clarifyStatement]\n", errout_str()); check("char* f(char* c) {\n" " (*c)++;\n" " return c;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(char* c) {\n" " bar(*c++);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("char*** f(char*** c) {\n" " ***c++;\n" " return c;\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:5]: (warning, inconclusive) Found suspicious operator '*', result is not used. [constStatement]\n" "[test.cpp:2:9]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'? [clarifyStatement]\n", errout_str()); check("char** f(char*** c) {\n" " **c[5]--;\n" " return **c;\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:5]: (warning, inconclusive) Found suspicious operator '*', result is not used. [constStatement]\n" "[test.cpp:2:11]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'? [clarifyStatement]\n", errout_str()); check("char*** f(char*** c) {\n" " (***c)++;\n" " return c;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const int*** p) {\n" // #10923 " delete[] **p;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void *f(char** c) {\n" " bar(**c++);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void *f(char* p) {\n" " for (p = path; *p++;) ;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " std::array<:array>,3> array;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::vector& v) {\n" // #12088 " for (auto it = v.begin(); it != v.end(); delete *it++);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateBranch() { check("void f(int a, int &b) {\n" " if (a)\n" " b = 1;\n" " else\n" " b = 1;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:5] -> [test.cpp:2:5]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str()); check("void f(int a, int &b) {\n" " if (a) {\n" " if (a == 1)\n" " b = 2;\n" " else\n" " b = 2;\n" " } else\n" " b = 1;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:9] -> [test.cpp:3:9]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str()); check("void f(int a, int &b) {\n" " if (a == 1)\n" " b = 1;\n" " else {\n" " if (a)\n" " b = 2;\n" " else\n" " b = 2;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:7:9] -> [test.cpp:5:9]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str()); check("int f(int signed, unsigned char value) {\n" " int ret;\n" " if (signed)\n" " ret = (signed char)value;\n" // cast must be kept so the simplifications and verification is skipped " else\n" " ret = (unsigned char)value;\n" " return ret;\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " if (b)\n" " __asm__(\"mov ax, bx\");\n" " else\n" " __asm__(\"mov bx, bx\");\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #3407 check("void f() {\n" " if (b)\n" " __asm__(\"mov ax, bx\");\n" " else\n" " __asm__(\"mov ax, bx\");\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:5] -> [test.cpp:2:5]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str()); } void duplicateBranch1() { // tests inspired by http://www.viva64.com/en/b/0149/ ( Comparison between PVS-Studio and cppcheck ) // Errors detected in Quake 3: Arena by PVS-Studio: Fragment 2 check("void f()\n" "{\n" " if (front < 0)\n" " frac = front/(front-back);\n" " else\n" " frac = front/(front-back);\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:3] -> [test.cpp:3:3]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str()); check("void f()\n" "{\n" " if (front < 0)\n" " { frac = front/(front-back);}\n" " else\n" " frac = front/((front-back));\n" "}\n"); ASSERT_EQUALS("[test.cpp:5:3] -> [test.cpp:3:3]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str()); // No message about empty branches (#5354) check("void f()\n" "{\n" " if (front < 0)\n" " {}\n" " else\n" " {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateBranch2() { checkP("#define DOSTUFF1 ;\n" "#define DOSTUFF2 ;\n" "void f(int x) {\n" // #4329 " if (x)\n" " DOSTUFF1\n" " else\n" " DOSTUFF2\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateBranch3() { check("void f(bool b, int i) {\n" " int j = i;\n" " if (b) {\n" " x = i;\n" " } else {\n" " x = j;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:5:7] -> [test.cpp:3:5]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n" "[test.cpp:2:9]: (style) The scope of the variable 'j' can be reduced. [variableScope]\n", errout_str()); check("void f(bool b, int i) {\n" " int j = i;\n" " i++;\n" " if (b) {\n" " x = i;\n" " } else {\n" " x = j;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateBranch4() { check("void* f(bool b) {\n" " if (b) {\n" " return new A::Y(true);\n" " } else {\n" " return new A::Z(true);\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateBranch5() { check("void f(bool b) {\n" " int j;\n" " if (b) {\n" " unsigned int i = 0;\n" " j = i;\n" " } else {\n" " unsigned int i = 0;\n" " j = i;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:7] -> [test.cpp:3:5]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str()); check("void f(bool b) {\n" " int j;\n" " if (b) {\n" " unsigned int i = 0;\n" " j = i;\n" " } else {\n" " unsigned int i = 0;\n" " j = 1;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(bool b) {\n" " int j;\n" " if (b) {\n" " unsigned int i = 0;\n" " } else {\n" " int i = 0;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(bool b) {\n" " int j;\n" " if (b) {\n" " unsigned int i = 0;\n" " j = i;\n" " } else {\n" " int i = 0;\n" " j = i;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int i) {\n" " if (1 == i) {\n" " ;\n" " } else {\n" " ;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateBranch6() { check("void f(bool b) {\n" " if (b) {\n" " } else {\n" " int i = 0;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(bool b) {\n" " if (b) {\n" " int i = 0;\n" " } else {\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression1() { check("void foo(int a) {\n" " if (a == a) { }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str()); check("void fun(int b) {\n" " return a && a ||\n" " b == b &&\n" " d > d &&\n" " e < e &&\n" " f ;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:15]: (style) Same expression on both sides of '&&'. [duplicateExpression]\n" "[test.cpp:3:15]: (style) Same expression on both sides of '=='. [duplicateExpression]\n" "[test.cpp:4:15]: (style) Same expression on both sides of '>'. [duplicateExpression]\n" "[test.cpp:5:15]: (style) Same expression on both sides of '<'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " return a && a;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:14]: (style) Same expression on both sides of '&&'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " a = b && b;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Same expression on both sides of '&&'. [duplicateExpression]\n", errout_str()); check("void foo(int b) {\n" " f(a,b == b);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:11]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str()); check("void foo(int b) {\n" " f(b == b, a);\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:9]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if (x!=2 || x!=2) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:14]: (style) Same expression on both sides of '||'. [duplicateExpression]\n", errout_str()); check("void foo(int a, int b) {\n" " if ((a < b) && (b > a)) { }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: (style) Same expression on both sides of '&&' because 'aa' represent the same value. [knownConditionTrueFalse]\n", errout_str()); check("void foo(int a, int b) {\n" " if ((a <= b) && (b >= a)) { }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:18]: (style) Same expression on both sides of '&&' because 'a<=b' and 'b>=a' represent the same value. [knownConditionTrueFalse]\n", errout_str()); check("void foo() {\n" " if (x!=2 || y!=3 || x!=2) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:22]: (style) Same expression 'x!=2' found multiple times in chain of '||' operators. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if (x!=2 && (x=y) && x!=2) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " if (a && b || a && b) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) Same expression on both sides of '||'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if (a && b || b && c) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " if (a && b | b && c) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) Same expression on both sides of '|'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if ((a + b) | (a + b)) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: (style) Same expression on both sides of '|'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if ((a | b) & (a | b)) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: (style) Same expression on both sides of '&'. [duplicateExpression]\n", errout_str()); check("void foo(int a, int b) {\n" " if ((a | b) == (a | b)) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if (a1[a2[c & 0xff] & 0xff]) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void d(const char f, int o, int v)\n" "{\n" " if (((f=='R') && (o == 1) && ((v < 2) || (v > 99))) ||\n" " ((f=='R') && (o == 2) && ((v < 2) || (v > 99))) ||\n" " ((f=='T') && (o == 2) && ((v < 200) || (v > 9999)))) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int x) { return x+x; }\n"); ASSERT_EQUALS("", errout_str()); check("void f(int x) { while (x+=x) ; }\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " if (a && b && b) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) Same expression on both sides of '&&'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if (a || b || b) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:16]: (style) Same expression on both sides of '||'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if (a / 1000 / 1000) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int foo(int i) {\n" " return i/i;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:13]: (style) Same expression on both sides of '/'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if (a << 1 << 1) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f() { return !!y; }\n"); // No FP ASSERT_EQUALS("", errout_str()); // make sure there are not "same expression" fp when there are different casts check("void f(long x) { if ((int32_t)x == (int64_t)x) {} }\n", dinit(CheckOptions, $.inconclusive = false) ); ASSERT_EQUALS("", errout_str()); // make sure there are not "same expression" fp when there are different ({}) expressions check("void f(long x) { if (({ 1+2; }) == ({3+4;})) {} }\n"); ASSERT_EQUALS("", errout_str()); // #5535: Reference named like its type check("void foo() { UMSConfig& UMSConfig = GetUMSConfiguration(); }\n"); ASSERT_EQUALS("[test.cpp:1:25]: (style) Variable 'UMSConfig' can be declared as reference to const [constVariableReference]\n", errout_str()); // #3868 - false positive (same expression on both sides of |) check("void f(int x) {\n" " a = x ? A | B | C\n" " : A | B;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const Bar &bar) {\n" " bool a = bar.isSet() && bar->isSet();\n" " bool b = bar.isSet() && bar.isSet();\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:26]: (style) Same expression on both sides of '&&'. [duplicateExpression]\n", errout_str()); check("void foo(int a, int b) {\n" " if ((b + a) | (a + b)) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: (style) Same expression on both sides of '|' because 'b+a' and 'a+b' represent the same value. [duplicateExpression]\n", errout_str()); check("void foo(const std::string& a, const std::string& b) {\n" " return a.find(b+\"&\") || a.find(\"&\"+b);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a, int b) {\n" " if ((b > a) | (a > b)) {}\n" // > is not commutative "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(double a, double b) {\n" " if ((b + a) > (a + b)) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: (style) The comparison 'b+a > a+b' is always false because 'b+a' and 'a+b' represent the same value. [knownConditionTrueFalse]\n", errout_str()); check("void f(int x) {\n" " if ((x == 1) && (x == 0x00000001))\n" " a++;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:18]: (style) Same expression on both sides of '&&' because 'x==1' and 'x==0x00000001' represent the same value. [knownConditionTrueFalse]\n", errout_str()); check("void f() {\n" " enum { Four = 4 };\n" " if (Four == 4) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:14]: (style) The comparison 'Four == 4' is always true. [knownConditionTrueFalse]\n", errout_str()); check("void f() {\n" " enum { Four = 4 };\n" " static_assert(Four == 4, \"\");\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " enum { Four = 4 };\n" " _Static_assert(Four == 4, \"\");\n" "}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #15020 " enum { Four = 4 };\n" " static_assert(Four == 4, \"\");\n" "}\n", dinit(CheckOptions, $.cpp = false)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " enum { Four = 4 };\n" " static_assert(4 == Four, \"\");\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " enum { FourInEnumOne = 4 };\n" " enum { FourInEnumTwo = 4 };\n" " if (FourInEnumOne == FourInEnumTwo) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:23]: (style) The comparison 'FourInEnumOne == FourInEnumTwo' is always true because 'FourInEnumOne' and 'FourInEnumTwo' represent the same value. [knownConditionTrueFalse]\n", errout_str()); check("void f() {\n" " enum { FourInEnumOne = 4 };\n" " enum { FourInEnumTwo = 4 };\n" " static_assert(FourInEnumOne == FourInEnumTwo, \"\");\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo(int a, int b) {\n" " if (sizeof(a) == sizeof(a)) { }\n" " if (sizeof(a) == sizeof(b)) { }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:19]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str()); check("float bar(int) __attribute__((pure));\n" "char foo(int) __attribute__((pure));\n" "int test(int a, int b) {\n" " if (bar(a) == bar(a)) { }\n" " if (unknown(a) == unknown(a)) { }\n" " if (foo(a) == foo(a)) { }\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:16]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str()); } void duplicateExpression2() { // check if float is NaN or Inf check("int f(long double ldbl, double dbl, float flt) {\n" // ticket #2730 " if (ldbl != ldbl) have_nan = 1;\n" " if (!(dbl == dbl)) have_nan = 1;\n" " if (flt != flt) have_nan = 1;\n" " return have_nan;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("float f(float x) { return x-x; }\n"); // ticket #4485 (Inf) ASSERT_EQUALS("", errout_str()); check("float f(float x) { return (X double)x == (X double)x; }\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("", errout_str()); check("struct X { float f; };\n" "float f(struct X x) { return x.f == x.f; }\n"); ASSERT_EQUALS("", errout_str()); check("struct X { int i; };\n" "int f(struct X x) { return x.i == x.i; }\n"); ASSERT_EQUALS("[test.cpp:2:32]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str()); // #5284 - when type is unknown, assume it's float check("int f() { return x==x; }\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression3() { constexpr char xmldata[] = "\n" "\n" " \n" " \n" " \n" " \n" " \n" ""; /*const*/ Settings settings = settingsBuilder().libraryxml(xmldata).severity(Severity::style).build(); check("void foo() {\n" " if (x() || x()) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A {\n" " void foo() const;\n" " bool bar() const;\n" "};\n" "void A::foo() const {\n" " if (bar() && bar()) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:6:15]: (style) Same expression on both sides of '&&'. [duplicateExpression]\n", errout_str()); check("struct A {\n" " void foo();\n" " bool bar();\n" " bool bar() const;\n" "};\n" "void A::foo() {\n" " if (bar() && bar()) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("class B {\n" " void bar(int i);\n" "};\n" "class A {\n" " void bar(int i) const;\n" "};\n" "void foo() {\n" " B b;\n" " A a;\n" " if (b.bar(1) && b.bar(1)) {}\n" " if (a.bar(1) && a.bar(1)) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:11:18]: (style) Same expression on both sides of '&&'. [duplicateExpression]\n", errout_str()); check("class D { void strcmp(); };\n" "void foo() {\n" " D d;\n" " if (d.strcmp() && d.strcmp()) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " if ((mystrcmp(a, b) == 0) || (mystrcmp(a, b) == 0)) {}\n" "}\n", dinit(CheckOptions, $.inconclusive = false, $.settings = &settings)); ASSERT_EQUALS("[test.cpp:2:31]: (style) Same expression on both sides of '||'. [duplicateExpression]\n", errout_str()); check("void GetValue() { return rand(); }\n" "void foo() {\n" " if ((GetValue() == 0) || (GetValue() == 0)) { dostuff(); }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void __attribute__((const)) GetValue() { return X; }\n" "void foo() {\n" " if ((GetValue() == 0) || (GetValue() == 0)) { dostuff(); }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:27]: (style) Same expression on both sides of '||'. [duplicateExpression]\n", errout_str()); check("void GetValue() __attribute__((const));\n" "void GetValue() { return X; }\n" "void foo() {\n" " if ((GetValue() == 0) || (GetValue() == 0)) { dostuff(); }\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:27]: (style) Same expression on both sides of '||'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if (str == \"(\" || str == \"(\") {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:20]: (style) Same expression on both sides of '||'. [duplicateExpression]\n", errout_str()); check("void foo() {\n" " if (bar(a) && !strcmp(a, b) && bar(a) && !strcmp(a, b)) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #5334 check("void f(C *src) {\n" " if (x(src) || x(src))\n" " a++;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(A *src) {\n" " if (dynamic_cast(src) || dynamic_cast(src)) {}\n" "}\n", dinit(CheckOptions, $.inconclusive = false)); ASSERT_EQUALS("[test.cpp:2:31]: (style) Same expression on both sides of '||'. [duplicateExpression]\n", errout_str()); // #5819 check("Vector func(Vector vec1) {\n" " return fabs(vec1 & vec1 & vec1);\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("Vector func(int vec1) {\n" " return fabs(vec1 & vec1 & vec1);\n" "}\n"); ASSERT_EQUALS( "[test.cpp:2:22]: (style) Same expression on both sides of '&'. [duplicateExpression]\n" "[test.cpp:2:29]: (style) Same expression on both sides of '&'. [duplicateExpression]\n", errout_str()); } void duplicateExpression4() { check("void foo() {\n" " if (*a++ != b || *a++ != b) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " if (*a-- != b || *a-- != b) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); // assignment check("void f() {\n" " while (*(a+=2)==*(b+=2) && *(a+=2)==*(b+=2)) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression5() { // #3749 - macros with same values check("void f() {\n" " if ($a == $a) { }\n" "}\n"); ASSERT_EQUALS("", errout_str()); checkP("#define X 1\n" "#define Y 1\n" "void f() {\n" " if (X == X) {}\n" " if (X == Y) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:11]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str()); checkP("#define X 1\n" "#define Y X\n" "void f() {\n" " if (X == Y) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression6() { // #4639 check("float IsNan(float value) { return !(value == value); }\n" "double IsNan(double value) { return !(value == value); }\n" "long double IsNan(long double value) { return !(value == value); }\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression7() { check("void f() {\n" " const int i = sizeof(int);\n" " if ( i != sizeof (int)){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:25] -> [test.cpp:3:12]: (style) The comparison 'i != sizeof(int)' is always false because 'i' and 'sizeof(int)' represent the same value. [knownConditionTrueFalse]\n", errout_str()); check("void f() {\n" " const int i = sizeof(int);\n" " if ( sizeof (int) != i){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:25] -> [test.cpp:3:23]: (style) The comparison 'sizeof(int) != i' is always false because 'sizeof(int)' and 'i' represent the same value. [knownConditionTrueFalse]\n", errout_str()); check("void f(int a = 1) { if ( a != 1){}}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int a = 1;\n" " if ( a != 1){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:3:12]: (style) The comparison 'a != 1' is always false. [knownConditionTrueFalse]\n", errout_str()); check("void f() {\n" " int a = 1;\n" " int b = 1;\n" " if ( a != b){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:3:13] -> [test.cpp:4:12]: (style) The comparison 'a != b' is always false because 'a' and 'b' represent the same value. [knownConditionTrueFalse]\n", errout_str()); check("void f() {\n" " int a = 1;\n" " int b = a;\n" " if ( a != b){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:13] -> [test.cpp:4:12]: (style) The comparison 'a != b' is always false because 'a' and 'b' represent the same value. [knownConditionTrueFalse]\n", errout_str()); check("void use(int);\n" "void f() {\n" " int a = 1;\n" " int b = 1;\n" " use(b);\n" " if ( a != 1){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:13] -> [test.cpp:6:12]: (style) The comparison 'a != 1' is always false. [knownConditionTrueFalse]\n", errout_str()); check("void use(int);\n" "void f() {\n" " int a = 1;\n" " use(a);\n" " a = 2;\n" " if ( a != 1){}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void use(int);\n" "void f() {\n" " int a = 2;\n" " use(a);\n" " a = 1;\n" " if ( a != 1){}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("const int a = 1;\n" "void f() {\n" " if ( a != 1){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:15] -> [test.cpp:3:12]: (style) The comparison 'a != 1' is always false. [knownConditionTrueFalse]\n", errout_str()); check("int a = 1;\n" " void f() {\n" " if ( a != 1){}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " static const int a = 1;\n" " if ( a != 1){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:26] -> [test.cpp:3:12]: (style) The comparison 'a != 1' is always false. [knownConditionTrueFalse]\n", errout_str()); check("void f() {\n" " static int a = 1;\n" " if ( a != 1){}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int a = 1;\n" " if ( a != 1){\n" " a++;\n" " }}\n"); ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:3:12]: (style) The comparison 'a != 1' is always false. [knownConditionTrueFalse]\n", errout_str()); check("void f(int b) {\n" " int a = 1;\n" " while (b) {\n" " if ( a != 1){}\n" " a++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("bool f(bool a, bool b) {\n" " const bool c = a;\n" " return a && b && c;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:20] -> [test.cpp:3:19]: (style) Same expression 'a' found multiple times in chain of '&&' operators because 'a' and 'c' represent the same value. [knownConditionTrueFalse]\n", errout_str()); // 6906 check("void f(const bool b) {\n" " const bool b1 = !b;\n" " if(!b && b1){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:20] -> [test.cpp:3:10]: (style) Same expression on both sides of '&&' because '!b' and 'b1' represent the same value. [knownConditionTrueFalse]\n", errout_str()); // 7284 check("void f(void) {\n" " if (a || !!a) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:10]: (style) Same expression on both sides of '||' because 'a' and '!!a' represent the same value. [knownConditionTrueFalse]\n", errout_str()); // 8205 check("void f(int x) {\n" " int Diag = 0;\n" " switch (x) {\n" " case 12:\n" " if (Diag==0) {}\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:15] -> [test.cpp:5:16]: (style) The comparison 'Diag == 0' is always true. [knownConditionTrueFalse]\n", errout_str()); // #9744 check("void f(const std::vector& ints) {\n" " int i = 0;\n" " for (int p = 0; i < ints.size(); ++i) {\n" " if (p == 0) {}\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:18] -> [test.cpp:4:15]: (style) The comparison 'p == 0' is always true. [knownConditionTrueFalse]\n", errout_str()); // #11820 check("unsigned f(unsigned x) {\n" " return x - !!x;\n" "}\n" "unsigned g(unsigned x) {\n" " return !!x - x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression8() { check("void f() {\n" " int a = 1;\n" " int b = a;\n" " a = 2;\n" " if ( b != a){}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int * a, int i) { int b = a[i]; a[i] = 2; if ( b != a[i]){}}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int * a, int i) { int b = *a; *a = 2; if ( b != *a){}}\n"); ASSERT_EQUALS("", errout_str()); check("struct A { int f() const; };\n" "A g();\n" "void foo() {\n" " for (A x = A();;) {\n" " const int a = x.f();\n" " x = g();\n" " if (x.f() == a) break;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("int f(int i);\n" "struct A {\n" " enum E { B, C };\n" " bool f(E);\n" "};\n" "void foo() {\n" " A a;\n" " const bool x = a.f(A::B);\n" " const bool y = a.f(A::C);\n" " if(!x && !y) return;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void foo() {\n" " const bool x = a.f(A::B);\n" " const bool y = a.f(A::C);\n" " if (!x && !y) return;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(bool * const b);\n" "void foo() {\n" " bool x = true;\n" " bool y = true;\n" " f(&x);\n" " if (!x && !y) return;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " const int a = {};\n" " if(a == 1) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("volatile const int var = 42;\n" "void f() { if(var == 42) {} }\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int a = 0;\n" " struct b c;\n" " c.a = &a;\n" " g(&c);\n" " if (a == 0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression9() { // #9320 check("void f() {\n" " uint16_t x = 1000;\n" " uint8_t y = x;\n" " if (x != y) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression10() { // #9485 check("int f() {\n" " const int a = 1;\n" " const int b = a-1;\n" " const int c = a+1;\n" " return c;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression11() { check("class Fred {\n" "public:\n" " double getScale() const { return m_range * m_zoom; }\n" " void setZoom(double z) { m_zoom = z; }\n" " void dostuff(int x);\n" "private:\n" " double m_zoom;\n" " double m_range;\n" "};\n" "\n" "void Fred::dostuff(int x) {\n" " if (x == 43) {\n" " double old_scale = getScale();\n" " setZoom(m_zoom + 1);\n" " double scale_ratio = getScale() / old_scale;\n" // <- FP " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression12() { //#10026 check("int f(const std::vector &buffer, const uint8_t index)\n" "{\n" " int var = buffer[index - 1];\n" " return buffer[index - 1] - var;\n" // << "}\n"); ASSERT_EQUALS("[test.cpp:3:25] -> [test.cpp:4:34]: (style) Same expression on both sides of '-' because 'buffer[index-1]' and 'var' represent the same value. [duplicateExpression]\n", errout_str()); } void duplicateExpression13() { //#7899 check("void f() {\n" " if (sizeof(long) == sizeof(long long)) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression14() { //#9871 check("int f() {\n" " int k = 7;\n" " int* f = &k;\n" " int* g = &k;\n" " return (f + 4 != g + 4);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:14] -> [test.cpp:4:14] -> [test.cpp:5:19]: (style) The comparison 'f+4 != g+4' is always false because 'f+4' and 'g+4' represent the same value. [knownConditionTrueFalse]\n", errout_str()); } void duplicateExpression15() { //#10650 check("bool f() {\n" " const int i = int(0);\n" " return i == 0;\n" "}\n" "bool g() {\n" " const int i = int{ 0 };\n" " return i == 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:22] -> [test.cpp:3:14]: (style) The comparison 'i == 0' is always true. [knownConditionTrueFalse]\n" "[test.cpp:6:22] -> [test.cpp:7:14]: (style) The comparison 'i == 0' is always true. [knownConditionTrueFalse]\n", errout_str()); } void duplicateExpression16() { check("void f(const std::string& a) {\n" //#10569 " if ((a == \"x\") ||\n" " (a == \"42\") ||\n" " (a == \"y\") ||\n" " (a == \"42\")) {}\n" "}\n" "void g(const std::string& a) {\n" " if ((a == \"42\") ||\n" " (a == \"x\") ||\n" " (a == \"42\") ||\n" " (a == \"y\")) {}\n" "}\n" "void h(const std::string& a) {\n" " if ((a == \"42\") ||\n" " (a == \"x\") ||\n" " (a == \"y\") ||\n" " (a == \"42\")) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:1:28] -> [test.cpp:4:20]: (style) Same expression 'a==\"42\"' found multiple times in chain of '||' operators. [duplicateExpression]\n" "[test.cpp:7:28] -> [test.cpp:9:20]: (style) Same expression 'a==\"42\"' found multiple times in chain of '||' operators. [duplicateExpression]\n" "[test.cpp:13:28] -> [test.cpp:16:20]: (style) Same expression 'a==\"42\"' found multiple times in chain of '||' operators. [duplicateExpression]\n", errout_str()); check("void f(const char* s) {\n" // #6371 " if (*s == '\x0F') {\n" " if (!s[1] || !s[2] || !s[1])\n" " break;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:28]: (style) Same expression '!s[1]' found multiple times in chain of '||' operators. [duplicateExpression]\n", errout_str()); } void duplicateExpression17() { check("enum { E0 };\n" // #12036 "void f() {\n" " if (0 > E0) {}\n" " if (E0 > 0) {}\n" " if (E0 == 0) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:11]: (style) The comparison '0 > E0' is always false. [knownConditionTrueFalse]\n" "[test.cpp:4:12]: (style) The comparison 'E0 > 0' is always false. [knownConditionTrueFalse]\n" "[test.cpp:5:12]: (style) The comparison 'E0 == 0' is always true. [knownConditionTrueFalse]\n", errout_str()); check("struct S {\n" // #12040, #12044 " static const int I = 0;\n" " enum { E0 };\n" " enum F { F0 };\n" " void f() {\n" " if (0 > I) {}\n" " if (0 > S::I) {}\n" " if (0 > E0) {}\n" " if (0 > S::E0) {}\n" " }\n" "};\n" "void g() {\n" " if (0 > S::I) {}\n" " if (0 > S::E0) {}\n" " if (0 > S::F::F0) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:26] -> [test.cpp:6:15]: (style) The comparison '0 > I' is always false. [knownConditionTrueFalse]\n" "[test.cpp:2:26] -> [test.cpp:7:15]: (style) The comparison '0 > S::I' is always false. [knownConditionTrueFalse]\n" "[test.cpp:8:15]: (style) The comparison '0 > E0' is always false. [knownConditionTrueFalse]\n" "[test.cpp:9:15]: (style) The comparison '0 > S::E0' is always false. [knownConditionTrueFalse]\n" "[test.cpp:2:26] -> [test.cpp:13:11]: (style) The comparison '0 > S::I' is always false. [knownConditionTrueFalse]\n" "[test.cpp:14:11]: (style) The comparison '0 > S::E0' is always false. [knownConditionTrueFalse]\n" "[test.cpp:15:11]: (style) The comparison '0 > S::F::F0' is always false. [knownConditionTrueFalse]\n", errout_str()); check("template\n" // #12122 "void f() {\n" " static_assert(std::is_same::value || std::is_integral::value);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression18() { checkP("#if defined(ABC)\n" // #13218 "#define MACRO1 (0x1)\n" "#else\n" "#define MACRO1 (0)\n" "#endif\n" "#if defined(XYZ)\n" "#define MACRO2 (0x2)\n" "#else\n" "#define MACRO2 (0)\n" "#endif\n" "#define MACRO_ALL (MACRO1 | MACRO2)\n" "void f() {\n" " if (MACRO_ALL == 0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression19() { checkP("const int i = 0;\n" "void f() {\n" " assert(i == 0);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression20() { check("enum { N = 1 };\n" // #14202 "int f() { return N - 1; }\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression21() { check("struct S { int i; };\n" // #12795 "struct T {\n" " std::map<:string s> m;\n" " S* get(const std::string& s) { return m[s]; }\n" " void modify() { for (const auto& e : m) e.second->i = 0; }\n" "};\n" "void f(T& t) {\n" " const S* p = t.get(\"abc\");\n" " const int o = p->i;\n" " t.modify();\n" " if (p->i == o) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int i; };\n" " struct T {\n" " std::vector m;\n" " void modify() { for (auto e : m) e->i = 0; }\n" "};\n" "void f(T& t) {\n" " const S* p = t.m[0];\n" " const int o = p->i;\n" " t.modify();\n" " if (p->i == o) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpression22() { check("int f() {\n" // #14913 " return 0x1 | (0x2 | 0x4) | 0x1;\n" "}\n" "int g() {\n" " return 0x1 | (0x2 | 0x1);\n" "}\n" "int h() {\n" " return 0x1 | (0x1 | 0x2);\n" "}\n" "int i() {\n" " return 0x2 | (0x4 | 0x1) | 0x1;\n" "}\n" "int j() {\n" " return 0x2 | (0x1 | 0x4) | 0x1;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:30]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n" "[test.cpp:5:23]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n" "[test.cpp:8:23]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n" "[test.cpp:11:23]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n" "[test.cpp:14:23]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n", errout_str()); check("bool f(const int** a, const int** b) {\n" " return (a[0] != nullptr) != (b[0] != nullptr);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpressionLoop() { check("void f() {\n" " int a = 1;\n" " while ( a != 1){}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:3:15]: (style) The comparison 'a != 1' is always false. [knownConditionTrueFalse]\n", errout_str()); check("void f() { int a = 1; while ( a != 1){ a++; }}\n"); ASSERT_EQUALS("", errout_str()); check("void f() { int a = 1; for ( int i=0; i < 3 && a != 1; i++){ a++; }}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int b) { int a = 1; while (b) { if ( a != 1){} b++; } a++; }\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " for(int i = 0; i < 10;) {\n" " if( i != 0 ) {}\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:17] -> [test.cpp:3:15]: (style) The comparison 'i != 0' is always false. [knownConditionTrueFalse]\n", errout_str()); check("void f() {\n" " for(int i = 0; i < 10;) {\n" " if( i != 0 ) {}\n" " i++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " for(int i = 0; i < 10;) {\n" " if( i != 0 ) { i++; }\n" " i++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " for(int i = 0; i < 10;) {\n" " if( i != 0 ) { i++; }\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " int i = 0;\n" " while(i < 10) {\n" " if( i != 0 ) {}\n" " i++;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(int b) {\n" " while (b) {\n" " int a = 1;\n" " if ( a != 1){}\n" " b++;\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:17] -> [test.cpp:4:16]: (style) The comparison 'a != 1' is always false. [knownConditionTrueFalse]\n", errout_str()); check("struct T {\n" // #11083 " std::string m;\n" " const std::string & str() const { return m; }\n" " T* next();\n" "};\n" "void f(T* t) {\n" " const std::string& s = t->str();\n" " while (t && t->str() == s)\n" " t = t->next();\n" " do {\n" " t = t->next();\n" " } while (t && t->str() == s);\n" " for (; t && t->str() == s; t = t->next());\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(std::string &out, const std::vector<:string> &list) {\n" // #13669 " for (int i = 0, size = list.size(); i < size; i++) {\n" " out += list[i];\n" " if (size > 0 && i < (size - 2))\n" " out += \",\";\n" " else if (i == (size - 1))\n" " out += \".\";\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(const std::vector& v) {\n" // #14193 " for (const int& r1 : v) {\n" " for (const int& r2 : v) {\n" " if (&r1 == &r2) {}\n" " }\n" " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateExpressionTernary() { // #6391 check("void f() {\n" " return A ? x : x;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:18]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); check("int f(bool b, int a) {\n" " const int c = a;\n" " return b ? a : c;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:19] -> [test.cpp:3:18]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); check("void f() {\n" " return A ? x : z;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(unsigned char c) {\n" " x = y ? (signed char)c : (unsigned char)c;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("std::string stringMerge(std::string const& x, std::string const& y) {\n" // #7938 " return ((x > y) ? (y + x) : (x + y));\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #6426 { const char code[] = "void foo(bool flag) {\n" " bar( (flag) ? ~0u : ~0ul);\n" "}\n"; /*const*/ Settings settings = settings1; settings.platform.sizeof_int = 4; settings.platform.int_bit = 32; settings.platform.sizeof_long = 4; settings.platform.long_bit = 32; check(code, dinit(CheckOptions, $.settings = &settings)); ASSERT_EQUALS("[test.cpp:2:21]: (style) Same value in both branches of ternary operator. [duplicateValueTernary]\n", errout_str()); settings.platform.sizeof_long = 8; settings.platform.long_bit = 64; check(code, dinit(CheckOptions, $.settings = &settings)); ASSERT_EQUALS("", errout_str()); } } void duplicateValueTernary() { check("void f() {\n" " if( a ? (b ? false:false): false ) ;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2:23]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); check("int f1(int a) {return (a == 1) ? (int)1 : 1; }\n"); ASSERT_EQUALS("[test.cpp:1:41]: (style) Same value in both branches of ternary operator. [duplicateValueTernary]\n", errout_str()); check("int f2(int a) {return (a == 1) ? (int)1 : (int)1; }\n"); ASSERT_EQUALS("[test.cpp:1:41]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); check("int f3(int a) {return (a == 1) ? 1 : (int)1; }\n"); ASSERT_EQUALS("[test.cpp:1:36]: (style) Same value in both branches of ternary operator. [duplicateValueTernary]\n", errout_str()); check("int f4(int a) {return (a == 1) ? 1 : 1; }\n"); ASSERT_EQUALS("[test.cpp:1:36]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); check("int f5(int a) {return (a == (int)1) ? (int)1 : 1; }\n"); ASSERT_EQUALS("[test.cpp:1:46]: (style) Same value in both branches of ternary operator. [duplicateValueTernary]\n", errout_str()); check("int f6(int a) {return (a == (int)1) ? (int)1 : (int)1; }\n"); ASSERT_EQUALS("[test.cpp:1:46]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); check("int f7(int a) {return (a == (int)1) ? 1 : (int)1; }\n"); ASSERT_EQUALS("[test.cpp:1:41]: (style) Same value in both branches of ternary operator. [duplicateValueTernary]\n", errout_str()); check("int f8(int a) {return (a == (int)1) ? 1 : 1; }\n"); ASSERT_EQUALS("[test.cpp:1:41]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); check("struct Foo {\n" " std::vector bar{1,2,3};\n" " std::vector baz{4,5,6};\n" "};\n" "void f() {\n" " Foo foo;\n" " it = true ? foo.bar.begin() : foo.baz.begin();\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(bool b) {\n" " std::vector bar{1,2,3};\n" " std::vector baz{4,5,6};\n" " std::vector v = b ? bar : baz;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(bool q) {\n" // #9570 " static int a = 0;\n" " static int b = 0;\n" " int& x = q ? a : b;\n" " ++x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct S { int a, b; };\n" // #10107 "S f(bool x, S s) {\n" " (x) ? f.a = 42 : f.b = 42;\n" " return f;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("float f(float x) {\n" // # 11368 " return (x >= 0.0) ? 0.0 : -0.0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("struct A {};\n" // # 14300 "struct B {};\n" "void f(bool x) {\n" " A* a = new A();\n" " B* b = new B();\n" " auto p = x ? static_cast(a) : static_cast(b);\n" " (void)p;\n" " delete a;\n" " delete b;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void duplicateValueTernarySizeof() { // #13773 check("int f() { return x ? sizeof(uint32_t) : sizeof(unsigned int); }\n"); ASSERT_EQUALS("", errout_str()); check("int f() { return x ? sizeof(uint32_t) : sizeof(uint32_t); }\n"); ASSERT_EQUALS("[test.cpp:1:39]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); } void duplicateExpressionTemplate() { check("template void f() {\n" // #6930 " if (I >= 0 && I < 3) {}\n" "}\n" "\n" "static auto a = f<0>();\n"); ASSERT_EQUALS("", errout_str()); check("template\n" // #7754 "void f() {\n" " if (std::is_same_v || std::is_same_v) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("typedef long long int64_t;" "template\n" "void f() {\n" " if (std::is_same_v || std::is_same_v) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); checkP("#define int32_t int" "template\n" "void f() {\n" " if (std::is_same_v || std::is_same_v) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); checkP("#define F(v) (v) != 0\n" // #12392 "template