Skip to content

[[gsl::Pointer]] and [[clang::lifetimebound]] produce duplicate (or near-duplicate) warnings for some code locations #93386

@pkasting

Description

@pkasting

[[gsl::Pointer]] and [[clang::lifetimebound]] each catch unique lifetime bugs (demonstration of non-overlappingness: https://godbolt.org/z/4W7G5n48r). Therefore, both are desirable when applicable. However, applying both sometimes results in multiple warnings about the same location:

#include <stddef.h>
#include <array>

struct [[gsl::Pointer]] S {
  template <size_t N>
  S(const std::array<int, N>& a [[clang::lifetimebound]]) : p(a.data()) {}

  const int* p;
};

S f() {
  std::array a{1, 2, 3};
  return S(a);  // Two instances of -Wreturn-stack-address, not just one
}

auto s = S(std::array{1, 2, 3});  // Both -Wdangling-gsl and -Wdangling, not just one

Live repro: https://godbolt.org/z/K4zc3WE73

Especially in the first case, it's confusing to see the exact same error for the same location issued repeatedly. I think the compiler should issue just one warning here.

The second case is more understandable, as it's technically two distinct warnings -- but one is controlled by the other, they are firing at the exact same location, and they are effectively warning about the same issue. Ideally this would also result in just one warning.

This is not a huge deal, but it can confuse coders about whether they have multiple distinct issues to fix. More importantly in my case, it requires tweaks to nocompile tests to deal with the multiple diagnostics being emitted, which makes the tests more brittle.

Metadata

Metadata

Assignees

Labels

clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions