Skip to content

Commit 3ccd050

Browse files
Enable and mitigate readability-simplify-boolean-expr (danmar#4897)
1 parent 173c843 commit 3ccd050

19 files changed

Lines changed: 36 additions & 79 deletions

.clang-tidy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
---
2-
Checks: '*,-abseil-*,-altera-*,-android-*,-boost-*,-cert-*,-cppcoreguidelines-*,-darwin-*,-fuchsia-*,-google-*,-hicpp-*,-linuxkernel-*,-llvm-*,-llvmlibc-*,-mpi-*,-objc-*,-openmp-*,-zircon-*,google-explicit-constructor,-readability-braces-around-statements,-readability-magic-numbers,-bugprone-macro-parentheses,-readability-isolate-declaration,-readability-function-size,-modernize-use-trailing-return-type,-readability-implicit-bool-conversion,-readability-uppercase-literal-suffix,-modernize-use-auto,-readability-else-after-return,-modernize-use-default-member-init,-readability-redundant-member-init,-modernize-avoid-c-arrays,-modernize-use-equals-default,-readability-container-size-empty,-readability-simplify-boolean-expr,-bugprone-branch-clone,-bugprone-narrowing-conversions,-modernize-raw-string-literal,-readability-convert-member-functions-to-static,-modernize-loop-convert,-readability-const-return-type,-modernize-return-braced-init-list,-performance-inefficient-string-concatenation,-misc-throw-by-value-catch-by-reference,-readability-avoid-const-params-in-decls,-misc-non-private-member-variables-in-classes,-clang-analyzer-*,-bugprone-signed-char-misuse,-misc-no-recursion,-readability-use-anyofallof,-performance-no-automatic-move,-readability-function-cognitive-complexity,-readability-redundant-access-specifiers,-concurrency-mt-unsafe,-bugprone-easily-swappable-parameters,-readability-suspicious-call-argument,-readability-identifier-length,-readability-container-data-pointer,-bugprone-assignment-in-if-condition,-misc-const-correctness,-portability-std-allocator-const,-modernize-deprecated-ios-base-aliases,-bugprone-unchecked-optional-access,-modernize-replace-auto-ptr,-readability-identifier-naming,-portability-simd-intrinsics,-misc-use-anonymous-namespace'
2+
Checks: '*,-abseil-*,-altera-*,-android-*,-boost-*,-cert-*,-cppcoreguidelines-*,-darwin-*,-fuchsia-*,-google-*,-hicpp-*,-linuxkernel-*,-llvm-*,-llvmlibc-*,-mpi-*,-objc-*,-openmp-*,-zircon-*,google-explicit-constructor,-readability-braces-around-statements,-readability-magic-numbers,-bugprone-macro-parentheses,-readability-isolate-declaration,-readability-function-size,-modernize-use-trailing-return-type,-readability-implicit-bool-conversion,-readability-uppercase-literal-suffix,-modernize-use-auto,-readability-else-after-return,-modernize-use-default-member-init,-readability-redundant-member-init,-modernize-avoid-c-arrays,-modernize-use-equals-default,-readability-container-size-empty,-bugprone-branch-clone,-bugprone-narrowing-conversions,-modernize-raw-string-literal,-readability-convert-member-functions-to-static,-modernize-loop-convert,-readability-const-return-type,-modernize-return-braced-init-list,-performance-inefficient-string-concatenation,-misc-throw-by-value-catch-by-reference,-readability-avoid-const-params-in-decls,-misc-non-private-member-variables-in-classes,-clang-analyzer-*,-bugprone-signed-char-misuse,-misc-no-recursion,-readability-use-anyofallof,-performance-no-automatic-move,-readability-function-cognitive-complexity,-readability-redundant-access-specifiers,-concurrency-mt-unsafe,-bugprone-easily-swappable-parameters,-readability-suspicious-call-argument,-readability-identifier-length,-readability-container-data-pointer,-bugprone-assignment-in-if-condition,-misc-const-correctness,-portability-std-allocator-const,-modernize-deprecated-ios-base-aliases,-bugprone-unchecked-optional-access,-modernize-replace-auto-ptr,-readability-identifier-naming,-portability-simd-intrinsics,-misc-use-anonymous-namespace'
33
WarningsAsErrors: '*'
44
HeaderFilterRegex: '(cli|gui|lib|oss-fuzz|test|triage)\/[a-z]+\.h'
55
CheckOptions:
66
- key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
7-
value: '1'
7+
value: '1'
8+
- key: readability-simplify-boolean-expr.SimplifyDeMorgan
9+
value: '0'

clang-tidy.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ We are not interesting in the size/complexity of a function.
4949

5050
`readability-magic-numbers`<br>
5151
`readability-redundant-member-init`<br>
52-
`readability-simplify-boolean-expr`<br>
5352

5453
These do not (always) increase readability.
5554

gui/cppchecklibrarydata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ static CppcheckLibraryData::Markup loadMarkup(QXmlStreamReader &xmlReader)
365365
mandatoryAttibuteMissing(xmlReader, "ext");
366366
}
367367
if (xmlReader.attributes().hasAttribute("aftercode")) {
368-
markup.afterCode = (xmlReader.attributes().value("aftercode") == QString("true")) ? true : false;
368+
markup.afterCode = (xmlReader.attributes().value("aftercode") == QString("true"));
369369
} else {
370370
mandatoryAttibuteMissing(xmlReader, "aftercode");
371371
}
372372
if (xmlReader.attributes().hasAttribute("reporterrors")) {
373-
markup.reportErrors = (xmlReader.attributes().value("reporterrors") == QString("true")) ? true : false;
373+
markup.reportErrors = (xmlReader.attributes().value("reporterrors") == QString("true"));
374374
} else {
375375
mandatoryAttibuteMissing(xmlReader, "reporterrors");
376376
}

