fix: fix if-init done by AI#5312
Conversation
| // 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); |
There was a problem hiding this comment.
| 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( |
There was a problem hiding this comment.
| if (auto updateRes = updateAlignmentParameters( | |
| if (const auto updateRes = updateAlignmentParameters( |
| auto isoutliner = ts.typeFlags().isOutlier(); | ||
|
|
||
| if (isoutliner) { | ||
| if (auto isoutliner = ts.typeFlags().isOutlier(); isoutliner) { |
There was a problem hiding this comment.
| 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()) { |
There was a problem hiding this comment.
| 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()) { |
There was a problem hiding this comment.
| if (auto typeFlags = state.typeFlags(); typeFlags.isHole()) { | |
| if (const auto typeFlags = state.typeFlags(); typeFlags.isHole()) { |
| if (auto currentSurface = navigator.currentSurface(state.navigation); | ||
| currentSurface && selector(*currentSurface)) { |
There was a problem hiding this comment.
| if (auto currentSurface = navigator.currentSurface(state.navigation); | |
| currentSurface && selector(*currentSurface)) { | |
| if (const Surface* currentSurface = navigator.currentSurface(state.navigation); | |
| currentSurface != nullptr && selector(*currentSurface)) { |
| if (auto currentVolume = navigator.currentVolume(state.navigation); | ||
| currentVolume && selector(*currentVolume)) { |
There was a problem hiding this comment.
| if (auto currentVolume = navigator.currentVolume(state.navigation); | |
| currentVolume && selector(*currentVolume)) { | |
| if (const Volume* currentVolume = navigator.currentVolume(state.navigation); | |
| currentVolume != nullptr && selector(*currentVolume)) { |
| if (precResult_t precResult = | ||
| doPrecFit ? fastPrecFit<fitStraws, fitTime>( | ||
| measurements, initialGuess, precFitDelegate) | ||
| : std::nullopt; | ||
| doPrecFit && !precResult) { |
There was a problem hiding this comment.
Previous is better, readability is worse now and an if clause within an if initialization doesn't really help.
There was a problem hiding this comment.
here I would also argue that it hurts readability
There was a problem hiding this comment.
also here I fear it hurts the algorithm flow
| // register the face | ||
| auto searchIter = m_glueVolumes.find(bsf); | ||
| if (searchIter == m_glueVolumes.end()) { | ||
| if (auto searchIter = m_glueVolumes.find(bsf); |
There was a problem hiding this comment.
| 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); |
There was a problem hiding this comment.
| if (auto searchIter = m_glueVolumes.find(bsf); | |
| if (const auto searchIter = m_glueVolumes.find(bsf); |
| if (auto boundsType = m_config.bounds ? m_config.bounds->type() | ||
| : VolumeBounds::BoundsType::eOther; |
There was a problem hiding this comment.
| 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; |
| !(boundsType == VolumeBounds::BoundsType::eTrapezoid || | ||
| boundsType == VolumeBounds::BoundsType::eCuboid || | ||
| boundsType == VolumeBounds::BoundsType::eDiamond)) { |
There was a problem hiding this comment.
a local using enum could improve readability here
There was a problem hiding this comment.
I had tried another one for the using enum but I need to narrow that down to case-by-case.
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
asalzburger
left a comment
There was a problem hiding this comment.
First pass through, no reverts yet.
| 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() && |
There was a problem hiding this comment.
I agree, makes it better readable.
| if (const NavigationTarget target = chooseIntersection( | ||
| state.options.geoContext, surface, position, direction, | ||
| BoundaryTolerance::Infinite(), state.options.nearLimit, farLimit, | ||
| state.options.surfaceTolerance); | ||
| target.isValid()) { | ||
| return target; | ||
| } |
| if (const double initialStepLength = std::abs(initialH); | ||
| nextAccuracy < initialStepLength || nextAccuracy > previousAccuracy) { | ||
| state.stepSize.setAccuracy(nextAccuracy); |
| if (const auto cmpsOnSurface = | ||
| std::count_if(components.cbegin(), components.cend(), | ||
| [&](auto& cmp) { | ||
| return cmp.status == IntersectionStatus::onSurface; | ||
| }); | ||
| cmpsOnSurface > 0) { |
There was a problem hiding this comment.
Previous is better.
| if (precResult_t precResult = | ||
| doPrecFit ? fastPrecFit<fitStraws, fitTime>( | ||
| measurements, initialGuess, precFitDelegate) | ||
| : std::nullopt; | ||
| doPrecFit && !precResult) { |
There was a problem hiding this comment.
Previous is better, readability is worse now and an if clause within an if initialization doesn't really help.
| !(boundsType == VolumeBounds::BoundsType::eTrapezoid || | ||
| boundsType == VolumeBounds::BoundsType::eCuboid || | ||
| boundsType == VolumeBounds::BoundsType::eDiamond)) { |
There was a problem hiding this comment.
I had tried another one for the using enum but I need to narrow that down to case-by-case.
|
|
I am gonna close this one, and try to refine the prompt. |



C++17
ifinit-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:
After:
The same idea applies to pointers, booleans, iterator results,
Resulttypes,dynamic_castresults, and similar “compute once, then branch” code.Notable cases
if/else ifchains: Names introduced in the init-statement are in scope for the entireif/else if/elsechain. This is used for flags such astypeFlagsandcurrentStatewhere several branches need the same value.CompositeSpacePointLineFitter:precResultis initialized in the firstif; the followingelse if (precResult)remains valid because it is part of the sameifstatement.GaussianSumFitter: One branch usesif constexpr (constexpr bool IsMultiParameters = …; !IsMultiParameters)(C++20), consistent with the project standard.VectorHelpers::perp:if constexprwith an init-statement forRowsAtCompileTime.SteppingHelper:farLimitis only needed for the reachability check; it is moved into thatif’s init-statement whilenearLimitstays outside.Files touched (39)
Alignment/include/ActsAlignment/Kernel/Alignment.ippCore/include/Acts/AmbiguityResolution/ScoreBasedAmbiguityResolution.ippCore/include/Acts/EventData/MultiTrajectoryHelpers.hppCore/include/Acts/Propagator/DirectNavigator.hppCore/include/Acts/Propagator/EigenStepper.ippCore/include/Acts/Propagator/MaterialInteractor.hppCore/include/Acts/Propagator/MultiStepperLoop.ippCore/include/Acts/Propagator/Propagator.ippCore/include/Acts/Propagator/StandardAborters.hppCore/include/Acts/Propagator/SurfaceCollector.hppCore/include/Acts/Propagator/VolumeCollector.hppCore/include/Acts/Propagator/detail/SteppingHelper.hppCore/include/Acts/Seeding/BinnedGroupIterator.ippCore/include/Acts/Seeding/CompositeSpacePointLineFitter.ippCore/include/Acts/TrackFinding/CombinatorialKalmanFilter.hppCore/include/Acts/TrackFinding/TrackStateCreator.hppCore/include/Acts/TrackFitting/GaussianSumFitter.hppCore/include/Acts/TrackFitting/GlobalChiSquareFitter.hppCore/include/Acts/TrackFitting/KalmanFitter.hppCore/include/Acts/TrackFitting/detail/GsfActor.hppCore/include/Acts/Utilities/AlgebraHelpers.hppCore/include/Acts/Utilities/Frustum.ippCore/include/Acts/Utilities/TrackHelpers.hppCore/include/Acts/Utilities/TypeDispatcher.hppCore/include/Acts/Utilities/UnitVectors.hppCore/include/Acts/Utilities/VectorHelpers.hppCore/src/EventData/CorrectedTransformationFreeToBound.cppCore/src/Geometry/ConeVolumeBounds.cppCore/src/Geometry/CuboidVolumeStack.cppCore/src/Geometry/CutoutCylinderVolumeBounds.cppCore/src/Geometry/CylinderPortalShell.cppCore/src/Geometry/CylinderVolumeBuilder.cppCore/src/Geometry/CylinderVolumeHelper.cppCore/src/Geometry/CylinderVolumeStack.cppCore/src/Geometry/DiscLayer.cppCore/src/Geometry/GlueVolumesDescriptor.cppCore/src/Geometry/LayerArrayCreator.cppCore/src/Geometry/MultiWireVolumeBuilder.cppVerification
acts build acts-sonarcloud(from~/Documents/work/devwith your usualactsenvironment).acts test acts-sonarcloud(fullctestsuite 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
ifstatement).--- END COMMIT MESSAGE ---
Any further description goes here, @-mentions are ok here!
feat,fix,refactor,docs,choreandbuildtypes.