Skip to content

[clang-tidy] false-negative in bugprone-return-const-ref-from-parameter when overload for rvalues exists with different constness #160650

@xuhdev

Description

@xuhdev

This is a followup bug of #90274.

The following code snippet should generate bugprone-return-const-ref-from-parameter error but clang-tidy doesn't:

#include <string>

struct Foo {
  const std::string &f(const std::string &a) const {
    return a;
  }
  void f(std::string&&) = delete;
};

The following bug-prone usage compiles:

int main() {
  const Foo foo;
  auto& a = foo.f(std::string("12"));
  // use a...
  return 0;
}

The proper way to avoid this clang-tidy warning is to overload with the same constness:

#include <string>

struct Foo {
  const std::string &f(const std::string &a) const {
    return a;
  }
  void f(std::string&&) const = delete;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions