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

clang should warn if 'else if' clause is same as any previous 'if' clause #9565

Closed
seanm opened this issue Feb 10, 2011 · 4 comments
Closed
Labels
bugzilla Issues migrated from bugzilla clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate quality-of-implementation

Comments

@seanm
Copy link

seanm commented Feb 10, 2011

Bugzilla Link 9193
Resolution DUPLICATE
Resolved on Sep 12, 2012 18:08
Version trunk
OS All
CC @nico

Extended Description

Consider the following C snippit:

if (x == 5)
{
	...
}
else if (x == 5)
{
	...
}

I've made a typo in the 'else if' and accidentally tested condition.

It would be nice it clang could warn when it sees this.

With raw ints it's easy to see the mistake, but when they are constants, and two constants accidentally have the same value, it's harder to find.

It would be happy with this in either the compiler or static analyzer.

@seanm
Copy link
Author

seanm commented Oct 19, 2011

Interesting to note that PVS-Studio can catch this, and found an example of it within clang's own codebase:

http://software.intel.com/en-us/blogs/2011/08/08/pvs-studio-vs-clang/
(scroll down to "Copy-Paste error N5")

"V523 The 'then' statement is equivalent to the 'else' statement. LLVMInstCombine instcombineandorxor.cpp 1387"

@nico
Copy link
Contributor

nico commented Sep 13, 2012

*** This bug has been marked as a duplicate of bug llvm/llvm-bugzilla-archive#9952 ***

@seanm
Copy link
Author

seanm commented Sep 13, 2012

Nico, I don't think you should have marked this as duplicate of bug llvm/llvm-bugzilla-archive#9952 .

bug 9952 is talking about what PVS Studio calls:
"V501. There are identical sub-expressions to the left and to the right of the 'foo' operator"
http://www.viva64.com/en/d/0090/

bug 9193 (this bug) is talking about what PVS Studio calls:
"V517. The use of 'if (A) {...} else if (A) {...}' pattern was detected."
http://www.viva64.com/en/d/0106/

I'd like to see both, bug don't see the reason to conflate them.

@nico
Copy link
Contributor

nico commented Nov 27, 2021

mentioned in issue llvm/llvm-bugzilla-archive#9952

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
This issue was closed.
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:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate quality-of-implementation
Projects
None yet
Development

No branches or pull requests

2 participants