Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace C-style casts #3146

Merged
merged 32 commits into from
Apr 27, 2024
Merged

Conversation

AJPfleger
Copy link
Contributor

@AJPfleger AJPfleger commented Apr 25, 2024

Summary

This pull request updates to use modern C++ style casts (static_cast<int>(variable), dynamic_cast, reinterpret_cast, const_cast) instead of C-style casts like (int)variable. The change enhances code safety, readability, and maintainability by making type conversions explicit and type-safe.

  • Replaced most instances of C-style casts with their appropriate C++ style counterparts.
  • Reviewed each conversion to ensure the chosen C++ cast is suitable for its specific use case.
  • Use uniform initialization, where it is clear, e.g. type{3.14}

Benefits

  • Improved Type Safety: Prevents the compiler from performing potentially unsafe conversions.
  • Increased Clarity and Maintainability: Makes the intentions clear, especially regarding the nature of each cast.
  • Easier to search for casts by search for _cast<.

CI

I experimented a bit with clang-tidy but couldn't find a proper test, that warns correctly. I tried cppcoreguidelines-pro-type-cstyle-cast but this is only suited for a subset. Using a regex like \(([^\s()]+)\)[a-zA-Z_][a-zA-Z0-9_]* creates too many false positives.

google-readability-casting seems promising and will be added in:

@AJPfleger AJPfleger added this to the next milestone Apr 25, 2024
@github-actions github-actions bot added Component - Core Affects the Core module Component - Fatras Affects the Fatras module Component - Examples Affects the Examples module Event Data Model Vertexing Track Finding labels Apr 25, 2024
@CouthuresJeremy
Copy link
Contributor

For the CI test, did you try google-readability-casting?

@github-actions github-actions bot added the Component - Plugins Affects one or more Plugins label Apr 26, 2024
@github-actions github-actions bot added Infrastructure Changes to build tools, continous integration, ... Seeding labels Apr 26, 2024
@github-actions github-actions bot removed the Infrastructure Changes to build tools, continous integration, ... label Apr 27, 2024
@AJPfleger
Copy link
Contributor Author

For the CI test, did you try google-readability-casting?

@CouthuresJeremy thanks, this one is a bit stricter, than planned, but looks good.

@AJPfleger AJPfleger removed the 🚧 WIP Work-in-progress label Apr 27, 2024
@AJPfleger AJPfleger requested a review from andiwand April 27, 2024 11:08
@kodiakhq kodiakhq bot merged commit eae64a4 into acts-project:main Apr 27, 2024
53 checks passed
@AJPfleger AJPfleger deleted the real-casting branch April 27, 2024 21:39
kodiakhq bot pushed a commit that referenced this pull request Apr 28, 2024
Adds a new clang-tidy check. Necessary changes and more details in the PR below.
blocked by:
- #3146

## Sample
```
╭─ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Vertexing/SingleSeedVertexF─╮
│ 🟡                                                                           │
│ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Vertexing/SingleSeedVertexFi │
│ nderTests.cpp:47:10 WARNING [google-readability-casting]                     │
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ C-style casts are discouraged; use static_cast                           │ │
│ │    47 |   return (int)(gen() / 4294967296. * (to - from) + from);        │ │
│ │       |          ^~~~~                                                   │ │
│ │       |          static_cast<int>                                        │ │
│ │                                                                          │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│ ──────────────────────────────────────────────────────────────────────────── │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Material/MaterialCompositio─╮
│ 🟡                                                                           │
│ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Material/MaterialComposition │
│ Tests.cpp:28:26 WARNING [google-readability-casting]                         │
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ C-style casts are discouraged; use static_cast                           │ │
│ │    28 |   float carbonFraction = float(carbonWeight) / 255u;             │ │
│ │       |                          ^~~~~                                   │ │
│ │       |                          static_cast<float>                      │ │
│ │                                                                          │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│ ──────────────────────────────────────────────────────────────────────────── │
╰──────────────────────────────────────────────────────────────────────────────╯
```
EleniXoch pushed a commit to EleniXoch/acts that referenced this pull request May 6, 2024
## Summary

This pull request updates to use modern C++ style casts (`static_cast<int>(variable)`, `dynamic_cast`, `reinterpret_cast`, `const_cast`) instead of C-style casts like `(int)variable`. The change enhances code safety, readability, and maintainability by making type conversions explicit and type-safe.

- Replaced most instances of C-style casts with their appropriate C++ style counterparts.
- Reviewed each conversion to ensure the chosen C++ cast is suitable for its specific use case.
- Use uniform initialization, where it is clear, e.g. `type{3.14}`

## Benefits

- Improved Type Safety: Prevents the compiler from performing potentially unsafe conversions.
- Increased Clarity and Maintainability: Makes the intentions clear, especially regarding the nature of each cast.
- Easier to search for casts by search for `_cast<`.

