llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[-Wunsafe-buffer-usage] Extra size arg in fixit for std::span initialization with constant size array

Open jkorous-apple opened this issue 2 years ago • 1 comments

Currently the fixit that transforms a local pointer initialized with constant size array uses 2-parameter std::span constructor to which it passes the array and it's size.

https://github.com/llvm/llvm-project/blob/main/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-local-var-span.cpp#L52

void local_variable_qualifiers_specifiers() {
  int a[10];
...
  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:18}:"std::span<int const> p"
  // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:19-[[@LINE-2]]:19}:"{"
  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:", 10}"

Idiomatic fixit should not repeat the size and instead rely on single parameter std::span constructor that take const size array. No. 4 here: https://en.cppreference.com/w/cpp/container/span/span

Example:

void foo() {
    int foo[5];
    std::span<int> sp = foo;
}

jkorous-apple avatar Feb 07 '24 02:02 jkorous-apple

IMO we should just bail and not touch the initializer for this if

https://github.com/llvm/llvm-project/blob/main/clang/lib/Analysis/UnsafeBufferUsage.cpp#L2099

(in FixVarInitializerWithSpan)

jkorous-apple avatar Feb 07 '24 21:02 jkorous-apple