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