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?
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?
http://www.viva64.com/en/d/0090/ has more information about that warning and points out some cases where it doesn't warn.
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;
Created attachment 6839 [details] possible testcases I don't think we can support the "cy().isRelative()" case without being to noisy.
clang's r153568 suggests that warning on identical subexpressions next to >= (and <=, <, >) is probably useful, too.
A related idea would be to warn about identical condition expressions in if-else statements: if (cond) doStuff(); else if (cond) doOtherStuff();
*** Bug 9193 has been marked as a duplicate of this bug. ***
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.