unused-expr.c revision a7846852ba8b2b53664a674a41d116e0419d8768
1// RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unreachable-code 2 3int foo(int X, int Y); 4 5double sqrt(double X); // implicitly const because of no -fmath-errno! 6 7void bar(volatile int *VP, int *P, int A, 8 _Complex double C, volatile _Complex double VC) { 9 10 VP < P; // expected-warning {{expression result unused}} 11 (void)A; 12 (void)foo(1,2); // no warning. 13 14 A < foo(1, 2); // expected-warning {{expression result unused}} 15 16 foo(1,2)+foo(4,3); // expected-warning {{expression result unused}} 17 18 19 *P; // expected-warning {{expression result unused}} 20 *VP; // no warning. 21 P[4]; // expected-warning {{expression result unused}} 22 VP[4]; // no warning. 23 24 __real__ C; // expected-warning {{expression result unused}} 25 __real__ VC; 26 27 // We know this can't change errno because of no -fmath-errno. 28 sqrt(A); // expected-warning {{ignoring return value of function declared with const attribute}} 29} 30 31extern void t1(); 32extern void t2(); 33void t3(int c) { 34 c ? t1() : t2(); 35} 36 37// This shouldn't warn: the expr at the end of the stmtexpr really is used. 38int stmt_expr(int x, int y) { 39 return ({int _a = x, _b = y; _a > _b ? _a : _b; }); 40} 41 42void nowarn(unsigned char* a, unsigned char* b) 43{ 44 unsigned char c = 1; 45 *a |= c, *b += c; 46 47 48 // PR4633 49 int y, x; 50 ((void)0), y = x; 51} 52 53void t4(int a) { 54 int b = 0; 55 56 if (a) 57 b < 1; // expected-warning{{expression result unused}} 58 else 59 b < 2; // expected-warning{{expression result unused}} 60 61 while (1) 62 b < 3; // expected-warning{{expression result unused}} 63 64 do 65 b < 4; // expected-warning{{expression result unused}} 66 while (1); 67 68 for (;;) 69 b < 5; // expected-warning{{expression result unused}} 70 71 for (b < 1;;) {} // expected-warning{{expression result unused}} 72 for (;b < 1;) {} 73 for (;;b < 1) {} // expected-warning{{expression result unused}} 74} 75 76// rdar://7186119 77int t5f(void) __attribute__((warn_unused_result)); 78void t5() { 79 t5f(); // expected-warning {{ignoring return value of function declared with warn_unused_result}} 80} 81 82 83int fn1() __attribute__ ((warn_unused_result)); 84int fn2() __attribute__ ((pure)); 85#if __has_attribute(__const) 86int fn3() __attribute__ ((__const)); 87#endif 88// rdar://6587766 89int t6() { 90 if (fn1() < 0 || fn2(2,1) < 0 || fn3(2) < 0) // no warnings 91 return -1; 92 93 fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}} 94 fn2(92, 21); // expected-warning {{ignoring return value of function declared with pure attribute}} 95 fn3(42); // expected-warning {{ignoring return value of function declared with const attribute}} 96 __builtin_fabsf(0); // expected-warning {{ignoring return value of function declared with const attribute}} 97 (void)0, fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}} 98 return 0; 99} 100 101int t7 __attribute__ ((warn_unused_result)); // expected-warning {{'warn_unused_result' attribute only applies to functions}} 102 103// PR4010 104int (*fn4)(void) __attribute__ ((warn_unused_result)); 105void t8() { 106 fn4(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}} 107} 108 109void t9() __attribute__((warn_unused_result)); // expected-warning {{attribute 'warn_unused_result' cannot be applied to functions without return value}} 110 111// rdar://7410924 112void *some_function(void); 113void t10() { 114 (void*) some_function(); //expected-warning {{expression result unused; should this cast be to 'void'?}} 115} 116 117void f(int i, ...) { 118 __builtin_va_list ap; 119 120 __builtin_va_start(ap, i); 121 __builtin_va_arg(ap, int); 122 __builtin_va_end(ap); 123} 124 125// PR8371 126int fn5() __attribute__ ((__const)); 127