llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.87k stars 11.93k forks source link

readability-non-const-parameter false positive with a function pointer #47799

Open gumb0 opened 3 years ago

gumb0 commented 3 years ago
Bugzilla Link 48455
Version unspecified
OS Linux
CC @chfast,@EugeneZelenko

Extended Description

For the following code:

void fn(int& n)
{
    ++n;
}

void fn2(int* n)
{
    auto p_fn = &fn;
    p_fn(*n);
}

clang-tidy generates the warning:

$ clang-tidy a.cpp -checks="readability-non-const-parameter" --
1 warning generated.
/home/andrei/dev/a.cpp:7:15: warning: pointer parameter 'n' can be pointer to const [readability-non-const-parameter]
void fn2(int* n)
              ^
         const 

It can't be const though, because it's passed to a function taking non-const reference.

https://godbolt.org/z/YGxKPG

eustas commented 5 months ago

Similar case:

static void WriteICCUint32(uint32_t value, size_t pos,
                           std::vector<uint8_t>* icc) {
  if (icc->size() < pos + 4) icc->resize(pos + 4);
  (*icc)[pos + 0] = (value >> 24u) & 255;
  (*icc)[pos + 1] = (value >> 16u) & 255;
  (*icc)[pos + 2] = (value >> 8u) & 255;
  (*icc)[pos + 3] = value & 255;
}

clang-tidy claims icc can be const...