gui/settingsdialog.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ Qt::CheckState SettingsDialog::boolToCheckState(bool yes)
147147

148148
bool SettingsDialog::checkStateToBool(Qt::CheckState state)
149149
{
150-
if (state == Qt::Checked) {
151-
return true;
152-
}
153-
return false;
150+
return state == Qt::Checked;
154151
}
155152

156153

lib/astutils.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,7 @@ bool isVariableDecl(const Token* tok)
392392
if (var->nameToken() == tok)
393393
return true;
394394
const Token * const varDeclEndToken = var->declEndToken();
395-
if (Token::Match(varDeclEndToken, "; %var%") && varDeclEndToken->next() == tok)
396-
return true;
397-
return false;
395+
return Token::Match(varDeclEndToken, "; %var%") && varDeclEndToken->next() == tok;
398396
}
399397

400398
bool isStlStringType(const Token* tok)
@@ -2002,11 +2000,7 @@ bool isUniqueExpression(const Token* tok)
20022000
return true;
20032001

20042002
const std::string freturnType = f.retType ? f.retType->name() : f.retDef->stringifyList(f.returnDefEnd());
2005-
if (f.argumentList.size() == fun->argumentList.size() && returnType == freturnType &&
2006-
f.name() != fun->name()) {
2007-
return false;
2008-
}
2009-
return true;
2003+
return f.argumentList.size() != fun->argumentList.size() || returnType != freturnType || f.name() == fun->name();
20102004
}))
20112005
return false;
20122006
} else if (tok->variable()) {

lib/checkbufferoverrun.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,12 +1111,10 @@ static bool isVLAIndex(const Token* tok)
11111111
return true;
11121112
if (tok->str() == "?") {
11131113
// this is a VLA index if both expressions around the ":" is VLA index
1114-
if (tok->astOperand2() &&
1115-
tok->astOperand2()->str() == ":" &&
1116-
isVLAIndex(tok->astOperand2()->astOperand1()) &&
1117-
isVLAIndex(tok->astOperand2()->astOperand2()))
1118-
return true;
1119-
return false;
1114+
return tok->astOperand2() &&
1115+
tok->astOperand2()->str() == ":" &&
1116+
isVLAIndex(tok->astOperand2()->astOperand1()) &&
1117+
isVLAIndex(tok->astOperand2()->astOperand2());
11201118
}
11211119
return isVLAIndex(tok->astOperand1()) || isVLAIndex(tok->astOperand2());
11221120
}

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2894,7 +2894,7 @@ void CheckClass::checkCopyCtorAndEqOperator()
28942894
// This is disabled because of #8388
28952895
// The message must be clarified. How is the behaviour different?
28962896
// cppcheck-suppress unreachableCode - remove when code is enabled again
2897-
if ((true) || !mSettings->severity.isEnabled(Severity::warning))
2897+
if ((true) || !mSettings->severity.isEnabled(Severity::warning)) // NOLINT(readability-simplify-boolean-expr)
28982898
return;
28992899

