summaryrefslogtreecommitdiff
path: root/tests/usr.bin/xlint/lint1/msg_162.c
blob: 717a02081c4eacf1034c105b45d26bcc08fc0536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*	$NetBSD: msg_162.c,v 1.8 2023/03/28 14:44:35 rillig Exp $	*/
# 3 "msg_162.c"

// Test for message: operator '%s' compares '%s' with '%s' [162]

/* lint1-extra-flags: -hp -X 351 */

void
left_unsigned(unsigned int ui)
{
	if (ui < -5.0) {
	}

	/* expect+1: warning: operator '<' compares 'unsigned int' with 'negative constant' [162] */
	if (ui < -5) {
	}

	/* expect+1: warning: operator '<' compares 'unsigned int' with '0' [162] */
	if (ui < 0) {
	}

	/* expect+1: warning: operator '>=' compares 'unsigned int' with '0' [162] */
	if (ui >= 0) {
	}

	/* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */
	if (ui <= 0) {
	}
}

void
right_unsigned(unsigned int ui)
{

	if (-5.0 > ui) {
	}

	/* expect+1: warning: operator '>' compares 'negative constant' with 'unsigned int' [162] */
	if (-5 > ui) {
	}

	/* expect+1: warning: operator '>' compares '0' with 'unsigned int' [162] */
	if (0 > ui) {
	}

	/* expect+1: warning: operator '<=' compares '0' with 'unsigned int' [162] */
	if (0 <= ui) {
	}

	/* before 2021-09-05: comparison of 0 with unsigned int, op >= [162] */
	if (0 >= ui) {
	}
}

/*
 * Lint does not care about these comparisons, even though they are obviously
 * out of range.
 */
void
compare_signed_char(signed char sc)
{
	if (sc == -129)
		return;
	if (sc == -128)
		return;
	if (sc == 127)
		return;
	if (sc == 128)
		return;
}

void
compare_unsigned_char(unsigned char uc)
{
	/* expect+1: warning: operator '==' compares 'unsigned char' with 'negative constant' [162] */
	if (uc == -1)
		return;
	if (uc == 0)
		return;
	if (uc == 255)
		return;
	if (uc == 256)
		return;
}

void take_bool(_Bool);

void
compare_operators(unsigned int x)
{
	/* expect+1: warning: operator '<' compares 'unsigned int' with 'negative constant' [162] */
	take_bool(x < -1);
	/* expect+1: warning: operator '<' compares 'unsigned int' with '0' [162] */
	take_bool(x < 0);
	take_bool(x < 1);

	/* expect+1: warning: operator '<=' compares 'unsigned int' with 'negative constant' [162] */
	take_bool(x <= -1);
	/*
	 * Before tree.c 1.379 from 2021-09-05, lint warned about
	 * 'unsigned <= 0' as well as '0 >= unsigned'.  In all cases where
	 * the programmer knows whether the underlying data type is signed or
	 * unsigned, it is clearer to express the same thought as
	 * 'unsigned == 0', but that's a stylistic issue only.
	 *
	 * Removing this particular case of the warning is not expected to
	 * miss any bugs.  The expression 'x <= 0' is equivalent to 'x < 1',
	 * so lint should not warn about it, just as it doesn't warn about
	 * the inverted condition, which is 'x > 0'.
	 */
	/* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */
	take_bool(x <= 0);
	take_bool(x <= 1);

	/* expect+1: warning: operator '>' compares 'unsigned int' with 'negative constant' [162] */
	take_bool(x > -1);
	take_bool(x > 0);
	take_bool(x > 1);

	/* expect+1: warning: operator '>=' compares 'unsigned int' with 'negative constant' [162] */
	take_bool(x >= -1);
	/* expect+1: warning: operator '>=' compares 'unsigned int' with '0' [162] */
	take_bool(x >= 0);
	take_bool(x >= 1);

	/* expect+1: warning: operator '==' compares 'unsigned int' with 'negative constant' [162] */
	take_bool(x == -1);
	take_bool(x == 0);
	take_bool(x == 1);

	/* expect+1: warning: operator '!=' compares 'unsigned int' with 'negative constant' [162] */
	take_bool(x != -1);
	take_bool(x != 0);
	take_bool(x != 1);
}