## CI

I experimented a bit with clang-tidy but couldn't find a proper test, that warns correctly. I tried `cppcoreguidelines-pro-type-cstyle-cast` but this is only suited for a subset. Using a regex like `\(([^\s()]+)\)[a-zA-Z_][a-zA-Z0-9_]*` creates too many false positives.

`google-readability-casting` seems promising and will be added in:
- acts-project#3149
EleniXoch pushed a commit to EleniXoch/acts that referenced this pull request May 6, 2024
Adds a new clang-tidy check. Necessary changes and more details in the PR below.
blocked by:
- acts-project#3146

## Sample
```
╭─ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Vertexing/SingleSeedVertexF─╮
│ 🟡                                                                           │
│ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Vertexing/SingleSeedVertexFi │
│ nderTests.cpp:47:10 WARNING [google-readability-casting]                     │
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ C-style casts are discouraged; use static_cast                           │ │
│ │    47 |   return (int)(gen() / 4294967296. * (to - from) + from);        │ │
│ │       |          ^~~~~                                                   │ │
│ │       |          static_cast<int>                                        │ │
│ │                                                                          │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│ ──────────────────────────────────────────────────────────────────────────── │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Material/MaterialCompositio─╮
│ 🟡                                                                           │
│ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Material/MaterialComposition │
│ Tests.cpp:28:26 WARNING [google-readability-casting]                         │
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ C-style casts are discouraged; use static_cast                           │ │
│ │    28 |   float carbonFraction = float(carbonWeight) / 255u;             │ │
│ │       |                          ^~~~~                                   │ │
│ │       |                          static_cast<float>                      │ │
│ │                                                                          │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│ ──────────────────────────────────────────────────────────────────────────── │
╰──────────────────────────────────────────────────────────────────────────────╯
```
@andiwand andiwand modified the milestones: next, v35.0.0 May 17, 2024
asalzburger pushed a commit to asalzburger/acts that referenced this pull request May 21, 2024
## Summary

This pull request updates to use modern C++ style casts (`static_cast<int>(variable)`, `dynamic_cast`, `reinterpret_cast`, `const_cast`) instead of C-style casts like `(int)variable`. The change enhances code safety, readability, and maintainability by making type conversions explicit and type-safe.

- Replaced most instances of C-style casts with their appropriate C++ style counterparts.
- Reviewed each conversion to ensure the chosen C++ cast is suitable for its specific use case.
- Use uniform initialization, where it is clear, e.g. `type{3.14}`

## Benefits

- Improved Type Safety: Prevents the compiler from performing potentially unsafe conversions.
- Increased Clarity and Maintainability: Makes the intentions clear, especially regarding the nature of each cast.
- Easier to search for casts by search for `_cast<`.

## CI

I experimented a bit with clang-tidy but couldn't find a proper test, that warns correctly. I tried `cppcoreguidelines-pro-type-cstyle-cast` but this is only suited for a subset. Using a regex like `\(([^\s()]+)\)[a-zA-Z_][a-zA-Z0-9_]*` creates too many false positives.

`google-readability-casting` seems promising and will be added in:
- acts-project#3149
asalzburger pushed a commit to asalzburger/acts that referenced this pull request May 21, 2024
Adds a new clang-tidy check. Necessary changes and more details in the PR below.
blocked by:
- acts-project#3146

## Sample
```
╭─ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Vertexing/SingleSeedVertexF─╮
│ 🟡                                                                           │
│ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Vertexing/SingleSeedVertexFi │
│ nderTests.cpp:47:10 WARNING [google-readability-casting]                     │
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ C-style casts are discouraged; use static_cast                           │ │
│ │    47 |   return (int)(gen() / 4294967296. * (to - from) + from);        │ │
│ │       |          ^~~~~                                                   │ │
│ │       |          static_cast<int>                                        │ │
│ │                                                                          │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│ ──────────────────────────────────────────────────────────────────────────── │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Material/MaterialCompositio─╮
│ 🟡                                                                           │
│ /builds/acts/ci-bridge/src/Tests/UnitTests/Core/Material/MaterialComposition │
│ Tests.cpp:28:26 WARNING [google-readability-casting]                         │
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ C-style casts are discouraged; use static_cast                           │ │
│ │    28 |   float carbonFraction = float(carbonWeight) / 255u;             │ │
│ │       |                          ^~~~~                                   │ │
│ │       |                          static_cast<float>                      │ │
│ │                                                                          │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│ ──────────────────────────────────────────────────────────────────────────── │
╰──────────────────────────────────────────────────────────────────────────────╯
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component - Core Affects the Core module Component - Examples Affects the Examples module Component - Fatras Affects the Fatras module Component - Plugins Affects one or more Plugins Event Data Model Seeding Track Finding Vertexing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants