Skip to content

SemaTemplateInstantiate.cpp: warning: suggest parentheses around ‘&&’ within ‘||’ #164104

@aabhinavg1

Description

@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:

    git clone https://github.com/llvm/llvm-project.git
    cd llvm-project
    mkdir build && cd build
  2. Configure the build using CMake:

    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:

    ninja clang
  4. Observe the warning during compilation of:

    clang/lib/Sema/SemaTemplateInstantiate.cpp
    

Analysis

The expression triggering the warning is:

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:

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

Metadata

Metadata

Assignees

Labels

clang:frontendLanguage frontend issues, e.g. anything involving "Sema"code-quality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions