Advertisement
MaestroMaus

Clang Tidy Bug Example

Jul 5th, 2019
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. // C++ version: 17
  2. // filename: CardEncyclopedia
  3. #pragma once
  4. #include <vector>
  5.  
  6. struct Card
  7. {
  8.     bool operator!=(const Card& other) const noexcept
  9.     {
  10.         return false;
  11.     }
  12. };
  13.  
  14. class CardEncyclopedia
  15. {
  16.     std::vector<const Card&> cards;
  17.  
  18. public:
  19.     // the content of the function block below will be highlighted in its entirety
  20.     // the given warning is: "Redundant boolean literal in conditional return statement [readability-simplify-boolean-expr]"
  21.     // after the tidy operation the content of the block will be reduced down to one line: return !;
  22.     bool operator==(const CardEncyclopedia& other) noexcept
  23.     {
  24.         if (cards.size() != other.cards.size())
  25.             return false;
  26.  
  27.         for (size_t i = 0; i < other.cards.size(); ++i)
  28.             if (cards.at(i) != other.cards.at(i))
  29.                 return false;
  30.  
  31.         return true;
  32.     }
  33. };
  34.  
  35. // Note that vectors should not accept references because the component type of containers, like vectors,
  36. // must be re-assignable and references are not re-assignable. However, the compiler will not stop you from doing this.
  37. // This same issue also exists when using const pointers (because again; they are not re-assignable).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement