1

I ran into an clang-tidy warning I don't understand. I've created a minimum example:

test.cpp:

#include <array>
#include <cstddef>

extern size_t get_data(float*const* buffers, size_t count);

size_t get_data_2_a(float* buffer1, float* buffer2)
{
  const std::array<float*, 2> buffers{buffer1, buffer2};
  return get_data(buffers.data(), buffers.size());
}

size_t get_data_2_b(float* buffer1, float* buffer2)
{
  float* buffers[] = {buffer1, buffer2};
  return get_data(buffers, sizeof(buffers) / sizeof(*buffers));
}

Background info: get_data() is used to download data from a data acquisition device with multiple input channels, each input channel produces e.g. 100k data points. Using the get_data() the user can fetch data form a device with any number of channels. The get_data_2_a() function is a short cut, so the user doesn't have to create an array of pointers (which can't be done easily some other programming languages).

Running clang-tidy with:

clang-tidy -checks=readability-non-const-parameter test.cpp

clang-tidy output:

test.cpp:6:28: warning: pointer parameter 'buffer1' can be pointer to const [readability-non-const-parameter]
size_t get_data_2_a(float* buffer1, float* buffer2)
                           ^
                    const 
test.cpp:6:44: warning: pointer parameter 'buffer2' can be pointer to const [readability-non-const-parameter]
size_t get_data_2_a(float* buffer1, float* buffer2)
                                           ^
                                    const 

I've added those const's just to see what happens, clang-tidy still issues warnings:

test.cpp:6:34: warning: pointer parameter 'buffer1' can be pointer to const [readability-non-const-parameter]
size_t get_data_2_a(float* const buffer1, float* const buffer2)
                                 ^
                    const 
test.cpp:6:56: warning: pointer parameter 'buffer2' can be pointer to const [readability-non-const-parameter]
size_t get_data_2_a(float* const buffer1, float* const buffer2)
                                                       ^
                                          const 

clang-tidy version:

LLVM (http://llvm.org/):
  LLVM version 10.0.0
  
  Optimized build.
  Default target: x86_64-pc-linux-gnu
  Host CPU: znver2

I've tested it also using clang-tidy (trunk) at https://godbolt.org/z/P9xcEK

It only raises warning when using std::array, not when using a classic c-style array. What am I missing?

Reinder
  • 305
  • 1
  • 9
  • 1
    Refer to [C++ warning: “Pointer parameter ”arr“ can be pointer to const”](https://stackoverflow.com/questions/47860316/c-warning-pointer-parameter-arr-can-be-pointer-to-const) – Zeus Nov 27 '20 at 08:46
  • 1
    The declaration of `get_data()` qualifies the first argument with `const`. The definitions of `get_data_2_a()` and `get_data_2_a()` both pass (the first element) of an array of pointers to that function that can be similarly `const` qualified. Since the elements of the passed array in both cases are derived directly from the arguments `buffer1` and `buffer2`, those arguments can both be similarly `const` qualified. Clearly, clang-tidy has detected that. Different versions of lang-tidy may do implement the checks differently, hence the variation. – Peter Nov 27 '20 at 08:54
  • I've added some backgound info. – Reinder Nov 27 '20 at 10:00
  • @Peter why does clang-tidy still complain after chaning the buffer1/2 tpy to float* const ? – Reinder Nov 27 '20 at 10:01

0 Answers0