llvm / llvm/llvm-project

[clang-tidy] False positive `readability-make-member-function-const` for `union`

Open
#174,269 3 comments 0 reactions 1 assignee Claimed by @arthur3336 View on GitHub
clang-tidy false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/3dYM7vqo1
```cpp
struct S {
union { int* resource; };
int& get() { return *resource; }
const int& get() const { return *resource; }
};
```
```
:3:10: warning: method 'get' can be made const [readability-make-member-function-const]
3 | int& get() { return *resource; }
| ^
| const
1 warning generated.
```

Moving `resource` out of `union` makes the diagnostic go away, which appears to be the intended behavior. There are many types that store pointers or references and their `get()` or `operator*` has a non-const overload that could theoretically be `const` because `const` does not propagate into pointers/references.

On top of `union` preventing whatever would have suppressed the diagnostic otherwise, it also seems nonsensical to tell the user they could make a function `const` when there already exists another overload that is `const`; adding `const` would just result in a compiler error.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.