llvm / llvm/llvm-project

[clang-tidy] readability-redundant-parentheses deletes reference NTTP uses with no parentheses

Open
#224,705 1 comment 0 reactions 0 assignees View on GitHub
clang-tidy false-positive invalid-code-generation
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

`readability-redundant-parentheses` diagnoses a use of a reference non-type template parameter as redundant parentheses, even when no parentheses are written. Applying the fix-it deletes the parameter name and produces invalid code.

Reproducer:

```c++
int value = 0;

template
int &get() {
return R;
}

int &use() {
return get();
}
```

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

```text
test.cpp:5:10: warning: redundant parentheses around expression [readability-redundant-parentheses]
5 | return R;
| ^
```

Applying the fix with `--fix` changes the return statement to:

```c++
return ;
```

The original program compiles, but the fixed version fails because `get` must return a value.

In the instantiated AST, the reference template argument is represented by a `SubstNonTypeTemplateParmExpr` containing a synthetic `ParenExpr`. Both parenthesis locations point to the `R` token. The check matches this synthetic node and removes the token as though it were a written parenthesis.

The check should avoid diagnosing or removing synthetic parentheses that do not correspond to parentheses in the source.

Reproduced on LLVM 24.0.0git.

Contributor guide

Open the contributing guide

Research direction

Start with the clang-tidy readability-redundant-parentheses check and reproduce the issue using the shown test.cpp and clang-tidy command. Trace how the instantiated AST represents the reference non-type template parameter, then add coverage showing that synthetic parentheses are not diagnosed or removed and that the original code remains valid after --fix.

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
Mostly clear
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.