Skip to content

Clang does not warn on undefined shift in templated member function when using conditional expression #142248

Open
@pkennedyTT

Description

@pkennedyTT

As title says, clang doesn't warn about UB due to shifting beyond the width of type in specific circumstances.

Clang version:
Apple clang version 16.0.0 (clang-1600.0.26.6)
Target: arm64-apple-darwin23.6.0

Compile flags: -std=c++20 -Wall -Wextra -Wshift-count-overflow -pedantic

#include <iostream>
#include <type_traits>

template <typename T>
class Foo {
public:
    T foo() {
        return std::is_same_v<T, uint64_t> ? (1<<32): 0;  // No warning here
    }   
};

template <typename T>
T bar() {
    return std::is_same_v<T, uint64_t> ? (1<<32): 0;  // Warning here
}

int main()
{
    Foo<uint64_t> f;
    std::cout << f.foo() << " " << bar<uint64_t>() << "\n";
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions