Skip to content

fix: fix if-init done by AI#5312

Open
asalzburger wants to merge 1 commit intoacts-project:mainfrom
asalzburger:chore-if-init
Open

fix: fix if-init done by AI#5312
asalzburger wants to merge 1 commit 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

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 3, 2026

// 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 +85 to +86
if (bool passesMask = m_group->mask().at(m_gridItr.globalBinIndex());
dimCollection > 0ul && passesMask) {
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 (bool passesMask = m_group->mask().at(m_gridItr.globalBinIndex());
dimCollection > 0ul && passesMask) {
if (const bool passesMask = m_group->mask().at(m_gridItr.globalBinIndex());
dimCollection > 0ul && passesMask) {

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

Comment on lines +565 to +566
if (std::optional<SquareMatrix<N>> inverseH{safeInverse(miniHessian)};
inverseH) {
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 (std::optional<SquareMatrix<N>> inverseH{safeInverse(miniHessian)};
inverseH) {
if (const std::optional<SquareMatrix<N>> inverseH = safeInverse(miniHessian);
inverseH.has_value()) {

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

Comment on lines +116 to +117
if (SourceLinkRange slRange = sourceLinkAccessor(surface);
slRange.first != slRange.second) {
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 (SourceLinkRange slRange = sourceLinkAccessor(surface);
slRange.first != slRange.second) {
if (const auto slRange = sourceLinkAccessor(surface);
slRange.first != slRange.second) {

@@ -113,8 +113,8 @@ struct TrackStateCreator {
TrackStateContainerBackend& trajectory, const Logger& logger) const {
TrackStatesResult tsRes = TrackStatesResult::success({});
using SourceLinkRange = decltype(sourceLinkAccessor(surface));
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
using SourceLinkRange = decltype(sourceLinkAccessor(surface));

Comment on lines +206 to +210
if (Result<std::pair<typename std::vector<TrackStateProxy>::iterator,
typename std::vector<TrackStateProxy>::iterator>>
selectorResult =
measurementSelector(trackStateCandidates, isOutlier, logger);
!selectorResult.ok()) {
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 (Result<std::pair<typename std::vector<TrackStateProxy>::iterator,
typename std::vector<TrackStateProxy>::iterator>>
selectorResult =
measurementSelector(trackStateCandidates, isOutlier, logger);
!selectorResult.ok()) {
if (const auto selectorResult =
measurementSelector(trackStateCandidates, isOutlier, logger);
!selectorResult.ok()) {

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

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