llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-tidy] New check for static_cast of an enum class type

Open FalcoGer opened this issue 3 years ago • 17 comments

When casting an enum class type using static_cast<T>, this may result in a bug if the type T is not correct. In c++23 we have std::to_underlying which should be used instead. This also is more desirable because if the enum's underlying type is changed later, the casting code is automatically still correct without the need to make the change there, too.

// Fix, must add, if not already present:
#include <utility> // Provides std::to_underlying()


// NOLINTNEXTLINE(performance-enum-size)
enum class EType : int {};

auto main() -> int
{
    // NOLINTNEXTLINE(readability-magic-numbers)
    EType e {42};

    [[maybe_unused]]
    // c++23 and later only

    // Original code:
    // auto i = static_cast<int>(e);
    //          ~~~~~~~~~~~~~~~~
    //          ^ bugprone: use std::to_underlying
    // Fix:
    auto i = std::to_underlying(e);
    return 0;
}

FalcoGer avatar Nov 07 '23 14:11 FalcoGer

@llvm/issue-subscribers-clang-tidy

Author: None (FalcoGer)

When casting an `enum class` type using `static_cast<T>`, this may result in a bug if the type `T` is not correct. In c++23 we have `std::to_underlying` which should be used instead. This also is more desirable because if the enum's underlying type is changed later, the casting code is automatically still correct without the need to make the change there, too.
// Fix, must add, if not already present:
#include &lt;utility&gt; // Provides std::to_underlying()


// NOLINTNEXTLINE(performance-enum-size)
enum class EType : int {};

auto main() -&gt; int
{
    // NOLINTNEXTLINE(readability-magic-numbers)
    EType e {42};

    [[maybe_unused]]
    // c++23 and later only

    // Original code:
    // auto i = static_cast&lt;int&gt;(e);
    //          ~~~~~~~~~~~~~~~~
    //          ^ bugprone: use std::to_underlying
    // Fix:
    auto i = std::to_underlying(e);
    return 0;
}

llvmbot avatar Aug 09 '25 05:08 llvmbot

modernize-use-std-to-underlying?

It would be good to have an option for the user to specify their own hand-rolled version of this function, since nobody is going to wait for C++23 for it :) The check should probably not warn about the static_cast done inside that function.

carlosgalvezp avatar Aug 09 '25 07:08 carlosgalvezp

The check should probably not warn about the static_cast done inside that function.

I think one-time suppression will be enough for this. This function is likely to come from system headers so it will be filtered out anyway most of the time.

vbvictor avatar Aug 09 '25 08:08 vbvictor

Otherwise, we would need to look for functionDecl that 99.99% will be useless

vbvictor avatar Aug 09 '25 08:08 vbvictor

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot avatar Aug 09 '25 15:08 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: None (FalcoGer)

When casting an `enum class` type using `static_cast<T>`, this may result in a bug if the type `T` is not correct. In c++23 we have `std::to_underlying` which should be used instead. This also is more desirable because if the enum's underlying type is changed later, the casting code is automatically still correct without the need to make the change there, too.
// Fix, must add, if not already present:
#include &lt;utility&gt; // Provides std::to_underlying()


// NOLINTNEXTLINE(performance-enum-size)
enum class EType : int {};

auto main() -&gt; int
{
    // NOLINTNEXTLINE(readability-magic-numbers)
    EType e {42};

    [[maybe_unused]]
    // c++23 and later only

    // Original code:
    // auto i = static_cast&lt;int&gt;(e);
    //          ~~~~~~~~~~~~~~~~
    //          ^ bugprone: use std::to_underlying
    // Fix:
    auto i = std::to_underlying(e);
    return 0;
}

llvmbot avatar Aug 09 '25 15:08 llvmbot

Please read https://clang.llvm.org/extra/clang-tidy/Contributing.html for guides on how to write clang-tidy check. Most important steps are:

  1. Use add_new_check.py to booststrap, take name modernize-use-to-underlying (I omitted std as opposed to @carlosgalvezp suggestion because most of the check don't do that).
  2. Create options for custom to_underlying function and include header, look https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-std-format.html for reference (ReplacementFormatFunction and FormatHeader options).
  3. Write comprehensive test suite that check both default behavior and custom functions behavior
  4. Follow https://clang.llvm.org/extra/clang-tidy/Contributing.html#making-your-check-robust and https://clang.llvm.org/extra/clang-tidy/Contributing.html#making-your-check-robust guides

As an inspiration, there is similar rule in SonarQube https://rules.sonarsource.com/cpp/tag/since-c++23/RSPEC-7035/, but our version will be better and support e.g. llvm::to_underlying too :)

vbvictor avatar Aug 09 '25 15:08 vbvictor

modernize-use-std-to-underlying?

I like that name.

It would be good to have an option for the user to specify their own hand-rolled version of this function, since nobody is going to wait for C++23 for it :) The check should probably not warn about the static_cast done inside that function.

What I've been doing in my code is to write operator+ for the enum type that does the cast.

See input_field.h for an example.

This means that you write +e to get the enum value as underlying type.

LegalizeAdulthood avatar Aug 10 '25 04:08 LegalizeAdulthood

write +e

Arguably that's a detriment in readability :) I believe we have a check warning about that (for ints only).

carlosgalvezp avatar Aug 10 '25 06:08 carlosgalvezp

write +e

Arguably that's a detriment in readability :) I believe we have a check warning about that (for ints only).

Readability is -- as always -- subjective. I don't know what you consider more readable, but static_cast<int>(e) is not what I consider more readable.

Warning about +e should never happen when e is a type that has a user-defined operator+.

LegalizeAdulthood avatar Aug 11 '25 15:08 LegalizeAdulthood

Warning about +e should never happen when e is a type that has a user-defined operator+.

It hardly will, because we are most likely to match only cast expressions and not operators

vbvictor avatar Aug 11 '25 16:08 vbvictor

my team would like to work on this issue

arjunkonidala avatar Nov 02 '25 10:11 arjunkonidala

@arjunkonidala, Go ahead, I've assigned issue to you.

vbvictor avatar Nov 02 '25 11:11 vbvictor

New attempt in https://github.com/llvm/llvm-project/pull/210459 (since I guess the old PR #167212 is dead and I would also like to use this check)

vogelsgesang avatar Jul 17 '26 23:07 vogelsgesang

is there anyone else working on this issue? iam a new contributor can i work on this issue?

sangam151 avatar Sep 03 '26 16:09 sangam151

is there anyone else working on this issue? iam a new contributor can i work on this issue?

Yes, there is PR in review https://github.com/llvm/llvm-project/pull/210459

vbvictor avatar Sep 03 '26 17:09 vbvictor

Done in https://github.com/llvm/llvm-project/pull/210459

vbvictor avatar Sep 07 '26 20:09 vbvictor