Skip to content

Commit

Permalink
Shush cppcheck, you don't know your type traits (but we run an outdat…
Browse files Browse the repository at this point in the history
…ed you, so I forgive you)

https://godbolt.org/z/Wo7dPqvs3

```c++
#include <fmt/compile.h>
#include <fmt/format.h>

#include <type_traits>
#include <variant>

using VariantType = std::variant<int, unsigned int>;

template <class>
inline constexpr bool always_false_v = false;

constexpr void test_type(auto&& arg) {
    using T = std::decay_t<decltype(arg)>;
    if constexpr (std::is_same_v<T, unsigned int>) {
        fmt::print(FMT_COMPILE("Hey there unsigned! cppcheck thought you wouldn't be here\n"));
    } else if constexpr (std::is_same_v<T, int>) {
        fmt::print(FMT_COMPILE("Hello integer, how are you?\n"));
    } else {
        static_assert(always_false_v<T>, "non-exhaustive visitor!");
    }
}

constexpr void test_variant_type(const VariantType& arg) {
    std::visit(
        [](auto&& arg) {
            using T = std::decay_t<decltype(arg)>;
            if constexpr (std::is_same_v<T, int>) {
                fmt::print(FMT_COMPILE("Hello integer, how are you?\n"));
            } else if constexpr (std::is_same_v<T, unsigned int>) {
            fmt::print(FMT_COMPILE("Hey there unsigned! cppcheck thought you wouldn't be here\n"));
            } else {
                static_assert(always_false_v<T>, "non-exhaustive visitor!");
            }
        },
        arg);
}

int main() {
    int i = 10;
    test_type(i);
    unsigned u = 10;
    test_type(u);

    fmt::print("Variant\n");
    auto iv = VariantType(i);
    auto uv = VariantType(u);
    test_variant_type(iv);
    test_variant_type(uv);
}
```
  • Loading branch information
jmarrec committed Apr 18, 2024
1 parent 9235d7d commit 9e8871e
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/utilities/data/Attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ namespace detail {
return arg;
} else if constexpr (std::is_same_v<T, int>) {
return arg;
// cppcheck-suppress identicalConditionAfterEarlyExit
// cppcheck-suppress multiCondition
} else if constexpr (std::is_same_v<T, unsigned int>) {
return arg;
} else if constexpr (std::is_same_v<T, std::string>) {
Expand Down

0 comments on commit 9e8871e

Please sign in to comment.