warn-unused-value.c revision 25973455aed1cdc9c40b208c792b5db4f8f1297d
1// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value %s
2// RUN: %clang_cc1 -fsyntax-only -verify -Wunused %s
3// RUN: %clang_cc1 -fsyntax-only -verify -Wall %s
4
5int i = 0;
6int j = 0;
7
8void foo();
9
10// PR4806
11void pr4806() {
12  1,foo();          // expected-warning {{expression result unused}}
13
14  // other
15  foo();
16  i;                // expected-warning {{expression result unused}}
17
18  i,foo();          // expected-warning {{expression result unused}}
19  foo(),i;          // expected-warning {{expression result unused}}
20
21  i,j,foo();        // expected-warning {{expression result unused}} expected-warning {{expression result unused}}
22  i,foo(),j;        // expected-warning {{expression result unused}} expected-warning {{expression result unused}}
23  foo(),i,j;        // expected-warning {{expression result unused}} expected-warning {{expression result unused}}
24
25  i++;
26
27  i++,foo();
28  foo(),i++;
29
30  i++,j,foo();      // expected-warning {{expression result unused}}
31  i++,foo(),j;      // expected-warning {{expression result unused}}
32  foo(),i++,j;      // expected-warning {{expression result unused}}
33
34  i,j++,foo();      // expected-warning {{expression result unused}}
35  i,foo(),j++;      // expected-warning {{expression result unused}}
36  foo(),i,j++;      // expected-warning {{expression result unused}}
37
38  i++,j++,foo();
39  i++,foo(),j++;
40  foo(),i++,j++;
41
42  {};
43  ({});
44  ({}),foo();
45  foo(),({});
46
47  (int)1U;          // expected-warning {{expression result unused}}
48  (void)1U;
49
50  // pointer to volatile has side effect (thus no warning)
51  int* pi = &i;
52  volatile int* pj = &j;
53  *pi;              // expected-warning {{expression result unused}}
54  *pj;
55}
56
57// Don't warn about unused '||', '&&' expressions that contain assignments.
58int test_logical_foo1();
59int test_logical_foo2();
60int test_logical_foo3();
61int test_logical_bar() {
62  int x = 0;
63  (x = test_logical_foo1()) ||  // no-warning
64  (x = test_logical_foo2()) ||  // no-warning
65  (x = test_logical_foo3());    // no-warning
66
67  x || test_logical_foo1();     // no-warning
68
69  return x;
70}
71
72struct s0 { int f0; };
73
74void f0(int a);
75void f1(struct s0 *a) {
76  // rdar://8139785
77  f0((int)(a->f0 + 1, 10)); // expected-warning {{expression result unused}}
78}
79