1 1.9 rillig /* $NetBSD: msg_162.c,v 1.9 2024/05/11 15:53:38 rillig Exp $ */ 2 1.1 rillig # 3 "msg_162.c" 3 1.1 rillig 4 1.7 rillig // Test for message: operator '%s' compares '%s' with '%s' [162] 5 1.1 rillig 6 1.8 rillig /* lint1-extra-flags: -hp -X 351 */ 7 1.3 rillig 8 1.3 rillig void 9 1.3 rillig left_unsigned(unsigned int ui) 10 1.3 rillig { 11 1.9 rillig /* expect+1: warning: comparing integer 'unsigned int' to floating point constant -5 [379] */ 12 1.3 rillig if (ui < -5.0) { 13 1.3 rillig } 14 1.3 rillig 15 1.7 rillig /* expect+1: warning: operator '<' compares 'unsigned int' with 'negative constant' [162] */ 16 1.3 rillig if (ui < -5) { 17 1.3 rillig } 18 1.3 rillig 19 1.7 rillig /* expect+1: warning: operator '<' compares 'unsigned int' with '0' [162] */ 20 1.3 rillig if (ui < 0) { 21 1.3 rillig } 22 1.3 rillig 23 1.7 rillig /* expect+1: warning: operator '>=' compares 'unsigned int' with '0' [162] */ 24 1.3 rillig if (ui >= 0) { 25 1.3 rillig } 26 1.3 rillig 27 1.6 rillig /* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */ 28 1.3 rillig if (ui <= 0) { 29 1.3 rillig } 30 1.3 rillig } 31 1.3 rillig 32 1.3 rillig void 33 1.3 rillig right_unsigned(unsigned int ui) 34 1.3 rillig { 35 1.3 rillig 36 1.3 rillig if (-5.0 > ui) { 37 1.3 rillig } 38 1.3 rillig 39 1.7 rillig /* expect+1: warning: operator '>' compares 'negative constant' with 'unsigned int' [162] */ 40 1.3 rillig if (-5 > ui) { 41 1.3 rillig } 42 1.3 rillig 43 1.7 rillig /* expect+1: warning: operator '>' compares '0' with 'unsigned int' [162] */ 44 1.3 rillig if (0 > ui) { 45 1.3 rillig } 46 1.3 rillig 47 1.7 rillig /* expect+1: warning: operator '<=' compares '0' with 'unsigned int' [162] */ 48 1.3 rillig if (0 <= ui) { 49 1.3 rillig } 50 1.3 rillig 51 1.6 rillig /* before 2021-09-05: comparison of 0 with unsigned int, op >= [162] */ 52 1.3 rillig if (0 >= ui) { 53 1.3 rillig } 54 1.3 rillig } 55 1.4 rillig 56 1.4 rillig /* 57 1.4 rillig * Lint does not care about these comparisons, even though they are obviously 58 1.4 rillig * out of range. 59 1.4 rillig */ 60 1.4 rillig void 61 1.4 rillig compare_signed_char(signed char sc) 62 1.4 rillig { 63 1.4 rillig if (sc == -129) 64 1.4 rillig return; 65 1.4 rillig if (sc == -128) 66 1.4 rillig return; 67 1.4 rillig if (sc == 127) 68 1.4 rillig return; 69 1.4 rillig if (sc == 128) 70 1.4 rillig return; 71 1.4 rillig } 72 1.4 rillig 73 1.4 rillig void 74 1.4 rillig compare_unsigned_char(unsigned char uc) 75 1.4 rillig { 76 1.7 rillig /* expect+1: warning: operator '==' compares 'unsigned char' with 'negative constant' [162] */ 77 1.4 rillig if (uc == -1) 78 1.4 rillig return; 79 1.4 rillig if (uc == 0) 80 1.4 rillig return; 81 1.4 rillig if (uc == 255) 82 1.4 rillig return; 83 1.4 rillig if (uc == 256) 84 1.4 rillig return; 85 1.4 rillig } 86 1.5 rillig 87 1.5 rillig void take_bool(_Bool); 88 1.5 rillig 89 1.5 rillig void 90 1.5 rillig compare_operators(unsigned int x) 91 1.5 rillig { 92 1.7 rillig /* expect+1: warning: operator '<' compares 'unsigned int' with 'negative constant' [162] */ 93 1.5 rillig take_bool(x < -1); 94 1.7 rillig /* expect+1: warning: operator '<' compares 'unsigned int' with '0' [162] */ 95 1.5 rillig take_bool(x < 0); 96 1.5 rillig take_bool(x < 1); 97 1.5 rillig 98 1.7 rillig /* expect+1: warning: operator '<=' compares 'unsigned int' with 'negative constant' [162] */ 99 1.5 rillig take_bool(x <= -1); 100 1.5 rillig /* 101 1.6 rillig * Before tree.c 1.379 from 2021-09-05, lint warned about 102 1.6 rillig * 'unsigned <= 0' as well as '0 >= unsigned'. In all cases where 103 1.6 rillig * the programmer knows whether the underlying data type is signed or 104 1.6 rillig * unsigned, it is clearer to express the same thought as 105 1.6 rillig * 'unsigned == 0', but that's a stylistic issue only. 106 1.6 rillig * 107 1.6 rillig * Removing this particular case of the warning is not expected to 108 1.6 rillig * miss any bugs. The expression 'x <= 0' is equivalent to 'x < 1', 109 1.6 rillig * so lint should not warn about it, just as it doesn't warn about 110 1.6 rillig * the inverted condition, which is 'x > 0'. 111 1.5 rillig */ 112 1.6 rillig /* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */ 113 1.5 rillig take_bool(x <= 0); 114 1.5 rillig take_bool(x <= 1); 115 1.5 rillig 116 1.7 rillig /* expect+1: warning: operator '>' compares 'unsigned int' with 'negative constant' [162] */ 117 1.5 rillig take_bool(x > -1); 118 1.5 rillig take_bool(x > 0); 119 1.5 rillig take_bool(x > 1); 120 1.5 rillig 121 1.7 rillig /* expect+1: warning: operator '>=' compares 'unsigned int' with 'negative constant' [162] */ 122 1.5 rillig take_bool(x >= -1); 123 1.7 rillig /* expect+1: warning: operator '>=' compares 'unsigned int' with '0' [162] */ 124 1.5 rillig take_bool(x >= 0); 125 1.5 rillig take_bool(x >= 1); 126 1.5 rillig 127 1.7 rillig /* expect+1: warning: operator '==' compares 'unsigned int' with 'negative constant' [162] */ 128 1.5 rillig take_bool(x == -1); 129 1.5 rillig take_bool(x == 0); 130 1.5 rillig take_bool(x == 1); 131 1.5 rillig 132 1.7 rillig /* expect+1: warning: operator '!=' compares 'unsigned int' with 'negative constant' [162] */ 133 1.5 rillig take_bool(x != -1); 134 1.5 rillig take_bool(x != 0); 135 1.5 rillig take_bool(x != 1); 136 1.5 rillig } 137