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 9952 - Warn on identical subexpressions to the left and right of ||, &&, possibly >= etc
Summary: Warn on identical subexpressions to the left and right of ||, &&, possibly >=...
Status: NEW
Alias: None
Product: clang
Classification: Unclassified
Component: Frontend (show other bugs)
Version: unspecified
Hardware: PC All
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
: 9193 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-05-19 11:59 PDT by Nico Weber
Modified: 2013-05-15 10:44 PDT (History)
8 users (show)

See Also:
Fixed By Commit(s):


Attachments
possible testcases (1.75 KB, application/octet-stream)
2011-07-06 10:09 PDT, Nico Weber
Details
work-in-progress patch idea (7.79 KB, patch)
2013-05-15 10:44 PDT, Hans Wennborg
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nico Weber 2011-05-19 11:59:53 PDT
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?
Comment 1 Nico Weber 2011-05-19 12:00:54 PDT
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?
Comment 2 Hans Wennborg 2011-05-19 12:36:50 PDT
http://www.viva64.com/en/d/0090/ has more information about that warning and points out some cases where it doesn't warn.
Comment 3 Ryan Sleevi 2011-05-26 20:53:37 PDT
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;
Comment 4 Nico Weber 2011-07-06 10:09:06 PDT
Created attachment 6839 [details]
possible testcases

I don't think we can support the "cy().isRelative()" case without being to noisy.
Comment 5 Nico Weber 2012-03-28 11:16:15 PDT
clang's r153568 suggests that warning on identical subexpressions next to >= (and <=, <, >) is probably useful, too.
Comment 6 Hans Wennborg 2012-09-04 09:11:42 PDT
A related idea would be to warn about identical condition expressions in if-else statements:

if (cond)
  doStuff();
else if (cond)
  doOtherStuff();
Comment 7 Nico Weber 2012-09-12 17:58:55 PDT
*** Bug 9193 has been marked as a duplicate of this bug. ***
Comment 8 Hans Wennborg 2013-05-15 10:44:19 PDT
Created attachment 10514 [details]
work-in-progress patch idea

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