From 51870dff340ef7c5534a9357ebdc6bcf8fb1ffac Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Fri, 27 Sep 2019 20:53:23 -0700 Subject: [PATCH] [clang-tidy] Simplify boolean expressions Found with readability-simplify-boolean-expr Signed-off-by: Rosen Penev --- src/CLI2.cpp | 11 +++-------- src/Filter.cpp | 14 ++++---------- src/Lexer.cpp | 14 ++++---------- src/TDB2.cpp | 2 +- src/Task.cpp | 14 ++++---------- src/Variant.cpp | 12 ++++++------ src/ViewTask.cpp | 2 +- src/recur.cpp | 7 ++----- 8 files changed, 25 insertions(+), 51 deletions(-) diff --git a/src/CLI2.cpp b/src/CLI2.cpp index 263cd3c64..fddc40585 100644 --- a/src/CLI2.cpp +++ b/src/CLI2.cpp @@ -1492,11 +1492,9 @@ void CLI2::findIDs () } std::string raw = a.attribute ("raw"); - previousFilterArgWasAnOperator = (a._lextype == Lexer::Type::op && + previousFilterArgWasAnOperator = a._lextype == Lexer::Type::op && raw != "(" && - raw != ")") - ? true - : false; + raw != ")"; } } @@ -1722,10 +1720,7 @@ void CLI2::insertIDExpr () bool ascending = true; int low = strtol (r->first.c_str (), nullptr, 10); int high = strtol (r->second.c_str (), nullptr, 10); - if (low <= high) - ascending = true; - else - ascending = false; + ascending = low <= high; reconstructed.push_back (openParen); reconstructed.push_back (argID); diff --git a/src/Filter.cpp b/src/Filter.cpp index 7a890d037..15403c01c 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -73,7 +73,7 @@ void Filter::subset (const std::vector & input, std::vector & output // Debug output from Eval during compilation is useful. During evaluation // it is mostly noise. - eval.debug (Context::getContext ().config.getInteger ("debug.parser") >= 3 ? true : false); + eval.debug (Context::getContext ().config.getInteger ("debug.parser") >= 3); eval.compileExpression (precompiled); for (auto& task : input) @@ -124,7 +124,7 @@ void Filter::subset (std::vector & output) // Debug output from Eval during compilation is useful. During evaluation // it is mostly noise. - eval.debug (Context::getContext ().config.getInteger ("debug.parser") >= 3 ? true : false); + eval.debug (Context::getContext ().config.getInteger ("debug.parser") >= 3); eval.compileExpression (precompiled); output.clear (); @@ -240,16 +240,10 @@ bool Filter::pendingOnly () const if (countStatus) { - if (!countPending && !countWaiting && !countRecurring) - return false; - - return true; + return !(!countPending && !countWaiting && !countRecurring); } - if (countId) - return true; - - return false; + return countId != 0; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Lexer.cpp b/src/Lexer.cpp index c9d70476a..a47794477 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -75,7 +75,7 @@ bool Lexer::token (std::string& token, Lexer::Type& type) // - path < substitution < pattern // - set < number // - word last - if (isString (token, type, "'\"") || + return isString (token, type, "'\"") || isDate (token, type) || isDuration (token, type) || isURL (token, type) || @@ -92,10 +92,7 @@ bool Lexer::token (std::string& token, Lexer::Type& type) isPattern (token, type) || isOperator (token, type) || isIdentifier (token, type) || - isWord (token, type)) - return true; - - return false; + isWord (token, type); } //////////////////////////////////////////////////////////////////////////////// @@ -266,10 +263,7 @@ void Lexer::dequote (std::string& input, const std::string& quotes) // escapes, to get them past the shell. bool Lexer::wasQuoted (const std::string& input) { - if (input.find_first_of (" \t()<>&~") != std::string::npos) - return true; - - return false; + return input.find_first_of (" \t()<>&~") != std::string::npos; } //////////////////////////////////////////////////////////////////////////////// @@ -1571,7 +1565,7 @@ bool Lexer::readWord ( prev = c; } - return word.length () > 0 ? true : false; + return word.length () > 0; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 772e3db4a..847681652 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -1438,7 +1438,7 @@ int TDB2::id (const std::string& uuid) bool TDB2::verifyUniqueUUID (const std::string& uuid) { pending.get_tasks (); - return pending.id (uuid) != 0 ? false : true; + return pending.id (uuid) == 0; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Task.cpp b/src/Task.cpp index e60e1ee2d..eb8a66260 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -190,10 +190,7 @@ void Task::setAsNow (const std::string& att) //////////////////////////////////////////////////////////////////////////////// bool Task::has (const std::string& name) const { - if (data.find (name) != data.end ()) - return true; - - return false; + return data.find (name) != data.end (); } //////////////////////////////////////////////////////////////////////////////// @@ -1049,7 +1046,7 @@ int Task::getAnnotationCount () const //////////////////////////////////////////////////////////////////////////////// bool Task::hasAnnotations () const { - return annotation_count ? true : false; + return annotation_count != 0; } //////////////////////////////////////////////////////////////////////////////// @@ -1296,10 +1293,7 @@ bool Task::hasTag (const std::string& tag) const // Concrete tags. auto tags = split (get ("tags"), ','); - if (std::find (tags.begin (), tags.end (), tag) != tags.end ()) - return true; - - return false; + return std::find (tags.begin (), tags.end (), tag) != tags.end (); } //////////////////////////////////////////////////////////////////////////////// @@ -1368,7 +1362,7 @@ void Task::substitute ( const std::string& to, const std::string& flags) { - bool global = (flags.find ('g') != std::string::npos ? true : false); + bool global = (flags.find ('g') != std::string::npos); // Get the data to modify. std::string description = get ("description"); diff --git a/src/Variant.cpp b/src/Variant.cpp index abbdb81c0..96c818142 100644 --- a/src/Variant.cpp +++ b/src/Variant.cpp @@ -1820,7 +1820,7 @@ void Variant::cast (const enum type new_type) case type_integer: switch (new_type) { - case type_boolean: _bool = _integer == 0 ? false : true; break; + case type_boolean: _bool = _integer != 0; break; case type_integer: break; case type_real: _real = static_cast(_integer); break; case type_string: @@ -1838,7 +1838,7 @@ void Variant::cast (const enum type new_type) case type_real: switch (new_type) { - case type_boolean: _bool = _real == 0.0 ? false : true; break; + case type_boolean: _bool = _real != 0.0; break; case type_integer: _integer = static_cast(_real); break; case type_real: break; case type_string: @@ -1858,9 +1858,9 @@ void Variant::cast (const enum type new_type) switch (new_type) { case type_boolean: - _bool = (_string.length () == 0 || + _bool = !(_string.length () == 0 || _string == "0" || - _string == "0.0") ? false : true; + _string == "0.0"); break; case type_integer: _integer = static_cast(strtol (_string.c_str (), nullptr, (_string.substr (0, 2) == "0x" ? 16 : 10))); @@ -1913,7 +1913,7 @@ void Variant::cast (const enum type new_type) case type_date: switch (new_type) { - case type_boolean: _bool = _date != 0 ? true : false; break; + case type_boolean: _bool = _date != 0; break; case type_integer: _integer = static_cast(_date); break; case type_real: _real = static_cast(_date); break; case type_string: _string = std::string(*this); break; @@ -1925,7 +1925,7 @@ void Variant::cast (const enum type new_type) case type_duration: switch (new_type) { - case type_boolean: _bool = _duration != 0 ? true : false; break; + case type_boolean: _bool = _duration != 0; break; case type_integer: _integer = static_cast(_duration); break; case type_real: _real = static_cast(_duration); break; case type_string: _string = std::string(*this); break; diff --git a/src/ViewTask.cpp b/src/ViewTask.cpp index 28244d7c9..ccfe7eaed 100644 --- a/src/ViewTask.cpp +++ b/src/ViewTask.cpp @@ -302,7 +302,7 @@ std::string ViewTask::render (std::vector & data, std::vector & seque autoColorize (data[sequence[s]], rule_color); // Alternate rows based on |s % 2| - bool odd = (s % 2) ? true : false; + bool odd = (s % 2) != 0; Color row_color; if (Context::getContext ().color ()) { diff --git a/src/recur.cpp b/src/recur.cpp index 70703e4dc..6c32d946f 100644 --- a/src/recur.cpp +++ b/src/recur.cpp @@ -174,11 +174,8 @@ bool generateDueDates (Task& parent, std::vector & allDue) // parent mask contains all + or X, then there never will be another task // to generate, and this parent task may be safely reaped. auto mask = parent.get ("mask"); - if (mask.length () == allDue.size () && - mask.find ('-') == std::string::npos) - return false; - - return true; + return !(mask.length () == allDue.size () && + mask.find ('-') == std::string::npos); } if (i > now)