[clang-tidy] New check for static_cast of an enum class type
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;
}
@llvm/issue-subscribers-clang-tidy
Author: None (FalcoGer)
// 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;
}
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.
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.
Otherwise, we would need to look for functionDecl that 99.99% will be useless
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:
- 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.
- 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.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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.
@llvm/issue-subscribers-good-first-issue
Author: None (FalcoGer)
// 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;
}
Please read https://clang.llvm.org/extra/clang-tidy/Contributing.html for guides on how to write clang-tidy check. Most important steps are:
- Use
add_new_check.pyto booststrap, take namemodernize-use-to-underlying(I omittedstdas opposed to @carlosgalvezp suggestion because most of the check don't do that). - Create options for custom
to_underlyingfunction and include header, look https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-std-format.html for reference (ReplacementFormatFunctionandFormatHeaderoptions). - Write comprehensive test suite that check both default behavior and custom functions behavior
- 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 :)
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_castdone 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.
write
+e
Arguably that's a detriment in readability :) I believe we have a check warning about that (for ints only).
write
+eArguably 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+.
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
my team would like to work on this issue
@arjunkonidala, Go ahead, I've assigned issue to you.
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)
is there anyone else working on this issue? iam a new contributor can i work on this issue?
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
Done in https://github.com/llvm/llvm-project/pull/210459