LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 9193 - clang should warn if 'else if' clause is same as any previous 'if' clause
Summary: clang should warn if 'else if' clause is same as any previous 'if' clause
Status: RESOLVED DUPLICATE of bug 9952
Alias: None
Product: clang
Classification: Unclassified
Component: Frontend (show other bugs)
Version: trunk
Hardware: All All
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords: quality-of-implementation
Depends on:
Blocks:
 
Reported: 2011-02-10 14:46 PST by Sean McBride
Modified: 2012-09-12 18:08 PDT (History)
2 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sean McBride 2011-02-10 14:46:58 PST
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.
Comment 1 Sean McBride 2011-10-18 21:31:17 PDT
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"
Comment 2 Nico Weber 2012-09-12 17:58:55 PDT

*** This bug has been marked as a duplicate of bug 9952 ***
Comment 3 Sean McBride 2012-09-12 18:08:18 PDT
Nico, I don't think you should have marked this as duplicate of bug 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.