Skip to content
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

Improvements for Clang warning -Wtautological-overlap-compare #13473

Open
llvmbot opened this issue Jun 13, 2012 · 4 comments
Open

Improvements for Clang warning -Wtautological-overlap-compare #13473

llvmbot opened this issue Jun 13, 2012 · 4 comments
Labels
bugzilla Issues migrated from bugzilla clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Jun 13, 2012

Bugzilla Link 13101
Version unspecified
OS All
Reporter LLVM Bugzilla Contributor
CC @chandlerc,@nico,@Weverything

Extended Description

The simple test case, where

p[0] != 'h' || p[0] != 'H'
is always true, is not warned by clang++ and g++ too.

It will nice, If we have some flag like -Walways-true, which should warn on such usage.
This will churn out silly bugs.

tested with the following code.

int main()
{
        char *p ="Hello World\n";
        if(p[0] != 'h' || p[0] != 'H')
        {
                cout << "Word does not start with h/H\n";
        }
        return 0;
}
@chandlerc
Copy link
Member

Just fixed a bug in LLVM that this warning would have caught in r285930.

It would be great to actually implement this. Especially for binary expressions with ||, and the false variant for &&.

A much simpler set of test cases:

bool f(int i) {
return i != 1 || i != 2;
}

bool g(int i) {
return i == 1 && i == 2;
}

@Weverything
Copy link
Contributor

Since this original bug was filed, a new warning group, -Wtautological-overlap-compare, a sub-group of -Wtautological-compare, has been added to clang which can detect when a logical expression with multiple sub-expressions will always evaluate to true or always evaluate to false. This is in r205665.

From the test cases provided, there's at least three areas for improvement:

  1. The logical expression in return or assignment statements
  2. Handling character literals and other non-integer literal values as the constant
  3. Handling variables that are specific array indexes

@Weverything
Copy link
Contributor

https://reviews.llvm.org/rL290920

  1. has been fixed in r290920

@llvmbot
Copy link
Collaborator Author

llvmbot commented Jan 9, 2017

hmm... I think the warning message "overlapping comparisons always evaluate to false" is unfortunate. Comparisons such as "x < 2" and "x > 5" are not "overlapping".

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
@philnik777 philnik777 added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

No branches or pull requests

4 participants