Skip to content

False-positive readability-convert-member-functions-to-static when using explicit object parameters #141381

@chriselrod

Description

@chriselrod
#include <print>
#include <string>

struct Hello {
  std::string str_;

  void hello(this Hello &self) { std::println("Hello, {0}!", self.str_); }
};

int main() {
  Hello hi{"world"};
  hi.hello();
  return 0;
}

Saving this file as explicit_object_param.cpp, I get

$ clang-tidy --checks=readability-convert-member-functions-to-static explicit_object_param.cpp -- --std=c++23
1 warning generated.
/home/chriselrod/Documents/progwork/cxx/misc/explicit_object_param.cpp:7:8: warning: method 'hello' can be made static [readability-convert-member-functions-to-static]
    7 |   void hello(this Hello &self) { std::println("Hello, {0}!", self.str_); }
      |        ^
      |   static 

Of course, static is illegal there

explicit_object_param.cpp:7:21: error: an explicit object parameter cannot appear in a static function
    7 |   static void hello(this Hello &self) { std::println("Hello, {0}!", self.str_); }
      |   ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
1 error generated.

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