llvm / llvm/llvm-project

[clang-tidy] readability-use-std-min-max generates invalid code for string, where bare `size_type` is used

Open
#208,693 3 comments 0 reactions 0 assignees View on GitHub
clang-tidy invalid-code-generation
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

clang-tidy 22.1.8

```cpp
#include

void f(const std::string &s, unsigned &n) {
if (s.size() > n)
n = s.size();
}
```

`clang-tidy --checks=readability-use-std-min-max --fix a.cpp` rewrites the function body as follows

```cpp
n = std::max(s.size(), n);
```

which does not compile since `size_type` is unknown; the correct fix is

```cpp
n = std::max(s.size(), n);
// or std::string::size_type, or size_t?
```

Contributor guide

Open the contributing guide

Research direction

Start with the clang-tidy readability-use-std-min-max check and reproduce the issue using the a.cpp example and the provided clang-tidy command. Trace how the replacement type is selected for s.size(), then verify the fix no longer emits bare size_type and that the resulting a.cpp compiles.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.