Issue 207993
Summary clang-tidy check `readability-simplify-boolean-expr` produces bad fixes on C-code when using C23; seems to assume that code is C++
Labels
Assignees
Reporter Gizzzzmo
    Since C23 added `true` and `false` as literals, I thought I'd try using `readability-simplify-boolean-expr`, which doesn't work at all with older C standards because true and false are just macros that resolve to 1 and 0, and tidy seems to only match on syntax tree nodes whose type is *actually* `bool`.

And while it does indeed work, the fixes tidy proposes (and applies with `--fix`) seem to assume that the code is C++, as it tries to inject a `static_cast<bool>`:

```c
// simplify_bool_expr.c
#include <stdbool.h>

bool blub(int x)
{
    if(x > 10) {
        return true;
    }

    return false;
}
```
```
clang-tidy --checks="-*,readability-simplify*" simplify_bool_expr.c -- --std=c23
3 warnings generated.
/home/jonas/gitprjs/siemens/firmware/xmc4700/clang-tidy-plugins/simplify_bool_expr.c:6:16: warning: redundant boolean literal in conditional return statement [readability-simplify-boolean-expr]
    5 |     if(x > 10) {
      |     ~~~~~~~~~~~~
      |     return static_cast<bool>(x > 10)
    6 |         return true;
      |         ~~~~~~~^~~~~
    7 |     }
      |     ~
    8 |
    9 |     return false;
      |     ~~~~~~~~~~~~
Suppressed 2 warnings (2 with check filters).
```

And after running the same command with `--fix` the source file is invalid C:
```c
// simplify_bool.c
#include <stdbool.h>

bool blub(int x)
{
    return static_cast<bool>(x > 10);
}
```

Which tidy seems to agree with 🙃:
```
clang-tidy --checks="-*,readability-simplify*" simplify_bool_expr.c -- --std=c23
4 warnings and 2 errors generated.
Error while processing /home/jonas/gitprjs/siemens/firmware/xmc4700/clang-tidy-plugins/simplify_bool_expr.c.
/home/jonas/gitprjs/siemens/firmware/xmc4700/clang-tidy-plugins/simplify_bool_expr.c:5:12: error: use of undeclared identifier 'static_cast' [clang-diagnostic-error]
    5 |     return static_cast<bool>(x > 10);
      |            ^~~~~~~~~~~
/home/jonas/gitprjs/siemens/firmware/xmc4700/clang-tidy-plugins/simplify_bool_expr.c:5:24: error: expected _expression_ [clang-diagnostic-error]
    5 |     return static_cast<bool>(x > 10);
      |                        ^
Suppressed 4 warnings (4 with check filters).
Found compiler error(s).
```

I'm using clang-tidy based on llvm version 22.1.0.



_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to