Skip to content

Commit

Permalink
STYLE: Prefer explicit const designation
Browse files Browse the repository at this point in the history
Implements detection of local variables which could be declared as const but
are not. Declaring variables as const is recommended by many coding
guidelines, such as: ES.25 from the C++ Core Guidelines.

cd ${BLDDIR}
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,misc-const-correctness -header-filter=.* -fix
  • Loading branch information
hjmjohnson authored and dzenanz committed Dec 27, 2024
1 parent a76ce8f commit ab16e66
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
8 changes: 4 additions & 4 deletions Modules/Core/Common/include/itkDerivativeOperator.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ DerivativeOperator<TPixel, VDimension, TAllocator>::GenerateCoefficients() -> Co
unsigned int j = 1;
for (; j < w - 1; ++j)
{
PixelRealType next = coeff[j - 1] + coeff[j + 1] - 2 * coeff[j];
const PixelRealType next = coeff[j - 1] + coeff[j + 1] - 2 * coeff[j];
coeff[j - 1] = previous;
previous = next;
}
PixelRealType next = coeff[j - 1] - 2 * coeff[j];
const PixelRealType next = coeff[j - 1] - 2 * coeff[j];
coeff[j - 1] = previous;
coeff[j] = next;
}
Expand All @@ -50,11 +50,11 @@ DerivativeOperator<TPixel, VDimension, TAllocator>::GenerateCoefficients() -> Co
unsigned int j = 1;
for (; j < w - 1; ++j)
{
PixelRealType next = -0.5 * coeff[j - 1] + 0.5 * coeff[j + 1];
const PixelRealType next = -0.5 * coeff[j - 1] + 0.5 * coeff[j + 1];
coeff[j - 1] = previous;
previous = next;
}
PixelRealType next = -0.5 * coeff[j - 1];
const PixelRealType next = -0.5 * coeff[j - 1];
coeff[j - 1] = previous;
coeff[j] = next;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ FloodFilledSpatialFunctionConditionalConstIterator<TImage, TFunction>::IsPixelIn
IndexType tempIndex;
for (unsigned int i = 0; i < dim; ++i)
{
unsigned int counterCopy = counter;
const unsigned int counterCopy = counter;
tempIndex[i] = index[i] + static_cast<int>((counterCopy >> i) & 0x0001);
}

Expand Down Expand Up @@ -164,7 +164,7 @@ FloodFilledSpatialFunctionConditionalConstIterator<TImage, TFunction>::IsPixelIn
IndexType tempIndex;
for (unsigned int i = 0; i < dim; ++i)
{
unsigned int counterCopy = counter;
const unsigned int counterCopy = counter;
tempIndex[i] = index[i] + static_cast<int>((counterCopy >> i) & 0x0001);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ itkArchetypeSeriesFileNamesTest(int argc, char * argv[])
fit->SetArchetype(archetype);
ITK_TEST_SET_GET_VALUE(archetype, fit->GetArchetype());

std::vector<std::string> names = fit->GetFileNames();
const std::vector<std::string> names = fit->GetFileNames();

std::cout << "List of returned filenames: " << std::endl;
for (auto & name : names)
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/Meta/src/itkMetaImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ MetaImageIO::WriteImageInformation()

// Save out the metadatadictionary key/value pairs as part of
// the metaio header.
std::vector<std::string> keys = metaDict.GetKeys();
const std::vector<std::string> keys = metaDict.GetKeys();
for (auto & key : keys)
{
if (key == ITK_ExperimentDate || key == ITK_VoxelUnits)
Expand Down
4 changes: 2 additions & 2 deletions Modules/Numerics/Statistics/include/itkHistogram.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ Histogram<TMeasurement, TFrequencyContainer>::GetIndex(const MeasurementVectorTy

for (unsigned int dim = 0; dim < measurementVectorSize; ++dim)
{
MeasurementType tempMeasurement = measurement[dim];
IndexValueType begin = 0;
const MeasurementType tempMeasurement = measurement[dim];
IndexValueType begin = 0;
if (tempMeasurement < m_Min[dim][begin])
{
// one of measurement is below the minimum
Expand Down
31 changes: 16 additions & 15 deletions Modules/Numerics/Statistics/include/itkStatisticsAlgorithm.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -558,19 +558,19 @@ DownHeap(TSubsample * sample, unsigned int activeDimension, int beginIndex, int
while (true)
{
// location of first child
int largerChild = beginIndex + 2 * (currentNode - beginIndex) + 1;
int leftChild = largerChild;
int rightChild = leftChild + 1;
int largerChild = beginIndex + 2 * (currentNode - beginIndex) + 1;
const int leftChild = largerChild;
const int rightChild = leftChild + 1;
if (leftChild > endIndex - 1)
{
// leaf node
return;
}

const auto initValue = sample->GetMeasurementVectorByIndex(leftChild)[activeDimension];
SampleMeasurementType leftChildValue = initValue;
SampleMeasurementType rightChildValue = initValue;
SampleMeasurementType largerChildValue = initValue;
const auto initValue = sample->GetMeasurementVectorByIndex(leftChild)[activeDimension];
const SampleMeasurementType leftChildValue = initValue;
SampleMeasurementType rightChildValue = initValue;
SampleMeasurementType largerChildValue = initValue;

if (rightChild < endIndex)
{
Expand Down Expand Up @@ -633,14 +633,15 @@ IntrospectiveSortLoop(TSubsample * sample,
}

--depthLimit;
int cut = Partition<TSubsample>(sample,
activeDimension,
beginIndex,
endIndex,
MedianOfThree<SampleMeasurementType>(
sample->GetMeasurementVectorByIndex(beginIndex)[activeDimension],
sample->GetMeasurementVectorByIndex(beginIndex + length / 2)[activeDimension],
sample->GetMeasurementVectorByIndex(endIndex - 1)[activeDimension]));
const int cut =
Partition<TSubsample>(sample,
activeDimension,
beginIndex,
endIndex,
MedianOfThree<SampleMeasurementType>(
sample->GetMeasurementVectorByIndex(beginIndex)[activeDimension],
sample->GetMeasurementVectorByIndex(beginIndex + length / 2)[activeDimension],
sample->GetMeasurementVectorByIndex(endIndex - 1)[activeDimension]));
IntrospectiveSortLoop<TSubsample>(sample, activeDimension, cut, endIndex, depthLimit, sizeThreshold);
endIndex = cut;
length = endIndex - beginIndex;
Expand Down

0 comments on commit ab16e66

Please sign in to comment.