Skip to content

Commit 2f14bf5

Browse files
authored
Review casts in PCL, Part A [#4230] (#5504)
* Enabled cppcoreguidelines-pro-type-static-cast-downcast * Fixed issues flagged by cppcoreguidelines-pro-type-cstyle-cast * Fixed more issues flagged by cppcoreguidelines-pro-type-cstyle-cast * More cppcoreguidelines-*-cast escapes * Fixed more cppcoreguidelines-*-cast complaints * Fixed complaint from modernize-use-auto * Made changes per inspection * Switched more C-style casts to reinterpret_cast * Fixed clang-format issued flagged by CI * Fixed C-style cast escape * Fixed string constructor per review
1 parent 9502237 commit 2f14bf5

File tree

21 files changed

+93
-90
lines changed

21 files changed

+93
-90
lines changed

.clang-tidy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Checks: '-*,modernize-use-auto,modernize-deprecated-headers,modernize-redundant-void-arg,modernize-replace-random-shuffle,modernize-use-equals-default,modernize-use-equals-delete,modernize-use-nullptr,modernize-use-override,modernize-use-using,performance-faster-string-find,performance-for-range-copy,performance-implicit-conversion-in-loop,performance-inefficient-algorithm,performance-inefficient-vector-operation,performance-move-const-arg,performance-move-constructor-init,performance-no-automatic-move,performance-noexcept-move-constructor,performance-type-promotion-in-math-fn'
3-
WarningsAsErrors: '-*,modernize-use-auto,modernize-deprecated-headers,modernize-redundant-void-arg,modernize-replace-random-shuffle,modernize-use-equals-default,modernize-use-equals-delete,modernize-use-nullptr,modernize-use-override,modernize-use-using,performance-faster-string-find,performance-for-range-copy,performance-implicit-conversion-in-loop,performance-inefficient-algorithm,performance-inefficient-vector-operation,performance-move-const-arg,performance-move-constructor-init,performance-no-automatic-move,performance-noexcept-move-constructor,performance-type-promotion-in-math-fn'
2+
Checks: '-*,modernize-use-auto,modernize-deprecated-headers,modernize-redundant-void-arg,modernize-replace-random-shuffle,modernize-use-equals-default,modernize-use-equals-delete,modernize-use-nullptr,modernize-use-override,modernize-use-using,performance-faster-string-find,performance-for-range-copy,performance-implicit-conversion-in-loop,performance-inefficient-algorithm,performance-inefficient-vector-operation,performance-move-const-arg,performance-move-constructor-init,performance-no-automatic-move,performance-noexcept-move-constructor,performance-type-promotion-in-math-fn,cppcoreguidelines-pro-type-cstyle-cast,cppcoreguidelines-pro-type-static-cast-downcast'
3+
WarningsAsErrors: '-*,modernize-use-auto,modernize-deprecated-headers,modernize-redundant-void-arg,modernize-replace-random-shuffle,modernize-use-equals-default,modernize-use-equals-delete,modernize-use-nullptr,modernize-use-override,modernize-use-using,performance-faster-string-find,performance-for-range-copy,performance-implicit-conversion-in-loop,performance-inefficient-algorithm,performance-inefficient-vector-operation,performance-move-const-arg,performance-move-constructor-init,performance-no-automatic-move,performance-noexcept-move-constructor,performance-type-promotion-in-math-fn,cppcoreguidelines-pro-type-cstyle-cast,cppcoreguidelines-pro-type-static-cast-downcast'
44
CheckOptions:
55
- {key: modernize-use-auto.MinTypeNameLength, value: 7}

common/src/range_image_planar.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ namespace pcl
210210
std::cerr << __PRETTY_FUNCTION__<<": Given range image is not a RangeImagePlanar!\n";
211211
return;
212212
}
213-
RangeImagePlanar& ret = * (static_cast<RangeImagePlanar*> (&half_image));
213+
RangeImagePlanar& ret = * (dynamic_cast<RangeImagePlanar*> (&half_image));
214214

