Issue 237769
Summary [clang-tidy] readability-redundant-parentheses changes member addresses into pointers-to-members
Labels clang-tidy
Assignees
Reporter and2049
    readability-redundant-parentheses` diagnoses the parentheses in `&(S::value)` as redundant. Applying the fix changes the _expression_ from the address of the current object's member (`int *`) to a pointer-to-member (`int S::*`), producing invalid code.

Reproducer:

```cpp
struct S {
  int value;

  int *get() { return &(S::value); }
};
```

```sh
clang-tidy --checks='-*,readability-redundant-parentheses' test.cpp -- -std=c++17
```

```text
test.cpp:4:24: warning: redundant parentheses around _expression_ [readability-redundant-parentheses]
    4 |   int *get() { return &(S::value); }
      |                        ^ ~
```

Applying the fix with `--fix` changes the function to:

```cpp
 int *get() { return &S::value; }
```

The original code compiles, but the fixed version fails:

```text
error: cannot initialize return object of type 'int *' with an rvalue of type 'int S::*'
```

The parentheses prevent `&S::value` from forming a pointer-to-member. The check should preserve them when removing them would change the meaning of the address-of _expression_.

Reproduced on main at `70130f930fd3` (LLVM 24.0.0git).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to