[PATCH] D81272: [Clang-Tidy] New check `misc-redundant-condition`

Balogh, Ádám via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 5 09:26:12 PDT 2020


baloghadamsoftware created this revision.
baloghadamsoftware added reviewers: aaron.ballman, gribozavr2.
baloghadamsoftware added a project: clang-tools-extra.
Herald added subscribers: martong, steakhal, gamesh411, Szelethus, dkrupp, rnkovacs, xazax.hun, whisperity, mgorny.
Herald added a project: clang.
baloghadamsoftware marked an inline comment as done.
baloghadamsoftware added a comment.

This check was made upon user request. I think it is a good base that can later be extended also for the negated cases (where the code inside the inner `if` never executes). That case is even more suspicious. I put it into `misc` because it is not such a bug that it should go into `bugprone` but rather a suspicious thing. The type of the bug is somewhat similar to the bugs detected by `misc-redundant-expression` so I think `misc` is the best place also for this one.



================
Comment at: clang-tools-extra/docs/clang-tidy/checks/list.rst:135
    `cppcoreguidelines-avoid-goto <cppcoreguidelines-avoid-goto.html>`_,
+   `cppcoreguidelines-avoid-non-const-global-variables <cppcoreguidelines-avoid-non-const-global-variables.html>`_,
    `cppcoreguidelines-init-variables <cppcoreguidelines-init-variables.html>`_, "Yes"
----------------
What are these changes? I surely did not make them? Maybe the check adder Python script?


Checking the same condition again in a nested if usually make no sense, except if the value of the expression could have been changed between the two checks. Although compilers may optimize this out, such code is suspicious: the programmer may have meant to check something else. Therefore it is worth to find such places in the code and notify the user about the problem.

This patch implements a basic check for this problem. Currently it only detects redundant conditions where the condition is a variable of integral type. It also detects the possible bug if the variable is in an "or" or "and" logical expression in the inner if and/or the variable is in an "and" logical expression in the outer if statement. Negated cases are not handled yet.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81272

Files:
  clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
  clang-tools-extra/clang-tidy/misc/CMakeLists.txt
  clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
  clang-tools-extra/clang-tidy/misc/RedundantConditionCheck.cpp
  clang-tools-extra/clang-tidy/misc/RedundantConditionCheck.h
  clang-tools-extra/clang-tidy/utils/Aliasing.cpp
  clang-tools-extra/clang-tidy/utils/Aliasing.h
  clang-tools-extra/clang-tidy/utils/CMakeLists.txt
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/misc-redundant-condition.rst
  clang-tools-extra/test/clang-tidy/checkers/misc-redundant-condition.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81272.268838.patch
Type: text/x-patch
Size: 51847 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200605/39f27704/attachment-0001.bin>


More information about the cfe-commits mailing list