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

Warn on identical subexpressions to the left and right of ||, &&, possibly >= etc #10324

Open
nico opened this issue May 19, 2011 · 8 comments
Open
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

@nico
Copy link
Contributor

nico commented May 19, 2011

Bugzilla Link 9952
Version unspecified
OS All
CC @chandlerc,@AnnaZaks,@gribozavr,@zmodem,@pwo,@seanm

Extended Description

http://www.viva64.com/ru/pvs-studio/ seems to warn on this code:

void f() {
int a0 = 0, a1 = 1;
if (a0 == 0 || a0 == 0)
a1 = 2;
}

It says something along the lines of "There are identical sub-expressions to the left and to the right of the '||' operator."

Can / should clang do this, too?

@nico
Copy link
Contributor Author

nico commented May 19, 2011

chandler: At one point, this would've discovered a minor-ish bug in chrome. Maybe you know someone who might want to work on this?

@zmodem
Copy link
Collaborator

zmodem commented May 19, 2011

http://www.viva64.com/en/d/0090/ has more information about that warning and points out some cases where it doesn't warn.

@llvmbot
Copy link
Collaborator

llvmbot commented May 27, 2011

Nico asked me to add some samples that represent real bugs that would have been caught with such a check. Each of these bugs are in various open-source libraries

Mesa3D, filed as https://bugs.freedesktop.org/show_bug.cgi?id=37648
Original:
else if (width > 0 && height > 0 && height > 0) {

Fixed:
else if (width > 0 && height > 0 && depth > 0) {


libjingle, filed as http://crbug.com/84129
Original:
if (!has_audio && !has_audio) {

Fixed:
if (!has_audio && !has_video) {


Webkit, filed as http://crbug.com/84141
Original:
return cy().isRelative()
|| cy().isRelative()
|| r().isRelative()
|| fx().isRelative()
|| fy().isRelative();

Fixed:
return cx().isRelative()
|| cy().isRelative() <-- Fix
|| r().isRelative()
|| fx().isRelative()
|| fy().isRelative();

Also http://crbug.com/84140 , http://crbug.com/84139

Original:
if (x >= 0 && x < paintingData.width && x >= 0 && y < paintingData.height)

Fixed:
if (x >= 0 && x < paintingData.width && y >= 0 && y < paintingData.height)


skia, filed as http://crbug.com/84138
Original:
return kKeep_StencilOp == fFrontPassOp &&
kKeep_StencilOp == fBackPassOp &&
kKeep_StencilOp == fFrontFailOp &&
kKeep_StencilOp == fFrontFailOp &&
kAlways_StencilFunc == fFrontFunc &&
kAlways_StencilFunc == fBackFunc;

Fixed:
return kKeep_StencilOp == fFrontPassOp &&
kKeep_StencilOp == fBackPassOp &&
kKeep_StencilOp == fFrontFailOp &&
kKeep_StencilOp == fBackFailOp && <-- Fix
kAlways_StencilFunc == fFrontFunc &&
kAlways_StencilFunc == fBackFunc;

@nico
Copy link
Contributor Author

nico commented Jul 6, 2011

possible testcases
I don't think we can support the "cy().isRelative()" case without being to noisy.

@nico
Copy link
Contributor Author

nico commented Mar 28, 2012

clang's r153568 suggests that warning on identical subexpressions next to >= (and <=, <, >) is probably useful, too.

@zmodem
Copy link
Collaborator

zmodem commented Sep 4, 2012

A related idea would be to warn about identical condition expressions in if-else statements:

if (cond)
doStuff();
else if (cond)
doOtherStuff();

@nico
Copy link
Contributor Author

nico commented Sep 13, 2012

*** Bug #9565 has been marked as a duplicate of this bug. ***

@zmodem
Copy link
Collaborator

zmodem commented May 15, 2013

work-in-progress patch idea
Attaching work-in-progress patch for safekeeping in case I loose it or my hard drive fails.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
@Quuxplusone Quuxplusone added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 17, 2022
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