Enable lint to enforce 'static' on constexpr variables in functions

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Server Programmability
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      It is generally a performance bug to not make them static, as people assume constexpr means static duration. Objects get re-initialized each function call. This also can lead to UB if returning a constexpr object assuming it has a static lifetime.

      clang-tidy has an open PR with the appropriate check that has been open for a while. Can take the implementation to create our own check and then replace it when shipped. https://github.com/llvm/llvm-project/pull/147809/changes. See this issue for more details about the check + fix: https://github.com/llvm/llvm-project/issues/146296

       

      NOTE: Can't do this for constexpr or consteval functions until C++23. They don't support static in the function bodies. See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2647r1.html

            Assignee:
            Stewart Smith
            Reporter:
            Stewart Smith
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: