STYLE: Replace declare-then-assign with const auto for FixedArray-based types#6014
Conversation
|
| Filename | Overview |
|---|---|
| Modules/Core/Common/test/itkAnnulusOperatorGTest.cxx | Replaces two-line declare-then-assign with const auto for AnnulusOperator size type |
| Modules/Core/Common/test/itkBSplineInterpolationWeightFunctionTest.cxx | Applies const auto pattern to BSpline interpolation weight function test declarations |
| Modules/Core/Common/test/itkImageRandomNonRepeatingIteratorWithIndexGTest.cxx | Applies const auto pattern to iterator index variable in GTest |
| Modules/Core/Common/test/itkVersorTest.cxx | Applies const auto pattern to Versor test FixedArray-based declarations |
| Modules/Core/ImageFunction/test/itkBSplineInterpolateImageFunctionTest.cxx | Applies const auto pattern to BSpline image interpolation function test |
| Modules/Core/Transform/test/itkBSplineDeformableTransformTest2.cxx | Applies const auto pattern to BSpline deformable transform test 2 |
| Modules/Core/Transform/test/itkBSplineDeformableTransformTest3.cxx | Applies const auto pattern to BSpline deformable transform test 3 |
| Modules/Core/Transform/test/itkBSplineTransformInitializerTest1.cxx | Applies const auto pattern to BSpline transform initializer test |
| Modules/Core/Transform/test/itkBSplineTransformTest2.cxx | Applies const auto pattern to BSpline transform test 2 |
| Modules/Core/Transform/test/itkBSplineTransformTest3.cxx | Applies const auto pattern to BSpline transform test 3 |
| Modules/Core/Transform/test/itkComposeScaleSkewVersor3DTransformTest.cxx | Applies const auto pattern to ComposeScaleSkewVersor3D transform test |
| Modules/Core/Transform/test/itkEuler2DTransformTest.cxx | Applies const auto pattern to Euler2D transform test |
| Modules/Core/Transform/test/itkQuaternionRigidTransformTest.cxx | Applies const auto pattern to quaternion rigid transform test |
| Modules/Core/Transform/test/itkRigid2DTransformTest.cxx | Applies const auto pattern to Rigid2D transform test |
| Modules/Core/Transform/test/itkRigid3DTransformTest.cxx | Applies const auto pattern to Rigid3D transform test |
| Modules/Core/Transform/test/itkScaleSkewVersor3DTransformTest.cxx | Applies const auto pattern to ScaleSkewVersor3D transform test |
| Modules/Core/Transform/test/itkScaleVersor3DTransformTest.cxx | Applies const auto pattern to ScaleVersor3D transform test |
| Modules/Core/Transform/test/itkSimilarity2DTransformTest.cxx | Applies const auto pattern to Similarity2D transform test |
| Modules/Core/Transform/test/itkSimilarity3DTransformTest.cxx | Applies const auto pattern to Similarity3D transform test |
| Modules/Core/Transform/test/itkVersorRigid3DTransformTest.cxx | Applies const auto pattern to VersorRigid3D transform test |
| Modules/Core/Transform/test/itkVersorTransformTest.cxx | Applies const auto pattern to Versor transform test |
| Modules/Filtering/LabelMap/test/itkLabelMapTest.cxx | Applies const auto pattern to label map test declarations |
| Modules/IO/DCMTK/test/itkDCMTKImageIONoPreambleTest.cxx | Applies const auto pattern to DCMTK IO no-preamble test |
| Modules/IO/GDCM/test/itkGDCMImagePositionPatientTest.cxx | Applies const auto pattern to GDCM image position patient test |
| Modules/Numerics/Statistics/test/itkGaussianRandomSpatialNeighborSubsamplerTest.cxx | Removes unused IndexType alias; uses const auto for index variable |
| Modules/Numerics/Statistics/test/itkImageToHistogramFilterTest.cxx | Applies const auto pattern to image-to-histogram filter test |
| Modules/Numerics/Statistics/test/itkUniformRandomSpatialNeighborSubsamplerTest.cxx | Removes unused IndexType alias; uses const auto for index variable |
| Modules/Registration/Metricsv4/test/itkEuclideanDistancePointSetMetricTest2.cxx | Applies const auto pattern to Euclidean distance point-set metric test |
| Modules/Registration/PDEDeformable/test/itkDemonsRegistrationFilterTest.cxx | Applies const auto pattern to Demons registration filter test |
| Modules/Registration/PDEDeformable/test/itkDiffeomorphicDemonsRegistrationFilterTest.cxx | Applies const auto pattern to Diffeomorphic Demons registration test |
| Modules/Registration/PDEDeformable/test/itkFastSymmetricForcesDemonsRegistrationFilterTest.cxx | Applies const auto pattern to Fast Symmetric Forces Demons test |
| Modules/Registration/PDEDeformable/test/itkLevelSetMotionRegistrationFilterTest.cxx | Applies const auto pattern to level-set motion registration test |
| Modules/Registration/PDEDeformable/test/itkSymmetricForcesDemonsRegistrationFilterTest.cxx | Applies const auto pattern to Symmetric Forces Demons registration test |
| Modules/Segmentation/LevelSets/test/itkCollidingFrontsImageFilterTest.cxx | Applies const auto pattern to colliding fronts image filter test |
| Modules/Segmentation/LevelSets/test/itkShapePriorMAPCostFunctionTest.cxx | Applies const auto pattern to ShapePriorMAP cost function test |
| Modules/Segmentation/LevelSets/test/itkShapePriorSegmentationLevelSetFunctionTest.cxx | Applies const auto pattern to shape prior segmentation level-set test |
| Modules/Segmentation/LevelSetsv4/test/itkMultiLevelSetChanAndVeseInternalTermTest.cxx | Applies const auto pattern to multi-level-set ChanAndVese internal term test |
| Modules/Segmentation/LevelSetsv4/test/itkMultiLevelSetDenseImageTest.cxx | Applies const auto pattern to multi-level-set dense image test |
| Modules/Segmentation/LevelSetsv4/test/itkMultiLevelSetEvolutionTest.cxx | Applies const auto pattern to multi-level-set evolution test |
| Modules/Segmentation/LevelSetsv4/test/itkTwoLevelSetMalcolmImage2DTest.cxx | Applies const auto pattern to two-level-set Malcolm 2D test |
| Modules/Segmentation/LevelSetsv4/test/itkTwoLevelSetShiImage2DTest.cxx | Applies const auto pattern to two-level-set Shi 2D test |
| Modules/Segmentation/LevelSetsv4/test/itkTwoLevelSetWhitakerImage2DTest.cxx | Applies const auto pattern to two-level-set Whitaker 2D test |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["Two-line declare-then-assign pattern
(e.g. SizeType sz; sz = img->GetSize())"] --> B{"Variable mutated
after assignment?
(e.g. sz[0] += 2)"}
B -- Yes --> C["Left unchanged"]
B -- No --> D["Replace with const auto
const auto sz = img->GetSize()"]
D --> E["Remove now-unused
type aliases
(using IndexType = ...)"]
Reviews (2): Last reviewed commit: "STYLE: Use explicit VectorType in itkVer..." | Re-trigger Greptile
|
Local build + test verification on
All 4 locally-verifiable changes compile cleanly, produce no new warnings, and pass their ctest targets. |
|
Added one more conversion and verified:
Why this case was missed in the earlier scan: the previous regex-based scanner looked only at the immediately following non-blank line, which was an unrelated Local verification:
|
|
Added 7 more scope-shrink conversions discovered by a codebase-wide clang-query scan. Each was individually verified via whole-function grep to confirm the variable has no references outside its target loop body. Commit: Files changed (7):
Build + test verification:
Discovery method: Hybrid clang-query + Python scanner. The clang-query AST matcher False positives rejected by the whole-function-scope check (7 candidates): iterator and path tests where a single |
Wave 2 — 17 additional conversions (32/32 tests pass)Added commit New conversionsSub-case A —
Sub-case B — explicit type merge (mutable after init):
Build verification: 5 drivers rebuilt (ITKCommonGTestDriver, ITKImageFunctionTestDriver, ITKPDEDeformableRegistrationTestDriver, ITKLevelSetsTestDriver, ITKLevelSetsv4TestDriver), 0 warnings, 0 errors, 32/32 ctest targets passed. PR summary (4 commits, 30 files total)
|
f6a9bdd to
c1f1cf8
Compare
c1f1cf8 to
a190f69
Compare
Replace `const auto xb = xa * sinangle` with `const VectorType xb` so the type is immediately clear at the point of use, per N-Dekker's review comment on PR InsightSoftwareConsortium#6014.
Modules/Core/ImageFunction/test/itkBSplineInterpolateImageFunctionTest.cxx
Outdated
Show resolved
Hide resolved
|
Addressed in commit
For all method return types ( |
Merge the two-line pattern:
Type var;
var = expr;
into the single-line form:
const Type var = expr;
across 49 files in Modules/ and Examples/. Variables that are never
modified after initialization are marked const. Explicit types are
used throughout -- auto is NOT used unless the type is blatantly
obvious from the RHS (cast or T::New()).
This follows the clang-tidy modernize-use-auto criteria (iterators,
new, casts only) rather than AAA ("auto almost always"), per review
feedback from N-Dekker and dzenanz.
Scope-shrink: where a variable was declared at function scope but
only used inside a loop body, the declaration is moved into the
loop to reduce its scope.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
b781291 to
c79c308
Compare
N-Dekker
left a comment
There was a problem hiding this comment.
Looks fine to me! Thank you for addressing my comments!
For what it's worth, No problem, of course, the term "FixedArray-based" is not in the proposed commit 👍 |
Summary
Replaces the two-line declare-then-assign pattern with a single
const autodeclaration for local variables of FixedArray-based types (SizeType,IndexType,PointType,VectorType) where the variable is not modified after initialization.Before:
```cpp
InputImageType::SizeType extentSize;
extentSize = reader->GetOutput()->GetLargestPossibleRegion().GetSize();
```
After:
```cpp
const auto extentSize = reader->GetOutput()->GetLargestPossibleRegion().GetSize();
```
Scope
5 conversions across 5 test files. Cases where the variable was reassigned or mutated after the initial assignment (e.g.
index[1] += 2,tempIndex -= offset) were intentionally left unchanged.Also removes two now-unused
using IndexType = ...type aliases in the Statistics subsample tests, which were only referenced by the replaced variable declarations.Motivation
Companion to #6010 (merged) which handled the
Type v; v.Fill(literal)→constexpr auto v = Type::Filled(literal)pattern. This PR addresses theType v; v = expr;→const auto v = expr;pattern for runtime-expression RHS.Unlike
::Filled()which allowsconstexpr,const autois used here since the RHS is a runtime expression.🤖 Generated with Claude Code