aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-09-14[clang-tidy][NFC] Switch to new file header style (#158497)Victor Chernyakin1-1/+1
As decided in #118553 and following up #153942. `rename_check.py` has small logic changes, everything else is a mechanical replacement.
2024-06-28[clang-tidy] improve messages when auto-fix does not work (#96917)Congcong Cai1-7/+35
Fixes: #93157
2024-05-11[clang-tidy] `readability-simplify-boolean-expr` avoid to warn expression ↵Congcong Cai1-25/+33
expand from macro when ``IgnoreMacro`` option is enabled. (#91757) Fixes: #91487
2024-01-14[clang-tidy] Add option to ignore macros in ↵Danny Mösch1-1/+9
`readability-simplify-boolean-expr` check (#78043)
2023-08-27[clang-tidy][NFC] Fix cppcoreguidelines-init-variables findingsPiotr Zegar1-2/+2
Fix issues found by clang-tidy in clang-tidy source directory.
2023-08-27[clang-tidy][NFC] Fix readability-static-accessed-through-instance findingsPiotr Zegar1-2/+2
Fix issues found by clang-tidy in clang-tidy source directory.
2023-01-14[clang-tidy][NFC] Use C++17 nested namespaces in the clang-tidy folderCarlos Galvez1-6/+2
Fix applied by running: run-clang-tidy.py -checks=-*,modernize-concat-nested-namespaces Differential Revision: https://reviews.llvm.org/D141770
2023-01-07[clang-tools-extra] Use std::optional instead of llvm::Optional (NFC)Kazu Hirata1-14/+14
This patch replaces (llvm::|)Optional< with std::optional<. I'll post a separate patch to clean up the "using" declarations, #include "llvm/ADT/Optional.h", etc. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07[clang-tools-extra] Add #include <optional> (NFC)Kazu Hirata1-0/+1
This patch adds #include <optional> to those files containing llvm::Optional<...> or Optional<...>. I'll post a separate patch to actually replace llvm::Optional with std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03[clang-tidy] Use std::nullopt instead of None (NFC)Kazu Hirata1-3/+3
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02Use CTAD on llvm::SaveAndRestoreJan Svoboda1-1/+1
Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D139229
2022-10-05[clang-tidy] Fix crashes on `if consteval` in readability checksEmilia Dreamer1-4/+6
The `readability-braces-around-statements` check tries to look at the closing parens of the if condition to determine where to insert braces, however, "consteval if" statements don't have a condition, and always have braces regardless, so the skip can be checked. The `readability-simplify-boolean-expr` check looks at the condition of the if statement to determine what could be simplified, but as "consteval if" statements do not have a condition that could be simplified, they can also be skipped here. There may still be more checks that try to look at the conditions of `if`s that aren't included here Fixes https://github.com/llvm/llvm-project/issues/57568 Reviewed By: njames93, aaron.ballman Differential Revision: https://reviews.llvm.org/D133413
2022-09-24[clang-tidy] Fix a false positive in readability-simplify-boolean-exprNathan James1-2/+2
Reviewed By: LegalizeAdulthood Differential Revision: https://reviews.llvm.org/D134590
2022-05-25[clang-tidy] Extend SimplifyBooleanExpr demorgan support.Nathan James1-2/+10
Adds an option SimplifyDemorganRelaxed which, when enabled, will transform negated conjunctions or disjunctions when neither operand is a negation. Default value is `false`. Reviewed By: LegalizeAdulthood Differential Revision: https://reviews.llvm.org/D126162
2022-05-22[clang-tidy] Fix not updating storeOptions after af77b1d9901Nathan James1-0/+1
2022-05-22[clang-tidy] add support for Demorgan conversions to ↵Nathan James1-1/+261
readability-simplify-bool-expr Adds support for recognising and converting boolean expressions that can be simplified using De Morgans Law. This is a different implementation to D124650. Fixes https://github.com/llvm/llvm-project/issues/55092 Reviewed By: LegalizeAdulthood Differential Revision: https://reviews.llvm.org/D124806
2022-05-18[clang-tidy] Fix readability-simplify-boolean-expr when Ifs have an init ↵Nathan James1-9/+16
statement or condition variable Fixes https://github.com/llvm/llvm-project/issues/55553. Reviewed By: LegalizeAdulthood Differential Revision: https://reviews.llvm.org/D125874
2022-05-18[clang-tidy] Fix readability-simplify-boolean-expr crash with implicit cast ↵Nathan James1-41/+6
in return. Fixes https://github.com/llvm/llvm-project/issues/55557 Reviewed By: LegalizeAdulthood Differential Revision: https://reviews.llvm.org/D125877
2022-05-16[clang-tidy][NFC] Reimplement SimplifyBooleanExpr with RecursiveASTVisitorsNathan James1-387/+313
Reimplement the matching logic using Visitors instead of matchers. Benchmarks from running the check over SemaCodeComplete.cpp Before 0.20s, After 0.04s Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D125026
2022-02-12[clang-tidy] SimplifyBooleanExprCheck - use cast<> instead of dyn_cast<> to ↵Simon Pilgrim1-3/+3
avoid dereference of nullptr The IfStmt pointer is always referenced inside the replaceCompoundReturnWithCondition call, so assert the cast is correct instead of returning nullptr
2022-01-28[clang-tidy] Recognize labelled statements when simplifying boolean exprsRichard1-131/+238
Inside a switch the caseStmt() and defaultStmt() have a nested statement associated with them. Similarly, labelStmt() has a nested statement. These statements were being missed when looking for a compound-if of the form "if (x) return true; return false;" when the if is nested under one of these labelling constructs. Enhance the matchers to look for these nested statements using some private matcher hasSubstatement() traversal matcher on case, default and label statements. Add the private matcher hasSubstatementSequence() to match the compound "if (x) return true; return false;" pattern. - Add unit tests for private matchers and corresponding test infrastructure - Add corresponding test file readability-simplify-bool-expr-case.cpp. - Fix variable name copy/paste error in readability-simplify-bool-expr.cpp. - Drop the asserts, which were used only for debugging matchers. - Run clang-format on the whole check. - Move local functions out of anonymous namespace and declare state, per LLVM style guide - Declare labels constexpr - Declare visitor arguments as pointer to const - Drop braces around simple control statements per LLVM style guide - Prefer explicit arguments over default arguments to methods Differential Revision: https://reviews.llvm.org/D56303 Fixes #27078
2021-02-27[clang-tidy] Simplify boolean expr checkStephen Kelly1-24/+14
Differential Revision: https://reviews.llvm.org/D97153
2021-01-29[clang-tidy] Applied clang-tidy fixes. NFCAlexander Kornienko1-11/+11
Applied fixes enabled by the LLVM's .clang-tidy configs. Reverted files where fixes introduced compile errors: clang-tools-extra/clang-tidy/hicpp/NoAssemblerCheck.cpp clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp $ clang-tools-extra/clang-tidy/tool/run-clang-tidy.py -fix clang-tools-extra/clang-tidy/ Enabled checks: llvm-else-after-return llvm-header-guard llvm-include-order llvm-namespace-comment llvm-prefer-isa-or-dyn-cast-in-conditionals llvm-prefer-register-over-unsigned llvm-qualified-auto llvm-twine-local misc-definitions-in-headers misc-misplaced-const misc-new-delete-overloads misc-no-recursion misc-non-copyable-objects misc-redundant-expression misc-static-assert misc-throw-by-value-catch-by-reference misc-unconventional-assign-operator misc-uniqueptr-reset-release misc-unused-alias-decls misc-unused-using-decls readability-identifier-naming Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D95614
2020-09-07[Ignore Expressions][NFC] Refactor to better use `IgnoreExpr.h` and nitsEduardo Caldas1-1/+1
This change groups * Rename: `ignoreParenBaseCasts` -> `IgnoreParenBaseCasts` for uniformity * Rename: `IgnoreConversionOperator` -> `IgnoreConversionOperatorSingleStep` for uniformity * Inline `IgnoreNoopCastsSingleStep` into a lambda inside `IgnoreNoopCasts` * Refactor `IgnoreUnlessSpelledInSource` to make adequate use of `IgnoreExprNodes` Differential Revision: https://reviews.llvm.org/D86880
2020-08-22[clang-tidy] readability-simplify-boolean-expr detects negated literalsNathan James1-22/+41
Adds support for detecting cases like `if (!true) ...`. Addresses [[ https://bugs.llvm.org/show_bug.cgi?id=47166 | readability-simplify-boolean-expr not detected for negated boolean literals. ]] Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D86176
2020-06-16[clang-tidy] simplify-bool-expr ignores template instantiationsNathan James1-9/+14
Ignore template instantiations in the matchers, Addresses [[ https://bugs.llvm.org/show_bug.cgi?id=46226 | readability-simplify-boolean-expr false-positive for bool from template. ]] Reviewed By: aaron.ballman, lebedev.ri Differential Revision: https://reviews.llvm.org/D81336
2020-04-12[clang-tidy] Convert config options that are bools to use the bool overload ↵Nathan James1-3/+2
of get(GlobalOrLocal)? Summary: This was done with a script that looks for calls to Options.get(GlobalOrLocal) that take an integer for the second argument and the result is either compared not equal to 0 or implicitly converted to bool. There may be other occurances Reviewers: aaron.ballman, alexfh, gribozavr2 Reviewed By: aaron.ballman Subscribers: wuzish, nemanjai, xazax.hun, kbarton, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D77831
2020-01-28Make llvm::StringRef to std::string conversions explicit.Benjamin Kramer1-1/+1
This is how it should've been and brings it more in line with std::string_view. There should be no functional change here. This is mostly mechanical from a custom clang-tidy check, with a lot of manual fixups. It uncovers a lot of minor inefficiencies. This doesn't actually modify StringRef yet, I'll do that in a follow-up.
2019-05-16[clang-tidy] Handle member variables in readability-simplify-boolean-exprJonas Toth1-18/+17
- Add readability-simplify-boolean-expr test cases for member variables Fixes PR40179 Patch by LegalizeAdulthood. Reviewers: alexfh, hokein, aaron.ballman, JonasToth Reviewed By: JonasToth Subscribers: jdoerfert, xazax.hun, cfe-commits Differential Revision: https://reviews.llvm.org/D56323 llvm-svn: 360882
2019-03-01Fix file headers. NFCFangrui Song1-1/+1
llvm-svn: 355188
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-12-21Fix warning about unused variable [NFC]Bjorn Pettersson1-1/+1
llvm-svn: 349891
2018-12-20[clang-tidy] Use translationUnitDecl() instead of a custom matcher.Alexander Kornienko1-17/+5
llvm-svn: 349758
2018-11-25A bit of AST matcher cleanup, NFC.Alexander Kornienko1-5/+5
Removed the uses of the allOf() matcher inside node matchers that are implicit allOf(). Replaced uses of allOf() with the explicit node matcher where it makes matchers more readable. Replace anyOf(hasName(), hasName(), ...) with the more efficient and readable hasAnyName(). llvm-svn: 347520
2018-11-15[clang-tidy] Update checks to play nicely with limited traversal scope added ↵Sam McCall1-3/+13
in r346847 Summary: (See D54204 for original review) Reviewers: hokein Subscribers: xazax.hun, cfe-commits Differential Revision: https://reviews.llvm.org/D54579 llvm-svn: 346961
2018-08-09Port getLocEnd -> getEndLocStephen Kelly1-2/+2
Subscribers: nemanjai, ioeric, kbarton, cfe-commits Differential Revision: https://reviews.llvm.org/D50355 llvm-svn: 339401
2018-08-09Port getLocStart -> getBeginLocStephen Kelly1-11/+11
Reviewers: javed.absar Subscribers: nemanjai, kbarton, ilya-biryukov, ioeric, jkorous, arphaman, jfb, cfe-commits Differential Revision: https://reviews.llvm.org/D50354 llvm-svn: 339400
2018-05-22[clang-tidy] SimplifyBoolenExpr doesn't add parens if unary negotiation is ↵Zinovy Nis1-0/+3
of ExprWithCleanups type bool foo(A &S) { if (S != (A)S) return false; return true; } is fixed into (w/o this patch) ... return !S != (A)S; // negotiation affects first operand only } instead of (with this patch) ... return S == (A)S; // note == instead of != } Differential Revision: https://reviews.llvm.org/D47122 llvm-svn: 333003
2017-11-29[clang-tidy] make readability-simplify-bool-expr completely ignore macrosAlexander Kornienko1-4/+1
llvm-svn: 319325
2017-05-15[clang-tidy] Partly rewrite readability-simplify-boolean-expr using RAVAlexander Kornienko1-105/+98
The check was using AST matchers in a very inefficient manner. By rewriting the BinaryOperator-related parts using RAV, the check was sped up by a factor of up to 10000 on some files (mostly, generated code using binary operators in tables), but also significantly sped up for regular large files. As a side effect, the code became clearer and more readable. llvm-svn: 303081
2016-09-24[clang-tidy] Cleaning up language options.Gabor Horvath1-3/+3
Differential Revision: https://reviews.llvm.org/D24881 llvm-svn: 282319
2016-05-31[ASTMatchers] Added ignoringParenImpCasts to has matchersPiotr Padlewski1-1/+2
has matcher changed behaviour, and now it matches "as is" and doesn't skip implicit and paren casts http://reviews.llvm.org/D20801 llvm-svn: 271289
2016-02-12Reapply r260096.Aaron Ballman1-93/+138
Expand the simplify boolean expression check to handle implicit conversion of integral types to bool and improve the handling of implicit conversion of member pointers to bool. Implicit conversion of member pointers are replaced with explicit comparisons to nullptr. Implicit conversions of integral types are replaced with explicit comparisons to 0. Patch by Richard Thomson. llvm-svn: 260681
2016-02-08Reverting r260096; it causes build bot failures:Aaron Ballman1-136/+93
http://bb.pgr.jp/builders/cmake-clang-tools-x86_64-linux/builds/23351 http://lab.llvm.org:8011/builders/clang-s390x-linux/builds/492 llvm-svn: 260097
2016-02-08Expand the simplify boolean expression check to handle implicit conversion ↵Aaron Ballman1-93/+136
of integral types to bool and improve the handling of implicit conversion of member pointers to bool. Implicit conversion of member pointers are replaced with explicit comparisons to nullptr. Implicit conversions of integral types are replaced with explicit comparisons to 0. Patch by Richard Thomson. llvm-svn: 260096
2015-12-29[clang-tidy] Fix a use-after-free bug found by asanAlexander Kornienko1-2/+3
llvm-svn: 256562
2015-12-28[clang-tidy] Preserve comments and preprocessor directives when simplifying ↵Alexander Kornienko1-39/+68
boolean expressions This changeset still emits the diagnostic that the expression could be simplified, but it doesn't generate any fix-its that would lose comments or preprocessor directives within the text that would be replaced. Fixes PR25842 Reviewers: alexfh Subscribers: xazax.hun, cfe-commits Patch by Richard Thomson! (+a naming style fix) Differential Revision: http://reviews.llvm.org/D15737 llvm-svn: 256492
2015-09-17Refactors AST matching code to use the new AST matcher names. This patch ↵Aaron Ballman1-22/+25
correlates to r247885 which performs the AST matcher rename in Clang. llvm-svn: 247886
2015-07-01[clang-tidy] Enhance clang-tidy readability-simplify-boolean-expr...Alexander Kornienko1-27/+230
Enhance clang-tidy readability-simplify-boolean-expr to handle 'if (e) return true; return false;' and improve replacement expressions. This changeset extends the simplify boolean expression check in clang-tidy to simplify if (e) return true; return false; to return e; (note the lack of an else clause on the if statement.) By default, chained conditional assignment is left unchanged, unless a configuration parameter is set to non-zero to override this behavior. It also improves the handling of replacement expressions to apply static_cast<bool>(expr) when expr is not of type bool. http://reviews.llvm.org/D9810 Patch by Richard Thomson! llvm-svn: 241155
2015-05-17[clang-tidy] Enhance clang-tidy readability-simplify-boolean-expr check...Alexander Kornienko1-11/+37
Enhance clang-tidy readability-simplify-boolean-expr check to handle chained conditional assignment and chained conditional return. Based on feedback from applying this tool to the clang/LLVM codebase, this changeset improves the readability-simplify-boolean-expr check so that conditional assignment or return statements at the end of a chain of if/else if statements are left unchanged unless a configuration option is supplied. http://reviews.llvm.org/D8996 Patch by Richard Thomson! llvm-svn: 237541