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