Skip to content

fix: fix if-init done by AI#5312

Closed
asalzburger wants to merge 4 commits intoacts-project:mainfrom
asalzburger:chore-if-init
Closed

fix: fix if-init done by AI#5312
asalzburger wants to merge 4 commits intoacts-project:mainfrom
asalzburger:chore-if-init

Conversation

@asalzburger
Copy link
Copy Markdown
Contributor

C++17 if init-statement (Sonar)

***These are fixes done by cursor AI running on (part of) our SonarCloud output ***

Summary

This change addresses Sonar findings that ask to declare a variable in the init-statement of an if (C++17), instead of declaring it on the preceding line and only using it in the condition and immediate body. The refactor narrows scope, matches modern C++ style, and clears the corresponding “Use the init-statement to declare … inside the if statement” issues (76 in the exported Sonar report used for triage).

Pattern

Before:

auto x = f();
if (!x.ok()) {
  return x.error();
}

After:

if (auto x = f(); !x.ok()) {
  return x.error();
}

The same idea applies to pointers, booleans, iterator results, Result types, dynamic_cast results, and similar “compute once, then branch” code.

Notable cases

  • Whole if / else if chains: Names introduced in the init-statement are in scope for the entire if / else if / else chain. This is used for flags such as typeFlags and currentState where several branches need the same value.
  • CompositeSpacePointLineFitter: precResult is initialized in the first if; the following else if (precResult) remains valid because it is part of the same if statement.
  • GaussianSumFitter: One branch uses if constexpr (constexpr bool IsMultiParameters = …; !IsMultiParameters) (C++20), consistent with the project standard.
  • VectorHelpers::perp: if constexpr with an init-statement for RowsAtCompileTime.
  • SteppingHelper: farLimit is only needed for the reachability check; it is moved into that if’s init-statement while nearLimit stays outside.

Files touched (39)

Area Path
Alignment Alignment/include/ActsAlignment/Kernel/Alignment.ipp
Ambiguity Core/include/Acts/AmbiguityResolution/ScoreBasedAmbiguityResolution.ipp
Event data Core/include/Acts/EventData/MultiTrajectoryHelpers.hpp
Propagator Core/include/Acts/Propagator/DirectNavigator.hpp
Core/include/Acts/Propagator/EigenStepper.ipp
Core/include/Acts/Propagator/MaterialInteractor.hpp
Core/include/Acts/Propagator/MultiStepperLoop.ipp
Core/include/Acts/Propagator/Propagator.ipp
Core/include/Acts/Propagator/StandardAborters.hpp
Core/include/Acts/Propagator/SurfaceCollector.hpp
Core/include/Acts/Propagator/VolumeCollector.hpp
Core/include/Acts/Propagator/detail/SteppingHelper.hpp
Seeding Core/include/Acts/Seeding/BinnedGroupIterator.ipp
Core/include/Acts/Seeding/CompositeSpacePointLineFitter.ipp
Track finding Core/include/Acts/TrackFinding/CombinatorialKalmanFilter.hpp
Core/include/Acts/TrackFinding/TrackStateCreator.hpp
Track fitting Core/include/Acts/TrackFitting/GaussianSumFitter.hpp
Core/include/Acts/TrackFitting/GlobalChiSquareFitter.hpp
Core/include/Acts/TrackFitting/KalmanFitter.hpp
Core/include/Acts/TrackFitting/detail/GsfActor.hpp
Utilities Core/include/Acts/Utilities/AlgebraHelpers.hpp
Core/include/Acts/Utilities/Frustum.ipp
Core/include/Acts/Utilities/TrackHelpers.hpp
Core/include/Acts/Utilities/TypeDispatcher.hpp
Core/include/Acts/Utilities/UnitVectors.hpp
Core/include/Acts/Utilities/VectorHelpers.hpp
Core sources Core/src/EventData/CorrectedTransformationFreeToBound.cpp
Geometry Core/src/Geometry/ConeVolumeBounds.cpp
Core/src/Geometry/CuboidVolumeStack.cpp
Core/src/Geometry/CutoutCylinderVolumeBounds.cpp
Core/src/Geometry/CylinderPortalShell.cpp
Core/src/Geometry/CylinderVolumeBuilder.cpp
Core/src/Geometry/CylinderVolumeHelper.cpp
Core/src/Geometry/CylinderVolumeStack.cpp
Core/src/Geometry/DiscLayer.cpp
Core/src/Geometry/GlueVolumesDescriptor.cpp
Core/src/Geometry/LayerArrayCreator.cpp
Core/src/Geometry/MultiWireVolumeBuilder.cpp

