summaryrefslogtreecommitdiff
path: root/tests/usr.bin/xlint/lint1/msg_338.c
blob: 777b2b7928e8440cb9d0f3505882fa7b177ecbdd (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*	$NetBSD: msg_338.c,v 1.9 2023/03/28 14:44:35 rillig Exp $	*/
# 3 "msg_338.c"

// Test for message: option '%c' should be handled in the switch [338]

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

int getopt(int, char *const *, const char *);
extern char *optarg;

int
main(int argc, char **argv)
{
	int o;

	/* expect+2: warning: option 'c' should be handled in the switch [338] */
	/* expect+1: warning: option 'd' should be handled in the switch [338] */
	while ((o = getopt(argc, argv, "a:bc:d")) != -1) {
		switch (o) {
		case 'a':
			break;
		case 'b':
			/*
			 * The following while loop must not finish the check
			 * for the getopt options.
			 */
			while (optarg[0] != '\0')
				optarg++;
			break;
		case 'e':
			/* expect-1: warning: option 'e' should be listed in the options string [339] */
			break;
		case 'f':
			/* expect-1: warning: option 'f' should be listed in the options string [339] */
			/*
			 * The case labels in nested switch statements are
			 * ignored by the check for getopt options.
			 */
			switch (optarg[0]) {
			case 'X':
				break;
			}
			break;
		case '?':
		default:
			break;
		}
	}

	/* A while loop that is not related to getopt is simply skipped. */
	while (o != 0) {
		switch (o) {
		case '?':
			o = ':';
		}
	}

	return 0;
}

void usage(void);

/*
 * Before ckgetopt.c 1.11 from 2021-08-23, lint wrongly warned about a
 * missing '?' in the switch statement, even though it was there.
 *
 * Seen in usr.bin/ftp/main.c 1.127 from 2020-07-18.
 */
int
question_option(int argc, char **argv)
{
	int c;

	while ((c = getopt(argc, argv, "?x")) != -1) {
		switch (c) {
		case 'x':
			break;
		case '?':
			usage();
			return 0;
		default:
			usage();
			return 1;
		}
	}
	return 0;
}

/*
 * If the first character of the options string is ':', getopt does not print
 * its own error messages. Getopt returns ':' if an option is missing its
 * argument; that is handled by the 'default:' already.
 */
int
suppress_errors(int argc, char **argv)
{
	int c;

	/* expect+1: warning: option 'o' should be handled in the switch [338] */
	while ((c = getopt(argc, argv, ":b:o")) != -1) {
		switch (c) {
		case 'b':
			return 'b';
		default:
			usage();
		}
	}
	return 0;
}

/*
 * If the first character of the options string is ':', getopt returns ':'
 * if an option is missing its argument. This condition can be handled
 * separately from '?', which getopt returns for unknown options.
 */
int
missing_argument(int argc, char **argv)
{
	int c;

	/* expect+1: warning: option 'o' should be handled in the switch [338] */
	while ((c = getopt(argc, argv, ":b:o")) != -1) {
		switch (c) {
		case 'b':
			return 'b';
		case ':':
			return 'm';
		default:
			usage();
		}
	}
	return 0;
}

/*
 * Getopt only returns ':' if ':' is the first character in the options
 * string. Everywhere else, a ':' marks the preceding option as having a
 * required argument. In theory, if the options string contained "a::x",
 * that could be interpreted as '-a argument', followed by '-:' and '-x',
 * but nobody does that.
 */
int
unreachable_colon(int argc, char **argv)
{
	int c;

	/* expect+1: warning: option 'b' should be handled in the switch [338] */
	while ((c = getopt(argc, argv, "b:")) != -1) {
		switch (c) {
		/* expect+1: warning: option ':' should be listed in the options string [339] */
		case ':':
			return 'm';
		default:
			usage();
		}
	}
	return 0;
}