215215
ret.focal_length_x_ = focal_length_x_/2;
216216
ret.focal_length_x_reciprocal_ = 1.0f/ret.focal_length_x_;
@@ -233,7 +233,7 @@ namespace pcl
233233
std::cerr << __PRETTY_FUNCTION__<<": Given range image is not a RangeImagePlanar!\n";
234234
return;
235235
}
236-
RangeImagePlanar& ret = * (static_cast<RangeImagePlanar*> (&sub_image));
236+
RangeImagePlanar& ret = * (dynamic_cast<RangeImagePlanar*> (&sub_image));
237237

238238
ret.focal_length_x_ = focal_length_x_ / static_cast<float> (combine_pixels);
239239
ret.focal_length_x_reciprocal_ = 1.0f / ret.focal_length_x_;
@@ -255,7 +255,7 @@ namespace pcl
255255
std::cerr << PVARC(typeid (*this).name())<<PVARN(typeid (other).name());
256256
}
257257
assert (ERROR_GIVEN_RANGE_IMAGE_IS_NOT_A_RangeImagePlanar);
258-
*static_cast<RangeImagePlanar*> (&other) = *this;
258+
*dynamic_cast<RangeImagePlanar*> (&other) = *this;
259259
}
260260

261261
} // namespace end

io/src/ifs_io.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pcl::IFSReader::readHeader (const std::string &file_name, pcl::PCLPointCloud2 &c
7878

7979
//Read the magic
8080
std::uint32_t length_of_magic;
81-
fs.read ((char*)&length_of_magic, sizeof (std::uint32_t));
81+
fs.read (reinterpret_cast<char*>(&length_of_magic), sizeof (std::uint32_t));
8282
char *magic = new char [length_of_magic];
8383
fs.read (magic, sizeof (char) * length_of_magic);
8484
const bool file_is_ifs_file = (strcmp (magic, "IFS") == 0);
@@ -92,7 +92,7 @@ pcl::IFSReader::readHeader (const std::string &file_name, pcl::PCLPointCloud2 &c
9292

9393
//Read IFS version
9494
float version;
95-
fs.read ((char*)&version, sizeof (float));
95+
fs.read (reinterpret_cast<char*>(&version), sizeof (float));
9696
if (version == 1.0f)
9797
ifs_version = IFS_V1_0;
9898
else
@@ -107,7 +107,7 @@ pcl::IFSReader::readHeader (const std::string &file_name, pcl::PCLPointCloud2 &c
107107

108108
//Read the name
109109
std::uint32_t length_of_name;
110-
fs.read ((char*)&length_of_name, sizeof (std::uint32_t));
110+
fs.read (reinterpret_cast<char*>(&length_of_name), sizeof (std::uint32_t));
111111
char *name = new char [length_of_name];
112112
fs.read (name, sizeof (char) * length_of_name);
113113
delete[] name;
@@ -119,15 +119,15 @@ pcl::IFSReader::readHeader (const std::string &file_name, pcl::PCLPointCloud2 &c
119119
{
120120
//Read the keyword
121121
std::uint32_t length_of_keyword;
122-
fs.read ((char*)&length_of_keyword, sizeof (std::uint32_t));
122+
fs.read (reinterpret_cast<char*>(&length_of_keyword), sizeof (std::uint32_t));
123123
char *keyword = new char [length_of_keyword];
124124
fs.read (keyword, sizeof (char) * length_of_keyword);
125125

126126
const bool keyword_is_vertices = (strcmp (keyword, "VERTICES") == 0);
127127
delete[] keyword;
128128
if (keyword_is_vertices)
129129
{
130-
fs.read ((char*)&nr_points, sizeof (std::uint32_t));
130+
fs.read (reinterpret_cast<char*>(&nr_points), sizeof (std::uint32_t));
131131
if ((nr_points == 0) || (nr_points > 10000000))
132132
{
133133
PCL_ERROR ("[pcl::IFSReader::readHeader] Bad number of vertices %lu!\n", nr_points);
@@ -281,7 +281,7 @@ pcl::IFSReader::read (const std::string &file_name, pcl::PolygonMesh &mesh, int
281281
fs.seekg (data_size);
282282
// Read the TRIANGLES keyword
283283
std::uint32_t length_of_keyword;
284-
fs.read ((char*)&length_of_keyword, sizeof (std::uint32_t));
284+
fs.read (reinterpret_cast<char*>(&length_of_keyword), sizeof (std::uint32_t));
285285
char *keyword = new char [length_of_keyword];
286286
fs.read (keyword, sizeof (char) * length_of_keyword);
287287
if (strcmp (keyword, "TRIANGLES"))
@@ -293,7 +293,7 @@ pcl::IFSReader::read (const std::string &file_name, pcl::PolygonMesh &mesh, int
293293
delete[] keyword;
294294
// Read the number of facets
295295
std::uint32_t nr_facets;
296-
fs.read ((char*)&nr_facets, sizeof (std::uint32_t));
296+
fs.read (reinterpret_cast<char*>(&nr_facets), sizeof (std::uint32_t));
297297
if ((nr_facets == 0) || (nr_facets > 10000000))
298298
{
299299
PCL_ERROR ("[pcl::IFSReader::read] Bad number of facets %lu!\n", nr_facets);
@@ -307,9 +307,9 @@ pcl::IFSReader::read (const std::string &file_name, pcl::PolygonMesh &mesh, int
307307
{
308308
pcl::Vertices &facet = mesh.polygons[i];
309309
facet.vertices.resize (3);
310-
fs.read ((char*)&(facet.vertices[0]), sizeof (std::uint32_t));
311-
fs.read ((char*)&(facet.vertices[1]), sizeof (std::uint32_t));
312-
fs.read ((char*)&(facet.vertices[2]), sizeof (std::uint32_t));
310+
fs.read (reinterpret_cast<char*>(&(facet.vertices[0])), sizeof (std::uint32_t));
311+
fs.read (reinterpret_cast<char*>(&(facet.vertices[1])), sizeof (std::uint32_t));
312+
fs.read (reinterpret_cast<char*>(&(facet.vertices[2])), sizeof (std::uint32_t));
313313
}
314314
// We are done, close the file
315315
fs.close ();

io/src/image_rgb24.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pcl::io::ImageRGB24::fillGrayscale (unsigned width, unsigned height, unsigned ch
8181
unsigned dst_skip = gray_line_step - width; // skip of padding values in bytes
8282

8383
unsigned char* dst_line = gray_buffer;
84-
const RGB888Pixel* src_line = (RGB888Pixel*) wrapper_->getData ();
84+
const auto* src_line = reinterpret_cast<const RGB888Pixel*>(wrapper_->getData ());
8585

8686
for (unsigned yIdx = 0; yIdx < height; ++yIdx, src_line += src_skip, dst_line += dst_skip)
8787
{

io/src/image_yuv422.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pcl::io::ImageYUV422::fillRGB (unsigned width, unsigned height, unsigned char* r
7979
THROW_IO_EXCEPTION ("Downsampling only possible for power of two scale in both dimensions. Request was %d x %d -> %d x %d.", wrapper_->getWidth (), wrapper_->getHeight (), width, height);
8080
}
8181

82-
const std::uint8_t* yuv_buffer = (std::uint8_t*) wrapper_->getData ();
82+
const auto* yuv_buffer = reinterpret_cast<const std::uint8_t*>(wrapper_->getData ());
8383

8484
unsigned rgb_line_skip = 0;
8585
if (rgb_line_step != 0)
@@ -143,7 +143,7 @@ pcl::io::ImageYUV422::fillGrayscale (unsigned width, unsigned height, unsigned c
143143
unsigned yuv_step = wrapper_->getWidth () / width;
144144
unsigned yuv_x_step = yuv_step << 1;
145145
unsigned yuv_skip = (wrapper_->getHeight () / height - 1) * ( wrapper_->getWidth () << 1 );
146-
const std::uint8_t* yuv_buffer = ( (std::uint8_t*) wrapper_->getData () + 1);
146+
const std::uint8_t* yuv_buffer = ( reinterpret_cast<const std::uint8_t*>(wrapper_->getData ()) + 1);
147147

148148
for (unsigned yIdx = 0; yIdx < wrapper_->getHeight (); yIdx += yuv_step, yuv_buffer += yuv_skip, gray_buffer += gray_line_skip)
149149
{

io/src/robot_eye_grabber.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <pcl/common/point_tests.h> // for pcl::isFinite
4040
#include <pcl/console/print.h>
4141

42+
#include <string>
43+
4244
/////////////////////////////////////////////////////////////////////////////
4345
pcl::RobotEyeGrabber::RobotEyeGrabber ()
4446
: terminate_thread_ (false)
@@ -176,13 +178,15 @@ pcl::RobotEyeGrabber::convertPacketData (unsigned char *data_packet, std::size_t
176178
//The new packet data format contains this as a header
177179
//char[6] "EBRBEP"
178180
//std::uint32_t Timestamp // counts of a 66 MHz clock since power-on of eye.
179-
std::size_t response_size = 6; //"EBRBEP"
180-
if( !strncmp((char*)(data_packet), "EBRBEP", response_size) )
181+
const std::string PACKET_HEADER("EBRBEP");
182+
constexpr std::size_t RESPONSE_SIZE = 6; //"EBRBEP"
183+
std::string packet(reinterpret_cast<const char*>(data_packet), RESPONSE_SIZE);
184+
if(packet == PACKET_HEADER)
181185
{
182186
std::uint32_t timestamp; // counts of a 66 MHz clock since power-on of eye.
183-
computeTimestamp(timestamp, data_packet + response_size);
187+
computeTimestamp(timestamp, data_packet + RESPONSE_SIZE);
184188
//std::cout << "Timestamp: " << timestamp << std::endl;
185-
offset = (response_size + sizeof(timestamp));
189+
offset = (RESPONSE_SIZE + sizeof(timestamp));
186190
}
187191
else
188192
{

ml/src/svm.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class Kernel : public pcl::QMatrix {
370370
double
371371
kernel_precomputed(int i, int j) const
372372
{
373-
return x[i][int(x[j][0].value)].value;
373+
return x[i][static_cast<int>(x[j][0].value)].value;
374374
}
375375
};
376376

@@ -493,7 +493,7 @@ Kernel::k_function(const svm_node* x, const svm_node* y, const svm_parameter& pa
493493
return std::tanh(param.gamma * dot(x, y) + param.coef0);
494494

495495
case PRECOMPUTED: // x: test (validation), y: SV
496-
return x[int(y->value)].value;
496+
return x[static_cast<int>(y->value)].value;
497497

498498
default:
499499
return 0; // Unreachable
@@ -1461,7 +1461,7 @@ class SVC_Q : public Kernel {
14611461

14621462
if ((start = cache->get_data(i, &data, len)) < len) {
14631463
for (int j = start; j < len; j++)
1464-
data[j] = Qfloat(y[i] * y[j] * (this->*kernel_function)(i, j));
1464+
data[j] = static_cast<Qfloat>(y[i] * y[j] * (this->*kernel_function)(i, j));
14651465
}
14661466

14671467
return data;
@@ -1516,7 +1516,7 @@ class ONE_CLASS_Q : public Kernel {
15161516

15171517
if ((start = cache->get_data(i, &data, len)) < len) {
15181518
for (int j = start; j < len; j++)
1519-
data[j] = Qfloat((this->*kernel_function)(i, j));
1519+
data[j] = static_cast<Qfloat>((this->*kernel_function)(i, j));
15201520
}
15211521

15221522
return data;
@@ -1590,7 +1590,7 @@ class SVR_Q : public Kernel {
15901590

15911591
if (cache->get_data(real_i, &data, l) < l) {
15921592
for (j = 0; j < l; j++)
1593-
data[j] = Qfloat((this->*kernel_function)(real_i, j));
1593+
data[j] = static_cast<Qfloat>((this->*kernel_function)(real_i, j));
15941594
}
15951595

15961596
// reorder and copy
@@ -1601,7 +1601,7 @@ class SVR_Q : public Kernel {
16011601
schar si = sign[i];
16021602

16031603
for (j = 0; j < len; j++)
1604-
buf[j] = Qfloat(si) * Qfloat(sign[j]) * data[index[j]];
1604+
buf[j] = static_cast<Qfloat>(si) * static_cast<Qfloat>(sign[j]) * data[index[j]];
16051605

16061606
return buf;
16071607
}
@@ -1765,7 +1765,7 @@ solve_one_class(const svm_problem* prob,
17651765
double* zeros = new double[l];
17661766
schar* ones = new schar[l];
17671767

1768-
int n = int(param->nu * prob->l); // # of alpha's at upper bound
1768+
int n = static_cast<int>(param->nu * prob->l); // # of alpha's at upper bound
17691769

17701770
for (int i = 0; i < n; i++)
17711771
alpha[i] = 1;
@@ -2340,7 +2340,7 @@ svm_group_classes(const svm_problem* prob,
23402340
int* data_label = Malloc(int, l);
23412341

23422342
for (int i = 0; i < l; i++) {
2343-
int this_label = int(prob->y[i]);
2343+
int this_label = static_cast<int>(prob->y[i]);
23442344
int j;
23452345

23462346
for (j = 0; j < nr_class; j++) {
@@ -3097,7 +3097,7 @@ svm_save_model(const char* model_file_name, const svm_model* model)
30973097
const svm_node* p = SV[i];
30983098

30993099
if (param.kernel_type == PRECOMPUTED)
3100-
fprintf(fp, "0:%d ", int(p->value));
3100+
fprintf(fp, "0:%d ", static_cast<int>(p->value));
31013101
else
31023102
while (p->index != -1) {
31033103
fprintf(fp, "%d:%.8g ", p->index, p->value);
@@ -3124,7 +3124,7 @@ readline(FILE* input)
31243124
while (strrchr(line, '\n') == nullptr) {
31253125
max_line_len *= 2;
31263126
line = static_cast<char*>(realloc(line, max_line_len));
3127-
int len = int(strlen(line));
3127+
int len = static_cast<int>(strlen(line));
31283128

31293129
if (fgets(line + len, max_line_len - len, input) == nullptr)
31303130
break;
@@ -3374,7 +3374,7 @@ svm_load_model(const char* model_file_name)
33743374
if (val == nullptr)
33753375
break;
33763376

3377-
x_space[j].index = int(strtol(idx, nullptr, 10));
3377+
x_space[j].index = static_cast<int>(strtol(idx, nullptr, 10));
33783378

33793379
x_space[j].value = strtod(val, nullptr);
33803380

@@ -3521,7 +3521,7 @@ svm_check_parameter(const svm_problem* prob, const svm_parameter* param)
35213521
int* count = Malloc(int, max_nr_class);
35223522

35233523
for (int i = 0; i < l; i++) {
3524-
int this_label = int(prob->y[i]);
3524+
int this_label = static_cast<int>(prob->y[i]);
35253525
int j;
35263526

35273527
for (j = 0; j < nr_class; j++)

ml/src/svm_wrapper.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ pcl::SVMTrain::trainClassifier()
253253
doCrossValidation();
254254
}
255255
else {
256-
SVMModel* out;
257-
out = static_cast<SVMModel*>(svm_train(&prob_, &param_));
256+
auto* out = reinterpret_cast<SVMModel*>(svm_train(&prob_, &param_));
258257
if (out == nullptr) {
259258
PCL_ERROR("[pcl::%s::trainClassifier] Error taining the classifier model.\n",
260259
getClassName().c_str());
@@ -499,8 +498,7 @@ pcl::SVM::saveProblemNorm(const char* filename,
499498
bool
500499
pcl::SVMClassify::loadClassifierModel(const char* filename)
501500
{
502-
SVMModel* out;
503-
out = static_cast<SVMModel*>(svm_load_model(filename));
501+
auto* out = reinterpret_cast<SVMModel*>(svm_load_model(filename));
504502
if (out == nullptr) {
505503
PCL_ERROR("[pcl::%s::loadClassifierModel] Can't open classifier model %s.\n",
506504
getClassName().c_str(),

outofcore/tools/outofcore_viewer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class KeyboardCallback : public vtkCommand
159159
std::cout << "Key Pressed: " << key << std::endl;
160160

161161
Scene *scene = Scene::instance ();
162-
OutofcoreCloud *cloud = static_cast<OutofcoreCloud*> (scene->getObjectByName ("my_octree"));
162+
OutofcoreCloud *cloud = dynamic_cast<OutofcoreCloud*> (scene->getObjectByName ("my_octree"));
163163

164164
if (key == "Up" || key == "Down")
165165
{

people/apps/main_ground_based_people_detection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ struct callback_args{
101101
void
102102
pp_callback (const pcl::visualization::PointPickingEvent& event, void* args)
103103
{
104-
auto* data = (struct callback_args *)args;
104+
auto* data = reinterpret_cast<struct callback_args *>(args);
105105
if (event.getPointIndex () == -1)
106106
return;
107107
PointT current_point;

0 commit comments

Comments
 (0)