Verification

  • Build: acts build acts-sonarcloud (from ~/Documents/work/dev with your usual acts environment).
  • Tests: acts test acts-sonarcloud (full ctest suite for the project).

No intended behavioral change: only scope and style, except where the previous variable would have leaked into a wider block (now correctly limited to the if statement).

--- END COMMIT MESSAGE ---

Any further description goes here, @-mentions are ok here!

  • Use a conventional commits prefix: quick summary
    • We mostly use feat, fix, refactor, docs, chore and build types.
  • A milestone will be assigned by one of the maintainers

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

📊: Physics performance monitoring for b6183fd

Full contents

physmon summary

@andiwand andiwand mentioned this pull request Apr 3, 2026
2 tasks
// Set the alignment mask
auto iter_it = alignOptions.iterationState.find(iIter);
if (iter_it != alignOptions.iterationState.end()) {
if (auto iter_it = alignOptions.iterationState.find(iIter);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto iter_it = alignOptions.iterationState.find(iIter);
if (const auto iter_it = alignOptions.iterationState.find(iIter);

alignOptions.fitOptions.geoContext, alignOptions.alignedDetElements,
alignOptions.alignedTransformUpdater, alignResult);
if (!updateRes.ok()) {
if (auto updateRes = updateAlignmentParameters(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto updateRes = updateAlignmentParameters(
if (const auto updateRes = updateAlignmentParameters(

auto isoutliner = ts.typeFlags().isOutlier();

if (isoutliner) {
if (auto isoutliner = ts.typeFlags().isOutlier(); isoutliner) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto isoutliner = ts.typeFlags().isOutlier(); isoutliner) {
if (const bool isOutliner = ts.typeFlags().isOutlier(); isOutliner) {

trajState.nStates++;
auto typeFlags = state.typeFlags();
if (typeFlags.isHole()) {
if (auto typeFlags = state.typeFlags(); typeFlags.isHole()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto typeFlags = state.typeFlags(); typeFlags.isHole()) {
if (const auto typeFlags = state.typeFlags(); typeFlags.isHole()) {

trajState.NDF += state.calibratedSize();
auto typeFlags = state.typeFlags();
if (typeFlags.isHole()) {
if (auto typeFlags = state.typeFlags(); typeFlags.isHole()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto typeFlags = state.typeFlags(); typeFlags.isHole()) {
if (const auto typeFlags = state.typeFlags(); typeFlags.isHole()) {

Comment on lines +113 to +114
if (auto currentSurface = navigator.currentSurface(state.navigation);
currentSurface && selector(*currentSurface)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto currentSurface = navigator.currentSurface(state.navigation);
currentSurface && selector(*currentSurface)) {
if (const Surface* currentSurface = navigator.currentSurface(state.navigation);
currentSurface != nullptr && selector(*currentSurface)) {

Comment on lines +108 to +109
if (auto currentVolume = navigator.currentVolume(state.navigation);
currentVolume && selector(*currentVolume)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto currentVolume = navigator.currentVolume(state.navigation);
currentVolume && selector(*currentVolume)) {
if (const Volume* currentVolume = navigator.currentVolume(state.navigation);
currentVolume != nullptr && selector(*currentVolume)) {

Comment on lines +156 to +160
if (precResult_t precResult =
doPrecFit ? fastPrecFit<fitStraws, fitTime>(
measurements, initialGuess, precFitDelegate)
: std::nullopt;
doPrecFit && !precResult) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks more involved now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous is better, readability is worse now and an if clause within an if initialization doesn't really help.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I would also argue that it hurts readability

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here I fear it hurts the algorithm flow

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false positive IMO

// register the face
auto searchIter = m_glueVolumes.find(bsf);
if (searchIter == m_glueVolumes.end()) {
if (auto searchIter = m_glueVolumes.find(bsf);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto searchIter = m_glueVolumes.find(bsf);
if (const auto searchIter = m_glueVolumes.find(bsf);

// searching for the glue volumes according
auto searchIter = m_glueVolumes.find(bsf);
if (searchIter != m_glueVolumes.end()) {
if (auto searchIter = m_glueVolumes.find(bsf);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto searchIter = m_glueVolumes.find(bsf);
if (const auto searchIter = m_glueVolumes.find(bsf);

Comment on lines +40 to +41
if (auto boundsType = m_config.bounds ? m_config.bounds->type()
: VolumeBounds::BoundsType::eOther;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto boundsType = m_config.bounds ? m_config.bounds->type()
: VolumeBounds::BoundsType::eOther;
if (const VolumeBounds::BoundsType boundsType = m_config.bounds ? m_config.bounds->type()
: VolumeBounds::BoundsType::eOther;

Comment on lines +42 to 44
!(boundsType == VolumeBounds::BoundsType::eTrapezoid ||
boundsType == VolumeBounds::BoundsType::eCuboid ||
boundsType == VolumeBounds::BoundsType::eDiamond)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a local using enum could improve readability here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had tried another one for the using enum but I need to narrow that down to case-by-case.

asalzburger and others added 3 commits April 5, 2026 07:38
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
Copy link
Copy Markdown
Contributor Author

@asalzburger asalzburger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass through, no reverts yet.

Comment on lines 64 to +67
const double nearLimit = std::numeric_limits<double>::lowest();
const double farLimit = std::numeric_limits<double>::max();

if (sIntersection.isValid() &&
if (const double farLimit = std::numeric_limits<double>::max();
sIntersection.isValid() &&
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, makes it better readable.

Comment on lines +304 to 310
if (const NavigationTarget target = chooseIntersection(
state.options.geoContext, surface, position, direction,
BoundaryTolerance::Infinite(), state.options.nearLimit, farLimit,
state.options.surfaceTolerance);
target.isValid()) {
return target;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point taken.

Comment on lines +382 to 384
if (const double initialStepLength = std::abs(initialH);
nextAccuracy < initialStepLength || nextAccuracy > previousAccuracy) {
state.stepSize.setAccuracy(nextAccuracy);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed.

Comment on lines +147 to +152
if (const auto cmpsOnSurface =
std::count_if(components.cbegin(), components.cend(),
[&](auto& cmp) {
return cmp.status == IntersectionStatus::onSurface;
});
cmpsOnSurface > 0) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous is better.

Comment on lines +156 to +160
if (precResult_t precResult =
doPrecFit ? fastPrecFit<fitStraws, fitTime>(
measurements, initialGuess, precFitDelegate)
: std::nullopt;
doPrecFit && !precResult) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous is better, readability is worse now and an if clause within an if initialization doesn't really help.

Comment on lines +42 to 44
!(boundsType == VolumeBounds::BoundsType::eTrapezoid ||
boundsType == VolumeBounds::BoundsType::eCuboid ||
boundsType == VolumeBounds::BoundsType::eDiamond)) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had tried another one for the using enum but I need to narrow that down to case-by-case.

@asalzburger asalzburger marked this pull request as draft April 5, 2026 07:03
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 5, 2026

@asalzburger
Copy link
Copy Markdown
Contributor Author

I am gonna close this one, and try to refine the prompt.

@asalzburger asalzburger closed this Apr 7, 2026
@andiwand andiwand modified the milestones: next, v46.2.0 Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants