clang-tidy: misc-misplaced-widening-cast
authorRoland Schulz <roland.schulz@intel.com>
Sun, 1 Jul 2018 05:03:16 +0000 (22:03 -0700)
committerMark Abraham <mark.j.abraham@gmail.com>
Tue, 3 Jul 2018 10:38:51 +0000 (12:38 +0200)
commit118b2fe8b598f693cf54d1afbbc6a962c8e2d632
tree9c1a468b544a2c3665305773968745210ee7b61a
parent20959862438e175ab091992725508935383cc299
clang-tidy: misc-misplaced-widening-cast

Adds gmx::index to be used as type for index calculations and
comparisons.

given:
int a,b;
size_t c;
static_cast<size_t>(a+b) < c

clang-tidy warns because the static_cast does two things:
- it widens the sum from 32 bit to 64 bit
- it sign casts from signed to unsigned

If the widening is actually required (because while a and b
fit into 32 bit the sum is bigger), then this gives the
wrong result. Thus the terms should be widened prior to summing.
But this gives the wrong result if either of them is negative,
unless the terms are first widened to signed 64 bit and the sum
is then sign converted.

Additionally sign converting the signed to unsigned is unsafe
because it gives the wrong result if it negative. Converting
the unsigned is safe for 64 bit up to 2^63 which is safe to assume
won't be reached.

For all cases clang-tidy complained about (widening after sum), the
sign conversion is changed from signed->unsigned to unsigned->
signed. Introduced gmx::index to have a type for this purpose.

None of the potential bug highlighted by clang-tidy are fixed by
this change. Instead it assumes that in all cases here the intention
was just the signed conversion and no widening prior to summing
is required.

Related #2010

Change-Id: I9251196bf6ed744317868ce9bfc34876e5cfd43b
src/gromacs/CMakeLists.txt
src/gromacs/analysisdata/modules/plot.cpp
src/gromacs/commandline/pargs.cpp
src/gromacs/domdec/domdec_specatomcomm.cpp
src/gromacs/domdec/redistribute.cpp
src/gromacs/gmxana/gmx_bar.cpp
src/gromacs/mdlib/nbnxn_grid.cpp
src/gromacs/mdlib/nbnxn_search.cpp
src/gromacs/trajectoryanalysis/runnercommon.cpp
src/gromacs/utility/basedefinitions.h