Issue 164104
Summary SemaTemplateInstantiate.cpp: warning: suggest parentheses around ‘&&’ within ‘||’
Labels new issue
Assignees aabhinavg1
Reporter aabhinavg1
    # Warning: Suggest parentheses around '&&' within '||' in SemaTemplateInstantiate.cpp

## Summary

While building **Clang** as part of the **LLVM project**, a compiler warning appears in
`clang/lib/Sema/SemaTemplateInstantiate.cpp` suggesting parentheses around `&&` within `||`.

```
/home/aitr/llvm/llvm-project/clang/lib/Sema/SemaTemplateInstantiate.cpp:2866:49: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
 2866 |     assert(!Success || !Trap.hasErrorOccurred() &&
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~^~
 2867 |                            "Substitution failures must be handled "
      |                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 2868 |                            "by CheckConstraintSatisfaction.");
      |                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

## Steps to Reproduce

1. Clone the LLVM project:

   ```bash
   git clone https://github.com/llvm/llvm-project.git
   cd llvm-project
   mkdir build && cd build
   ```

2. Configure the build using CMake:

   ```bash
   cmake -G Ninja ../llvm  \
     -DLLVM_ENABLE_PROJECTS="clang;lld" \
     -DLLVM_USE_LINKER=lld \
     -DLLVM_PARALLEL_LINK_JOBS=1 \
     -DCMAKE_BUILD_TYPE=Debug \
     -DLLVM_ENABLE_ASSERTIONS=ON \
     -DLLVM_TARGETS_TO_BUILD="AMDGPU;PowerPC;AArch64;X86" \
     -DLLVM_ENABLE_DUMP=ON
   ```

3. Build Clang:

   ```bash
   ninja clang
   ```

4. Observe the warning during compilation of:

   ```
   clang/lib/Sema/SemaTemplateInstantiate.cpp
   ```

## Analysis

The _expression_ triggering the warning is:

```cpp
assert(!Success || !Trap.hasErrorOccurred() &&
       "Substitution failures must be handled "
       "by CheckConstraintSatisfaction.");
```

Operator precedence causes `&&` to bind tighter than `||`, which may lead to ambiguous intent. Although semantically correct, adding parentheses improves clarity and suppresses warnings.

## Proposed Fix

Wrap the `&&` clause with parentheses for clarity:

```cpp
assert(!Success || (!Trap.hasErrorOccurred() &&
       "Substitution failures must be handled "
       "by CheckConstraintSatisfaction."));
```


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

Reply via email to