aboutsummaryrefslogtreecommitdiff
path: root/test/SemaCXX/warn-for-var-in-else.cpp
blob: 1307c43bc20a8d618cfde3f2898f7ab713e9c867 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// RUN: %clang_cc1 -fsyntax-only -verify %s
// rdar://6425550
int bar();
void do_something(int);
int *get_ptr();

int foo() {
  if (int X = bar()) {
    return X;
  } else {
    do_something(X); // expected-warning{{'X' is always zero in this context}}
    return 0;
  }
}

bool foo2() {
  if (bool B = bar()) {
    if (int Y = bar()) {
      return B;
    } else {
      do_something(Y); // expected-warning{{'Y' is always zero in this context}}
      return B;
    }
  } else {
    if (bool B2 = B) { // expected-warning{{'B' is always false in this context}}
      do_something(B); // expected-warning{{'B' is always false in this context}}
    } else if (B2) {  // expected-warning{{'B2' is always false in this context}}
      do_something(B); // expected-warning{{'B' is always false in this context}}
      do_something(B2); // expected-warning{{'B2' is always false in this context}}
    }
    return B; // expected-warning{{'B' is always false in this context}}
  }
}

void foo3() {  
  if (int *P1 = get_ptr())
    do_something(*P1);
  else if (int *P2 = get_ptr()) {
    do_something(*P1); // expected-warning{{'P1' is always NULL in this context}}
    do_something(*P2);
  } else {
    do_something(*P1); // expected-warning{{'P1' is always NULL in this context}}
    do_something(*P2); // expected-warning{{'P2' is always NULL in this context}}
  }
}