Issue 149152
Summary clang-tidy: false positive readability-convert-member-functions-to-static to overloaded method
Labels clang-tidy, false-positive
Assignees
Reporter e-kwsm
    Prepare `/tmp/a.cpp`:

```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`:

```json
[{
  "directory": "/tmp",
 "command": "clang++ -c -o a.o a.cpp",
  "file": "a.cpp",
 "output": "a.o"
}]
```

```console
$ clang-tidy --version
LLVM (http://llvm.org/):
  LLVM version 20.1.7
  Optimized build.
```

```console
$ 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

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

  int i = 0;
};

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

```console
$ 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>
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to