Skip to content

clang/lib/Sema/SemaOverload.cpp: 2 * boolean in compare ? #102912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dcb314 opened this issue Aug 12, 2024 · 3 comments · Fixed by #102948
Closed

clang/lib/Sema/SemaOverload.cpp: 2 * boolean in compare ? #102912

dcb314 opened this issue Aug 12, 2024 · 3 comments · Fixed by #102948
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" code-quality

Comments

@dcb314
Copy link

dcb314 commented Aug 12, 2024

Static analyser cppcheck says:

clang/lib/Sema/SemaOverload.cpp:512:26: style: Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. [comparisonOfBoolWithBoolError]

Source code is

  return (FromWidth < ToWidth + (FromSigned == ToSigned)) &&
         (FromSigned <= ToSigned);

I am pretty sure comparison operators don't apply to booleans.

clang/lib/Sema/SemaOverload.cpp:545:44: style: Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. [comparisonOfBoolWithBoolError]

Duplicate.

@dcb314 dcb314 added clang Clang issues not falling into any other category code-quality labels Aug 12, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang Clang issues not falling into any other category labels Aug 12, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 12, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (dcb314)

Static analyser cppcheck says:

clang/lib/Sema/SemaOverload.cpp:512:26: style: Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. [comparisonOfBoolWithBoolError]

Source code is

  return (FromWidth &lt; ToWidth + (FromSigned == ToSigned)) &amp;&amp;
         (FromSigned &lt;= ToSigned);

I am pretty sure comparison operators don't apply to booleans.

clang/lib/Sema/SemaOverload.cpp:545:44: style: Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. [comparisonOfBoolWithBoolError]

Duplicate.

@MitalAshok
Copy link
Contributor

This was previously effectively !(FromSigned && !ToSigned) before I changed it with #78112

I make sense of it as: If From is "more" signed than To then it fails the check, which maps nicely to the comparison operator. The old one was less readable for me, especially when De Morgan'd as !FromSigned || ToSigned.

This is personal opinion, Clang maintainers might disagree. It's a very easy NFC fix.

@AaronBallman
Copy link
Collaborator

FWIW, when I was reviewing those changes, I definitely had to spend a while to convince myself that code was correct, so it probably would be reasonable to rewrite it.

cor3ntin pushed a commit that referenced this issue Aug 22, 2024
Static analyser tool cppcheck flags ordered comparison with `bool`s.
Replace with equivalent logical operators to prevent this.

Closes #102912
cjdb pushed a commit to cjdb/llvm-project that referenced this issue Aug 23, 2024
Static analyser tool cppcheck flags ordered comparison with `bool`s.
Replace with equivalent logical operators to prevent this.

Closes llvm#102912
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" code-quality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants