llvm / llvm/llvm-project

False positive -Wshadow-field-in-constructor-modified on reference parameter

Open
#162,494 1 comment 0 reactions 0 assignees View on GitHub
clang:diagnostics false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Compile this code with `-Wshadow`:
```c++
struct S {
int &x;
S(int &x) : x(x) { ++x; }
};
```
We get the warning:
```
:3:24: warning: modifying constructor parameter 'x' that shadows a field of 'S' [-Wshadow-field-in-constructor-modified]
3 | S(int &x) : x(x) { ++x; }
| ^
:3:12: note: variable 'x' is declared here
3 | S(int &x) : x(x) { ++x; }
| ^
:2:10: note: previous declaration is here
2 | int &x;
| ^
```
But we're not actually modifying the parameter: the parameter is a reference (and binds to the same object as the member).

However, it's not clear if we should suppress the warning if the parameter is a reference, or only if both parameter and member are references. If the member is not a reference, the intention might have been to modify the member instead of the object bound by the parameter reference.

Contributor guide

Open the contributing guide

Research direction

Start by compiling the C++ reproducer with -Wshadow and inspect how -Wshadow-field-in-constructor-modified classifies reference parameters. Decide whether suppression depends on the parameter, the member, or both being references, then add regression coverage for the chosen behavior and verify the warning output.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.