Skip to content

clang-tidy: false positive readability-convert-member-functions-to-static to overloaded method #149152

@e-kwsm

Description

@e-kwsm

Prepare /tmp/a.cpp:

struct S {
  void f();
  void f() const;

  int i = 0;
};

void S::f() { this->i++; }
void S::f() const { ; }

and /tmp/compile_commands.json:

[{
  "directory": "/tmp",
  "command": "clang++ -c -o a.o a.cpp",
  "file": "a.cpp",
  "output": "a.o"
}]
$ clang-tidy --version
LLVM (http://llvm.org/):
  LLVM version 20.1.7
  Optimized build.
$ clang-tidy --checks=readability-convert-member-functions-to-static --fix a.cpp
1 warning generated.
a.cpp:9:9: warning: method 'f' can be made static [readability-convert-member-functions-to-static]
    3 |   void f() const;
      |            ~~~~~
      |   static
    4 |
    5 |   int i = 0;
    6 | };
    7 |
    8 | void S::f() { this->i++; }
    9 | void S::f() const { ; }
      |         ^   ~~~~~
a.cpp:3:3: note: FIX-IT applied suggested code changes
    3 |   void f() const;
      |   ^
a.cpp:3:12: note: FIX-IT applied suggested code changes
    3 |   void f() const;
      |            ^
a.cpp:9:13: note: FIX-IT applied suggested code changes
    9 | void S::f() const { ; }
      |             ^
clang-tidy applied 3 of 3 suggested fixes.

gives the following invalid code

struct S {
  void f();
  static void f() ;

  int i = 0;
};

void S::f() { this->i++; }
void S::f() { ; }
$ clang++ -c -o a.o a.cpp
a.cpp:3:15: error: static and non-static member functions with the same parameter types cannot be overloaded
    3 |   static void f() ;
      |               ^
a.cpp:2:8: note: previous declaration is here
    2 |   void f();
      |        ^
a.cpp:9:9: error: redefinition of 'f'
    9 | void S::f() { ; }
      |         ^
a.cpp:8:9: note: previous definition is here
    8 | void S::f() { this->i++; }
      |         ^
2 errors generated.

https://godbolt.org/z/aaYq98GMT

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