xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_159.c (revision e6298b924c5ba98f3a22919b56dab04a87cdbb1c)
1 /*	$NetBSD: msg_159.c,v 1.7 2023/07/07 19:45:22 rillig Exp $	*/
2 # 3 "msg_159.c"
3 
4 // Test for message: assignment in conditional context [159]
5 
6 /* lint1-extra-flags: -h -X 351 */
7 
8 const char *
example(int a,int b)9 example(int a, int b)
10 {
11 
12 	if (a == b)
13 		return "comparison, not parenthesized";
14 
15 	/*
16 	 * Clang-Tidy marks a comparison with extra parentheses as an error,
17 	 * expecting that assignments are parenthesized and comparisons
18 	 * aren't.
19 	 */
20 	if ((a == b))
21 		return "comparison, parenthesized";
22 
23 	if (
24 # 25 "msg_159.c" 3 4
25 	    (a == b)
26 # 27 "msg_159.c"
27 	    )
28 		return "comparison, parenthesized, from system header";
29 
30 	/* expect+1: warning: assignment in conditional context [159] */
31 	if (a = b)
32 		return "assignment, not parenthesized";
33 
34 	/*
35 	 * GCC established the convention that an assignment that is
36 	 * parenthesized is intended as an assignment, so don't warn about
37 	 * that case.
38 	 */
39 	if ((a = b))
40 		return "assignment, parenthesized";
41 
42 	if ((a = b) != 0)
43 		return "explicit comparison after assignment";
44 
45 	return "other";
46 }
47