1*9cfc443fSrillig /* $NetBSD: msg_162.c,v 1.9 2024/05/11 15:53:38 rillig Exp $ */
2a0a15c14Srillig # 3 "msg_162.c"
3a0a15c14Srillig
440a9b8fdSrillig // Test for message: operator '%s' compares '%s' with '%s' [162]
5a0a15c14Srillig
6b2baa501Srillig /* lint1-extra-flags: -hp -X 351 */
70b138779Srillig
80b138779Srillig void
left_unsigned(unsigned int ui)90b138779Srillig left_unsigned(unsigned int ui)
100b138779Srillig {
11*9cfc443fSrillig /* expect+1: warning: comparing integer 'unsigned int' to floating point constant -5 [379] */
120b138779Srillig if (ui < -5.0) {
130b138779Srillig }
140b138779Srillig
1540a9b8fdSrillig /* expect+1: warning: operator '<' compares 'unsigned int' with 'negative constant' [162] */
160b138779Srillig if (ui < -5) {
170b138779Srillig }
180b138779Srillig
1940a9b8fdSrillig /* expect+1: warning: operator '<' compares 'unsigned int' with '0' [162] */
200b138779Srillig if (ui < 0) {
210b138779Srillig }
220b138779Srillig
2340a9b8fdSrillig /* expect+1: warning: operator '>=' compares 'unsigned int' with '0' [162] */
240b138779Srillig if (ui >= 0) {
250b138779Srillig }
260b138779Srillig
271f920779Srillig /* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */
280b138779Srillig if (ui <= 0) {
290b138779Srillig }
300b138779Srillig }
310b138779Srillig
320b138779Srillig void
right_unsigned(unsigned int ui)330b138779Srillig right_unsigned(unsigned int ui)
340b138779Srillig {
350b138779Srillig
360b138779Srillig if (-5.0 > ui) {
370b138779Srillig }
380b138779Srillig
3940a9b8fdSrillig /* expect+1: warning: operator '>' compares 'negative constant' with 'unsigned int' [162] */
400b138779Srillig if (-5 > ui) {
410b138779Srillig }
420b138779Srillig
4340a9b8fdSrillig /* expect+1: warning: operator '>' compares '0' with 'unsigned int' [162] */
440b138779Srillig if (0 > ui) {
450b138779Srillig }
460b138779Srillig
4740a9b8fdSrillig /* expect+1: warning: operator '<=' compares '0' with 'unsigned int' [162] */
480b138779Srillig if (0 <= ui) {
490b138779Srillig }
500b138779Srillig
511f920779Srillig /* before 2021-09-05: comparison of 0 with unsigned int, op >= [162] */
520b138779Srillig if (0 >= ui) {
530b138779Srillig }
540b138779Srillig }
550d1a27f6Srillig
560d1a27f6Srillig /*
570d1a27f6Srillig * Lint does not care about these comparisons, even though they are obviously
580d1a27f6Srillig * out of range.
590d1a27f6Srillig */
600d1a27f6Srillig void
compare_signed_char(signed char sc)610d1a27f6Srillig compare_signed_char(signed char sc)
620d1a27f6Srillig {
630d1a27f6Srillig if (sc == -129)
640d1a27f6Srillig return;
650d1a27f6Srillig if (sc == -128)
660d1a27f6Srillig return;
670d1a27f6Srillig if (sc == 127)
680d1a27f6Srillig return;
690d1a27f6Srillig if (sc == 128)
700d1a27f6Srillig return;
710d1a27f6Srillig }
720d1a27f6Srillig
730d1a27f6Srillig void
compare_unsigned_char(unsigned char uc)740d1a27f6Srillig compare_unsigned_char(unsigned char uc)
750d1a27f6Srillig {
7640a9b8fdSrillig /* expect+1: warning: operator '==' compares 'unsigned char' with 'negative constant' [162] */
770d1a27f6Srillig if (uc == -1)
780d1a27f6Srillig return;
790d1a27f6Srillig if (uc == 0)
800d1a27f6Srillig return;
810d1a27f6Srillig if (uc == 255)
820d1a27f6Srillig return;
830d1a27f6Srillig if (uc == 256)
840d1a27f6Srillig return;
850d1a27f6Srillig }
86165e1288Srillig
87165e1288Srillig void take_bool(_Bool);
88165e1288Srillig
89165e1288Srillig void
compare_operators(unsigned int x)90165e1288Srillig compare_operators(unsigned int x)
91165e1288Srillig {
9240a9b8fdSrillig /* expect+1: warning: operator '<' compares 'unsigned int' with 'negative constant' [162] */
93165e1288Srillig take_bool(x < -1);
9440a9b8fdSrillig /* expect+1: warning: operator '<' compares 'unsigned int' with '0' [162] */
95165e1288Srillig take_bool(x < 0);
96165e1288Srillig take_bool(x < 1);
97165e1288Srillig
9840a9b8fdSrillig /* expect+1: warning: operator '<=' compares 'unsigned int' with 'negative constant' [162] */
99165e1288Srillig take_bool(x <= -1);
100165e1288Srillig /*
1011f920779Srillig * Before tree.c 1.379 from 2021-09-05, lint warned about
1021f920779Srillig * 'unsigned <= 0' as well as '0 >= unsigned'. In all cases where
1031f920779Srillig * the programmer knows whether the underlying data type is signed or
1041f920779Srillig * unsigned, it is clearer to express the same thought as
1051f920779Srillig * 'unsigned == 0', but that's a stylistic issue only.
1061f920779Srillig *
1071f920779Srillig * Removing this particular case of the warning is not expected to
1081f920779Srillig * miss any bugs. The expression 'x <= 0' is equivalent to 'x < 1',
1091f920779Srillig * so lint should not warn about it, just as it doesn't warn about
1101f920779Srillig * the inverted condition, which is 'x > 0'.
111165e1288Srillig */
1121f920779Srillig /* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */
113165e1288Srillig take_bool(x <= 0);
114165e1288Srillig take_bool(x <= 1);
115165e1288Srillig
11640a9b8fdSrillig /* expect+1: warning: operator '>' compares 'unsigned int' with 'negative constant' [162] */
117165e1288Srillig take_bool(x > -1);
118165e1288Srillig take_bool(x > 0);
119165e1288Srillig take_bool(x > 1);
120165e1288Srillig
12140a9b8fdSrillig /* expect+1: warning: operator '>=' compares 'unsigned int' with 'negative constant' [162] */
122165e1288Srillig take_bool(x >= -1);
12340a9b8fdSrillig /* expect+1: warning: operator '>=' compares 'unsigned int' with '0' [162] */
124165e1288Srillig take_bool(x >= 0);
125165e1288Srillig take_bool(x >= 1);
126165e1288Srillig
12740a9b8fdSrillig /* expect+1: warning: operator '==' compares 'unsigned int' with 'negative constant' [162] */
128165e1288Srillig take_bool(x == -1);
129165e1288Srillig take_bool(x == 0);
130165e1288Srillig take_bool(x == 1);
131165e1288Srillig
13240a9b8fdSrillig /* expect+1: warning: operator '!=' compares 'unsigned int' with 'negative constant' [162] */
133165e1288Srillig take_bool(x != -1);
134165e1288Srillig take_bool(x != 0);
135165e1288Srillig take_bool(x != 1);
136165e1288Srillig }
137