llvm / llvm/llvm-project

[clang-tidy] Improvement on 'misc-unused-parameters' check

Open
#203,909 3 comments 0 reactions 0 assignees View on GitHub
clang-tidy enhancement
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

In MISRA C++:2023, Rule 0.2.2 (A named function parameter shall be used at least once) provides an example like:

```c++
template
int f(int i) { // Non-compliant for f
if constexpr (b) {
return i;
}
return 0;
}
```

Should 'misc-unused-parameters' check detect this condition as well? Currently, [template instantiations are explicitly skipped](https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp#L185).

An extension seems straight forward. Looking at the AST, the instantiation of `f` generates a `FunctionDecl` where `IfStmt` has an empty `CompoundStmt`. This means, no `DeclRefExpr` to `i`, so `i` is unused. See https://godbolt.org/z/TfGPx6qE9.

This implies that, the correct way to declare this function is:

```c++
template
int f(int i [[maybe_unused]]) { // Compliant
if constexpr (b) {
return i;
}
return 0;
}
```

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.