[cfe-commits] r60858 - in /cfe/trunk: include/clang/AST/Decl.h include/clang/Basic/DiagnosticKinds.def include/clang/Parse/Scope.h lib/Parse/ParseStmt.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaExprCXX.cpp test/SemaCXX/warn-for-var-in-else.cpp

Douglas Gregor dgregor at apple.com
Thu Dec 11 07:00:05 PST 2008


On Dec 11, 2008, at 6:06 AM, Sebastian Redl wrote:

> Douglas Gregor wrote:
>> Author: dgregor
>> Date: Wed Dec 10 17:01:14 2008
>> New Revision: 60858
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=60858&view=rev
>> Log:
>> Added a warning when referencing an if's condition variable in the
>> "else" clause, e.g.,
>>
>>  if (int X = foo()) {
>>  } else {
>>    if (X) { // warning: X is always zero in this context
>>    }
>>  }
>>
> What happens for
>
> struct WithToBool
> {
> operator bool() { ... }
>
> // Other data, not necessarily invalid if (bool)WTB is false.
> };
>
> if(WithToBool wtb = something()) {
> } else {
> orother(wtb);
> }
>
> It seems to me, from looking at the code (not trying it) that this  
> will warn about wtb always being zero.

We only produce this warning for scalar types:

+  if (VarDecl *Var = dyn_cast<VarDecl>(VD)) {
+    if (Var->isDeclaredInCondition() && Var->getType()- 
 >isScalarType()) {

	- Doug




More information about the cfe-commits mailing list