| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: msg_160.c,v 1.6 2021/10/09 21:25:39 rillig Exp $ */ | | 1 | /* $NetBSD: msg_160.c,v 1.7 2022/05/18 19:25:12 rillig Exp $ */ |
2 | # 3 "msg_160.c" | | 2 | # 3 "msg_160.c" |
3 | | | 3 | |
4 | // Test for message: operator '==' found where '=' was expected [160] | | 4 | // Test for message: operator '==' found where '=' was expected [160] |
5 | | | 5 | |
6 | /* lint1-extra-flags: -h */ | | 6 | /* lint1-extra-flags: -h */ |
7 | | | 7 | |
8 | _Bool | | 8 | _Bool |
9 | both_equal_or_unequal(int a, int b, int c, int d) | | 9 | both_equal_or_unequal(int a, int b, int c, int d) |
10 | { | | 10 | { |
11 | /* | | 11 | /* |
12 | * Before tree.c 1.201 from 2021-01-31, lint warned about each of | | 12 | * Before tree.c 1.201 from 2021-01-31, lint warned about each of |
13 | * the '==' subexpressions even though there is nothing surprising | | 13 | * the '==' subexpressions even though there is nothing surprising |
14 | * about them. | | 14 | * about them. |
| @@ -47,24 +47,58 @@ unparenthesized(int a, int b, int c, _Bo | | | @@ -47,24 +47,58 @@ unparenthesized(int a, int b, int c, _Bo |
47 | * Before tree.c 1.201 from 2021-01-31, lint warned about the | | 47 | * Before tree.c 1.201 from 2021-01-31, lint warned about the |
48 | * parenthesized '==' subexpression even though there is nothing | | 48 | * parenthesized '==' subexpression even though there is nothing |
49 | * surprising about it. | | 49 | * surprising about it. |
50 | */ | | 50 | */ |
51 | eval((a == b) == c); | | 51 | eval((a == b) == c); |
52 | /* | | 52 | /* |
53 | * Before tree.c 1.201 from 2021-01-31, lint warned about the | | 53 | * Before tree.c 1.201 from 2021-01-31, lint warned about the |
54 | * parenthesized '==' subexpression even though there is nothing | | 54 | * parenthesized '==' subexpression even though there is nothing |
55 | * surprising about it. | | 55 | * surprising about it. |
56 | */ | | 56 | */ |
57 | eval(a == (b == c)); | | 57 | eval(a == (b == c)); |
58 | } | | 58 | } |
59 | | | 59 | |
60 | /* Seen in bin/csh/dir.c 1.35 from 2020-08-09, line 223. */ | | | |
61 | void | | 60 | void |
62 | assignment_in_comma_expression(void) | | 61 | assignment_in_comma_expression(int len) |
63 | { | | 62 | { |
64 | int len; | | | |
65 | | | 63 | |
| | | 64 | /* |
| | | 65 | * No extra parentheses, just a comma operator. |
| | | 66 | * |
| | | 67 | * The usual interpretation is that the left-hand operand of the |
| | | 68 | * comma is a preparation, most often an assignment, and the |
| | | 69 | * right-hand operand of the comma is the actual condition. |
| | | 70 | */ |
| | | 71 | /* FIXME: The following code is totally fine. */ |
| | | 72 | /* expect+1: warning: operator '==' found where '=' was expected [160] */ |
| | | 73 | if (len = 3 * len + 1, len == 0) |
| | | 74 | return; |
| | | 75 | |
| | | 76 | /* Seen in bin/csh/dir.c 1.35 from 2020-08-09, line 223. */ |
| | | 77 | /* |
| | | 78 | * The extra parentheses are typically used to inform the compiler |
| | | 79 | * that an assignment using '=' is intentional, in particular it is |
| | | 80 | * not a typo of the comparison operator '=='. |
| | | 81 | * |
| | | 82 | * The comma operator in a condition is seldom used, which makes it |
| | | 83 | * reasonable to assume that the code author selected the operators |
| | | 84 | * on purpose. |
| | | 85 | * |
| | | 86 | * In this case the parentheses are redundant, it's quite possible |
| | | 87 | * that they come from a macro expansion though. |
| | | 88 | */ |
| | | 89 | /* FIXME: The following code is totally fine. */ |
| | | 90 | /* expect+1: warning: operator '==' found where '=' was expected [160] */ |
| | | 91 | if ((len = 3 * len + 1, len == 0)) |
| | | 92 | return; |
| | | 93 | |
| | | 94 | /* |
| | | 95 | * If the comma expression is part of a larger expression, the |
| | | 96 | * parentheses are required to mark the operator precedence. The |
| | | 97 | * parentheses must therefore not be interpreted as changing the |
| | | 98 | * intention from a condition to an assignment. |
| | | 99 | */ |
66 | /* FIXME: The following code is totally fine. */ | | 100 | /* FIXME: The following code is totally fine. */ |
67 | /* expect+1: warning: operator '==' found where '=' was expected [160] */ | | 101 | /* expect+1: warning: operator '==' found where '=' was expected [160] */ |
68 | if ((len = 3, len == 0)) | | 102 | if ((len = 3 * len + 1, len == 0) && len < 2) |
69 | return; | | 103 | return; |
70 | } | | 104 | } |