29002900
for (const Scope * scope : mSymbolDatabase->classAndStructScopes) {

lib/checkcondition.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,15 @@ void CheckCondition::comparison()
364364
if ((expr1->str() == "&" && (num1 & num2) != num2) ||
365365
(expr1->str() == "|" && (num1 | num2) != num2)) {
366366
const std::string& op(tok->str());
367-
comparisonError(expr1, expr1->str(), num1, op, num2, op=="==" ? false : true);
367+
comparisonError(expr1, expr1->str(), num1, op, num2, op != "==");
368368
}
369369
} else if (expr1->str() == "&") {
370370
const bool or_equal = Token::Match(tok, ">=|<=");
371371
const std::string& op(tok->str());
372372
if ((Token::Match(tok, ">=|<")) && (num1 < num2)) {
373-
comparisonError(expr1, expr1->str(), num1, op, num2, or_equal ? false : true);
373+
comparisonError(expr1, expr1->str(), num1, op, num2, !or_equal);
374374
} else if ((Token::Match(tok, "<=|>")) && (num1 <= num2)) {
375-
comparisonError(expr1, expr1->str(), num1, op, num2, or_equal ? true : false);
375+
comparisonError(expr1, expr1->str(), num1, op, num2, or_equal);
376376
}
377377
} else if (expr1->str() == "|") {
378378
if ((expr1->astOperand1()->valueType()) &&
@@ -382,11 +382,11 @@ void CheckCondition::comparison()
382382
if ((Token::Match(tok, ">=|<")) && (num1 >= num2)) {
383383
//"(a | 0x07) >= 7U" is always true for unsigned a
384384
//"(a | 0x07) < 7U" is always false for unsigned a
385-
comparisonError(expr1, expr1->str(), num1, op, num2, or_equal ? true : false);
385+
comparisonError(expr1, expr1->str(), num1, op, num2, or_equal);
386386
} else if ((Token::Match(tok, "<=|>")) && (num1 > num2)) {
387387
//"(a | 0x08) <= 7U" is always false for unsigned a
388388
//"(a | 0x07) > 6U" is always true for unsigned a
389-
comparisonError(expr1, expr1->str(), num1, op, num2, or_equal ? false : true);
389+
comparisonError(expr1, expr1->str(), num1, op, num2, !or_equal);
390390
}
391391
}
392392
}
@@ -1048,10 +1048,7 @@ static bool parseComparison(const Token *comp, bool &not1, std::string &op, std:
10481048
inconclusive = inconclusive || ((value)[0] == '\'' && !(op == "!=" || op == "=="));
10491049

10501050
// Only float and int values are currently handled
1051-
if (!MathLib::isInt(value) && !MathLib::isFloat(value) && (value)[0] != '\'')
1052-
return false;
1053-
1054-
return true;
1051+
return MathLib::isInt(value) || MathLib::isFloat(value) || (value[0] == '\'');
10551052
}
10561053

10571054
static std::string conditionString(bool not1, const Token *expr1, const std::string &op, const std::string &value1)

lib/checkother.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,7 @@ void CheckOther::redundantAssignmentInSwitchError(const Token *tok1, const Token
554554
//---------------------------------------------------------------------------
555555
static inline bool isFunctionOrBreakPattern(const Token *tok)
556556
{
557-
if (Token::Match(tok, "%name% (") || Token::Match(tok, "break|continue|return|exit|goto|throw"))
558-
return true;
559-
560-
return false;
557+
return Token::Match(tok, "%name% (") || Token::Match(tok, "break|continue|return|exit|goto|throw");
561558
}
562559

563560
void CheckOther::checkRedundantAssignmentInSwitch()
@@ -1097,7 +1094,7 @@ void CheckOther::variableScopeError(const Token *tok, const std::string &varname
10971094
void CheckOther::checkCommaSeparatedReturn()
10981095
{
10991096
// This is experimental for now. See #5076
1100-
if ((true) || !mSettings->severity.isEnabled(Severity::style))
1097+
if ((true) || !mSettings->severity.isEnabled(Severity::style)) // NOLINT(readability-simplify-boolean-expr)
11011098
return;
11021099

11031100
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {

lib/checkstl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,7 @@ static bool isSameIteratorContainerExpression(const Token* tok1,
702702
ValueFlow::Value::LifetimeKind kind = ValueFlow::Value::LifetimeKind::Iterator)
703703
{
704704
if (isSameExpression(true, false, tok1, tok2, library, false, false)) {
705-
if (astIsContainerOwned(tok1) && isTemporary(true, tok1, &library))
706-
return false;
707-
return true;
705+
return !astIsContainerOwned(tok1) || !isTemporary(true, tok1, &library);
708706
}
709707
if (kind == ValueFlow::Value::LifetimeKind::Address) {
710708
return isSameExpression(true, false, getAddressContainer(tok1), getAddressContainer(tok2), library, false, false);

0 commit comments

Comments
 (0)