llvm / llvm/llvm-project

[clang-format] class's operator& is mistaken for a reference

Open
#219,251 4 comments 0 reactions 0 assignees View on GitHub
clang-format confirmed
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Reproduce with this code:

```cpp
class Foo {
public:
Foo operator&(const Foo& other) { return *this; }
};

int main(int argc, char **argv) {
// class operator& is moved (wrong)
Foo foo;
foo & Foo();

// POD operator& is not moved (correct)
int bar;
bar & 2;

return 0;
}
```

and this `.clang-format`:

```
ReferenceAlignment: Left
```

with this version:

```bash
$ ~/repos/llvm-project/build/bin/clang-format --version
clang-format version 24.0.0git (https://github.com/llvm/llvm-project.git 4cca4a5721c9c48818639783770857f332e455c6)
```

Diff after formatting below. The class's `&` operator is moved according to the `ReferenceAlignment` rule, even though it's not a reference. This does not happen with the integer's `&`.

```diff
< foo & Foo();
> foo& Foo();
```

Contributor guide

Open the contributing guide

Research direction

Start by running the provided C++ reproducer through the clang-format binary with ReferenceAlignment: Left and compare the overloaded operator& with the integer case. Trace the clang-format handling of operator& and add regression coverage for the class operator while preserving the existing integer behavior. Done means the class expression is no longer reformatted as a reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.