llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-tidy] readability-use-std-min-max may add empty expression statement and remove comment

Open e-kwsm opened this issue 4 months ago • 2 comments

clang-tidy 22.1.8

void f(int &n) {
  if (n < -1) n = -1 ;
  if (n > 1) n = 1/*use `clamp` when c++17 or later is enabled*/;
}
$ clang-tidy --checks=readability-use-std-min-max --fix a.cpp
a.cpp:2:3: warning: use `std::max` instead of `<` [readability-use-std-min-max]
    1 | void f(int &n) {
    2 |   if (n < -1) n = -1 ;
      |   ^~~~~~~~~~~~~~~~~~
      |   n = std::max(n, -1);
a.cpp:1:1: note: FIX-IT applied suggested code changes
    1 | void f(int &n) {
      | ^
a.cpp:2:3: note: FIX-IT applied suggested code changes
    2 |   if (n < -1) n = -1 ;
      |   ^
a.cpp:3:3: warning: use `std::min` instead of `>` [readability-use-std-min-max]
    3 |   if (n > 1) n = 1/*use `clamp` when c++17 or later is enabled*/;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |   n = std::min(n, 1);
a.cpp:3:3: note: FIX-IT applied suggested code changes
clang-tidy applied 3 of 3 suggested fixes.

The function body becomes

  n = std::max(n, -1); ;
  n = std::min(n, 1);;

https://godbolt.org/z/sEz5qoTnq

e-kwsm avatar Jul 10 '26 12:07 e-kwsm

@llvm/issue-subscribers-clang-tidy

Author: Eisuke Kawashima (e-kwsm)

clang-tidy 22.1.8
void f(int &amp;n) {
  if (n &lt; -1) n = -1 ;
  if (n &gt; 1) n = 1/*use `clamp` when c++17 or later is enabled*/;
}
$ clang-tidy --checks=readability-use-std-min-max --fix a.cpp
a.cpp:2:3: warning: use `std::max` instead of `&lt;` [readability-use-std-min-max]
    1 | void f(int &amp;n) {
    2 |   if (n &lt; -1) n = -1 ;
      |   ^~~~~~~~~~~~~~~~~~
      |   n = std::max(n, -1);
a.cpp:1:1: note: FIX-IT applied suggested code changes
    1 | void f(int &amp;n) {
      | ^
a.cpp:2:3: note: FIX-IT applied suggested code changes
    2 |   if (n &lt; -1) n = -1 ;
      |   ^
a.cpp:3:3: warning: use `std::min` instead of `&gt;` [readability-use-std-min-max]
    3 |   if (n &gt; 1) n = 1/*use `clamp` when c++17 or later is enabled*/;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |   n = std::min(n, 1);
a.cpp:3:3: note: FIX-IT applied suggested code changes
clang-tidy applied 3 of 3 suggested fixes.

The function body becomes

  n = std::max(n, -1); ;
  n = std::min(n, 1);;

<https://godbolt.org/z/sEz5qoTnq>

I’d like to work on this

xxxxbc avatar Jul 10 '26 17:07 xxxxbc