llvm / llvm/llvm-project

[clang-tidy] Incorrect suggestion for readability-simplify-boolean-expr

Open
#163,128 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

clang-tidy invalid-code-generation
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Reproducer: https://godbolt.org/z/cKfnavMvb

```
#include
#include
struct Foo {
std::string appName;
int maxCopy;
};

int main()
{
std::string app;
Foo fileMeta;
int maxCopy;
bool x = !(app == fileMeta.appName && maxCopy == fileMeta.maxCopy);
}
```

clang-tidy shows:

```
:13:10: warning: Value stored to 'x' during its initialization is never read [clang-analyzer-deadcode.DeadStores]
13 | bool x = !(app == fileMeta.appName && maxCopy == fileMeta.maxCopy);
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:13:10: note: Value stored to 'x' during its initialization is never read
13 | bool x = !(app == fileMeta.appName && maxCopy == fileMeta.maxCopy);
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:13:14: warning: boolean expression can be simplified by DeMorgan's theorem [readability-simplify-boolean-expr]
13 | bool x = !(app == fileMeta.appName && maxCopy == fileMeta.maxCopy);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ! || !=
2 warnings generated.
```

But it should be `app != fileMeta.appName || maxCopy != fileMeta.maxCopy)`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the clang-tidy readability-simplify-boolean-expr check and reproduce the warning using the linked Compiler Explorer example. Compare the suggested DeMorgan transformation with the expected app != fileMeta.appName || maxCopy != fileMeta.maxCopy result, then add or update coverage for this expression so the diagnostic and replacement are correct.

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
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.