Home | History | Annotate | Line # | Download | only in lint1
d_c99_bool_strict.c revision 1.54
      1 /*	$NetBSD: d_c99_bool_strict.c,v 1.54 2025/04/10 20:37:48 rillig Exp $	*/
      2 # 3 "d_c99_bool_strict.c"
      3 
      4 /*
      5  * The option -T treats _Bool as incompatible with all other scalar types.
      6  * This is implemented by the following rules:
      7  *
      8  * strict-bool-typedef:
      9  *	The type _Bool is compatible with any typedef of _Bool.
     10  *
     11  *	Note: Since <stdbool.h> defines bool as textual alias of _Bool,
     12  *	having another typedef for bool is unusual.
     13  *
     14  * strict-bool-constant:
     15  *	There are 2 bool constants named false and true.
     16  *	No other constants are compatible with type _Bool.
     17  *
     18  *	Note: Internally these constants are named __lint_false and
     19  *	__lint_true.
     20  *
     21  * strict-bool-bit-field:
     22  *	A struct or union member that is a bit field with underlying type
     23  *	bool is compatible with plain bool.
     24  *
     25  * strict-bool-conversion:
     26  *	There is no implicit conversion between _Bool and any other type.
     27  *
     28  * strict-bool-controlling-expression:
     29  *	Controlling expressions in 'if', 'while', 'for', '?:' must be of
     30  *	type bool, except for a literal 0 in a do-while loop.
     31  *
     32  * strict-bool-operand-unary:
     33  *	Operator	bool?	scalar?
     34  *	!		yes	-
     35  *	&		yes	yes
     36  *	The other unary operators do not accept bool operands.
     37  *
     38  * strict-bool-operand-binary:
     39  *	Operator	left:	bool?	other?	right:	bool?	other?
     40  *	.			-	yes		yes	yes
     41  *	->			-	yes		yes	yes
     42  *	<=, <, >=, >		-	yes		-	yes
     43  *	==, !=			yes	yes		yes	yes
     44  *	&			yes	yes		yes	yes
     45  *	^			yes	yes		yes	yes
     46  *	|			yes	yes		yes	yes
     47  *	&&			yes	-		yes	-
     48  *	||			yes	-		yes	-
     49  *	?			yes	-		yes	yes
     50  *	:			yes	yes		yes	yes
     51  *	=			yes	yes		yes	yes
     52  *	&=, ^=, |=		yes	yes		yes	yes
     53  *	,			yes	yes		yes	yes
     54  *	The other binary operators do not accept bool operands.
     55  *
     56  * strict-bool-operator-result:
     57  *	The result type of the operators '!', '<', '<=', '>', '>=',
     58  *	'==', '!=', '&&', '||' is _Bool instead of int.
     59  *
     60  * strict-bool-bitwise-and:
     61  *	Expressions of the form "flags & FLAG" are compatible with _Bool if
     62  *	the resulting value is used in a context where it is implicitly and
     63  *	immediately compared to zero.
     64  *
     65  *	Note: Examples for such contexts are controlling expressions or the
     66  *	operands of the operators '!', '&&', '||'.
     67  *
     68  *	Note: Counterexamples for contexts are assignments to a bool variable,
     69  *	as without the conversion from C99 6.3.1.2, converting an integer to a
     70  *	"bool-like" integer type truncated the value instead of comparing it
     71  *	to 0.
     72  *
     73  *	Note: These rules ensure that conforming code behaves the same in both
     74  *	C99 and in environments that emulate a boolean type using a small
     75  *	integer type.
     76  */
     77 
     78 /*
     79  * The header <stdbool.h> defines the macros bool = _Bool, false = 0 and
     80  * true = 1.  Without further hacks, this would mean that constant expressions
     81  * of integer type have to be regarded as possible boolean constants if their
     82  * value is either 0 or 1.
     83  *
     84  * This would not help in migrating old code to use bool consistently.
     85  * Therefore lint provides its own <stdbool.h> header that expands false to
     86  * __lint_false and true to __lint_true, two predefined constant expressions.
     87  */
     88 
     89 /* lint1-extra-flags: -hT -X 351 */
     90 
     91 /*
     92  * strict-bool-typedef
     93  */
     94 
     95 /*
     96  * Using a typedef for bool does not hurt the checks, they all use the
     97  * underlying basic type (see tspec_t), which is BOOL.
     98  */
     99 typedef _Bool bool;
    100 
    101 extern void accept_bool(bool);
    102 extern void println(const char *);
    103 extern void take_arguments(bool, int, const char *, ...);
    104 extern void do_nothing(void);
    105 
    106 /*
    107  * strict-bool-constant
    108  */
    109 
    110 void
    111 strict_bool_constant(void)
    112 {
    113 	accept_bool(__lint_false);
    114 	accept_bool(__lint_true);
    115 	/* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
    116 	accept_bool(0);
    117 	/* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
    118 	accept_bool(1);
    119 	/* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
    120 	accept_bool(2);
    121 }
    122 
    123 enum strict_bool_constant_expressions {
    124 	/* Ok: __lint_false is a boolean constant expression. */
    125 	FALSE = __lint_false ? 100 : 101,
    126 
    127 	/* Ok: __lint_true is a boolean constant expression. */
    128 	TRUE = __lint_true ? 100 : 101,
    129 
    130 	/* Not ok: an integer is not a boolean constant expression. */
    131 	/* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
    132 	INT0 = 0 ? 100 : 101,
    133 
    134 	/* Not ok: an integer is not a boolean constant expression. */
    135 	/* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
    136 	INT1 = 1 ? 100 : 101,
    137 
    138 	/* Not ok: 2 is not a boolean constant. */
    139 	/* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
    140 	INT2 = 2 ? 100 : 101,
    141 
    142 	/* Not ok: compound integer expressions are not bool. */
    143 	/* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
    144 	ARITH = (2 - 2) ? 100 : 101,
    145 
    146 	/*
    147 	 * Without strict bool mode, these two variants of an expression can
    148 	 * occur when a preprocessor macro is either defined to 1 or left
    149 	 * empty (since C99).
    150 	 *
    151 	 * In strict bool mode, the resulting expression can be compared
    152 	 * against 0 to achieve the same effect (so +0 != 0 or 1 + 0 != 0).
    153 	 */
    154 	/* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
    155 	BINARY_PLUS = (1 + 0) ? 100 : 101,
    156 	/* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
    157 	UNARY_PLUS = (+0) ? 100 : 101,
    158 
    159 	/* The main operator '>' has return type bool. */
    160 	Q1 = (13 > 12) ? 100 : 101,
    161 
    162 	/*
    163 	 * The parenthesized expression has type int and thus cannot be
    164 	 * used as the controlling expression in the '?:' operator.
    165 	 */
    166 	/* expect+1: error: left operand of '?' must be bool, not 'int' [331] */
    167 	Q2 = (13 > 12 ? 1 : 7) ? 100 : 101,
    168 
    169 	/* expect+1: error: integral constant expression expected [55] */
    170 	BINAND_BOOL = __lint_false & __lint_true,
    171 	BINAND_INT = 0 & 1,
    172 
    173 	/* expect+1: error: integral constant expression expected [55] */
    174 	BINXOR_BOOL = __lint_false ^ __lint_true,
    175 	BINXOR_INT = 0 ^ 1,
    176 
    177 	/* expect+1: error: integral constant expression expected [55] */
    178 	BINOR_BOOL = __lint_false | __lint_true,
    179 	BINOR_INT = 0 | 1,
    180 
    181 	/* expect+1: error: integral constant expression expected [55] */
    182 	LOGOR_BOOL = __lint_false || __lint_true,
    183 	/* expect+2: error: left operand of '||' must be bool, not 'int' [331] */
    184 	/* expect+1: error: right operand of '||' must be bool, not 'int' [332] */
    185 	LOGOR_INT = 0 || 1,
    186 
    187 	/* expect+1: error: integral constant expression expected [55] */
    188 	LOGAND_BOOL = __lint_false && __lint_true,
    189 	/* expect+2: error: left operand of '&&' must be bool, not 'int' [331] */
    190 	/* expect+1: error: right operand of '&&' must be bool, not 'int' [332] */
    191 	LOGAND_INT = 0 && 1,
    192 };
    193 
    194 /*
    195  * strict-bool-bit-fields
    196  */
    197 
    198 void
    199 strict_bool_bit_fields(void)
    200 {
    201 	struct flags {
    202 		bool bool_flag: 1;
    203 		unsigned uint_flag: 1;
    204 	};
    205 
    206 	struct flags flags = { __lint_false, 0 };
    207 	struct flags *flags_ptr = &flags;
    208 	bool b;
    209 
    210 	b = flags.bool_flag;
    211 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'unsigned int' [107] */
    212 	b = flags.uint_flag;
    213 	flags.bool_flag = b;
    214 	/* expect+1: error: operands of '=' have incompatible types 'unsigned int' and '_Bool' [107] */
    215 	flags.uint_flag = b;
    216 
    217 	b = flags_ptr->bool_flag;
    218 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'unsigned int' [107] */
    219 	b = flags_ptr->uint_flag;
    220 	flags_ptr->bool_flag = b;
    221 	/* expect+1: error: operands of '=' have incompatible types 'unsigned int' and '_Bool' [107] */
    222 	flags_ptr->uint_flag = b;
    223 }
    224 
    225 void
    226 strict_bool_bit_fields_operand_conversion(void)
    227 {
    228 	struct s {
    229 		bool ordinary;
    230 		bool bit_field: 1;
    231 	};
    232 
    233 	struct s s = { 0 > 0 };
    234 
    235 	s.ordinary = s.ordinary | s.ordinary;
    236 	s.bit_field = s.bit_field | s.bit_field;
    237 }
    238 
    239 /*
    240  * strict-bool-conversion
    241  */
    242 
    243 bool
    244 strict_bool_conversion_return_false(void)
    245 {
    246 	return __lint_false;
    247 }
    248 
    249 bool
    250 strict_bool_conversion_return_true(void)
    251 {
    252 	return __lint_true;
    253 }
    254 
    255 bool
    256 strict_bool_conversion_return_bool(bool b)
    257 {
    258 	return b;
    259 }
    260 
    261 bool
    262 strict_bool_conversion_return_0(void)
    263 {
    264 	/* expect+1: error: function has return type '_Bool' but returns 'int' [211] */
    265 	return 0;
    266 }
    267 
    268 bool
    269 strict_bool_conversion_return_1(void)
    270 {
    271 	/* expect+1: error: function has return type '_Bool' but returns 'int' [211] */
    272 	return 1;
    273 }
    274 
    275 bool
    276 strict_bool_conversion_return_2(void)
    277 {
    278 	/* expect+1: error: function has return type '_Bool' but returns 'int' [211] */
    279 	return 2;
    280 }
    281 
    282 /* expect+2: warning: parameter 'p' unused in function 'strict_bool_conversion_return_pointer' [231] */
    283 bool
    284 strict_bool_conversion_return_pointer(const void *p)
    285 {
    286 	/* expect+1: error: function has return type '_Bool' but returns 'pointer' [211] */
    287 	return p;
    288 }
    289 
    290 char
    291 strict_bool_conversion_return_false_as_char(void)
    292 {
    293 	/* expect+1: error: function has return type 'char' but returns '_Bool' [211] */
    294 	return __lint_false;
    295 }
    296 
    297 char
    298 strict_bool_conversion_return_true_as_char(void)
    299 {
    300 	/* expect+1: error: function has return type 'char' but returns '_Bool' [211] */
    301 	return __lint_true;
    302 }
    303 
    304 
    305 void
    306 strict_bool_conversion_function_argument(void)
    307 {
    308 	accept_bool(__lint_false);
    309 	accept_bool(__lint_true);
    310 }
    311 
    312 void
    313 strict_bool_conversion_function_argument_pass(bool b, int i, const char *p)
    314 {
    315 	/* No conversion necessary. */
    316 	take_arguments(b, i, p);
    317 
    318 	/* Implicitly converting bool to other scalar types. */
    319 	/* expect+2: error: parameter 2 expects 'int', gets passed '_Bool' [334] */
    320 	/* expect+1: error: parameter 3 expects 'pointer', gets passed '_Bool' [334] */
    321 	take_arguments(b, b, b);
    322 
    323 	/* Implicitly converting int to bool (arg #1). */
    324 	/* expect+2: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
    325 	/* expect+1: warning: illegal combination of pointer 'pointer to const char' and integer 'int', arg #3 [154] */
    326 	take_arguments(i, i, i);
    327 
    328 	/* Implicitly converting pointer to bool (arg #1). */
    329 	/* expect+2: error: parameter 1 expects '_Bool', gets passed 'pointer' [334] */
    330 	/* expect+1: warning: illegal combination of integer 'int' and pointer 'pointer to const char', arg #2 [154] */
    331 	take_arguments(p, p, p);
    332 
    333 	/* Passing bool as vararg. */
    334 	/* TODO: maybe expect+1: arg#4 should not be bool but scalar */
    335 	take_arguments(b, i, p, b, i, p);
    336 
    337 	/* Passing a bool constant. */
    338 	take_arguments(__lint_false, i, p);
    339 
    340 	/* Passing a bool constant. */
    341 	take_arguments(__lint_true, i, p);
    342 
    343 	/* Trying to pass integer constants. */
    344 	/* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
    345 	take_arguments(0, i, p);
    346 	/* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
    347 	take_arguments(1, i, p);
    348 	/* expect+1: error: parameter 1 expects '_Bool', gets passed 'int' [334] */
    349 	take_arguments(2, i, p);
    350 }
    351 
    352 void
    353 strict_bool_conversion_between_bool_and_int(void)
    354 {
    355 	bool b;
    356 	int i;
    357 
    358 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
    359 	b = 0;
    360 	b = __lint_false;
    361 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
    362 	b = 1;
    363 	b = __lint_true;
    364 
    365 	i = 0;
    366 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    367 	i = __lint_false;
    368 	i = 1;
    369 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    370 	i = __lint_true;
    371 
    372 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    373 	i = b;
    374 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
    375 	b = i;
    376 }
    377 
    378 /* expect+2: warning: parameter 'b' unused in function 'strict_bool_conversion_from_bool_to_scalar' [231] */
    379 void
    380 strict_bool_conversion_from_bool_to_scalar(bool b)
    381 {
    382 	int i;
    383 	unsigned u;
    384 	double d;
    385 	void *p;
    386 
    387 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    388 	i = b;
    389 	/* expect+1: error: operands of '=' have incompatible types 'unsigned int' and '_Bool' [107] */
    390 	u = b;
    391 	/* expect+1: error: operands of '=' have incompatible types 'double' and '_Bool' [107] */
    392 	d = b;
    393 	/* expect+1: error: operands of '=' have incompatible types 'pointer' and '_Bool' [107] */
    394 	p = b;
    395 }
    396 
    397 /*
    398  * strict-bool-controlling-expression
    399  */
    400 
    401 void
    402 strict_bool_controlling_expression(bool b, int i, double d, const void *p)
    403 {
    404 	if (__lint_false)
    405 		/* expect+1: warning: 'call' statement not reached [193] */
    406 		do_nothing();
    407 
    408 	if (__lint_true)
    409 		do_nothing();
    410 
    411 	if (b)
    412 		do_nothing();
    413 
    414 	/* expect+1: error: controlling expression must be bool, not 'int' [333] */
    415 	if (0)
    416 		/* expect+1: warning: 'call' statement not reached [193] */
    417 		do_nothing();
    418 
    419 	/* expect+1: error: controlling expression must be bool, not 'int' [333] */
    420 	if (1)
    421 		do_nothing();
    422 
    423 	/* expect+1: error: controlling expression must be bool, not 'int' [333] */
    424 	if (2)
    425 		do_nothing();
    426 
    427 	/* Not allowed: There is no implicit conversion from scalar to bool. */
    428 	/* expect+1: error: controlling expression must be bool, not 'int' [333] */
    429 	if (i)
    430 		do_nothing();
    431 	if (i != 0)
    432 		do_nothing();
    433 
    434 	/* Not allowed: There is no implicit conversion from scalar to bool. */
    435 	/* expect+1: error: controlling expression must be bool, not 'double' [333] */
    436 	if (d)
    437 		do_nothing();
    438 	if (d != 0.0)
    439 		do_nothing();
    440 
    441 	/* Not allowed: There is no implicit conversion from scalar to bool. */
    442 	/* expect+1: error: controlling expression must be bool, not 'pointer' [333] */
    443 	if (p)
    444 		do_nothing();
    445 	if (p != (void *)0)
    446 		do_nothing();
    447 
    448 	// An endless loop. The preferred form is 'for (;;)' instead.
    449 	do {
    450 	} while (__lint_true);
    451 
    452 	// A do-once "loop", often used in statement macros.
    453 	/* expect+1: warning: loop not entered at top [207] */
    454 	do {
    455 	} while (__lint_false);
    456 
    457 	// This form is too unusual to be allowed in strict bool mode.
    458 	do {
    459 	/* expect+1: error: controlling expression must be bool, not 'int' [333] */
    460 	} while (1);
    461 
    462 	// Even though 0 is an integer instead of a bool, this idiom is so
    463 	// common that it is frequently used in system headers.  Since the
    464 	// Clang preprocessor does not mark each token as coming from a system
    465 	// header or from user code, this idiom can only be allowed everywhere
    466 	// or nowhere.
    467 	/* expect+1: warning: loop not entered at top [207] */
    468 	do {
    469 	} while (0);
    470 }
    471 
    472 /*
    473  * strict-bool-operand-unary
    474  */
    475 
    476 void
    477 strict_bool_operand_unary_not(void)
    478 {
    479 	bool b = __lint_false;
    480 
    481 	b = !b;
    482 	b = !!!b;
    483 	b = !__lint_false;
    484 	b = !__lint_true;
    485 
    486 	int i = 0;
    487 
    488 	/* expect+1: error: operand of '!' must be bool, not 'int' [330] */
    489 	i = !i;
    490 	/* expect+1: error: operand of '!' must be bool, not 'int' [330] */
    491 	i = !!!i;
    492 	/* expect+1: error: operand of '!' must be bool, not 'int' [330] */
    493 	i = !0;
    494 	/* expect+1: error: operand of '!' must be bool, not 'int' [330] */
    495 	i = !1;
    496 }
    497 
    498 void
    499 strict_bool_operand_unary_address(void)
    500 {
    501 	bool b = __lint_false;
    502 
    503 	/* Taking the address of a bool lvalue. */
    504 	bool *bp;
    505 	bp = &b;
    506 	*bp = b;
    507 	b = *bp;
    508 }
    509 
    510 /* see strict_bool_operand_unary_all below for the other unary operators. */
    511 
    512 /*
    513  * strict-bool-operand-binary
    514  */
    515 
    516 /*
    517  * Ensure that bool members can be accessed as usual.
    518  */
    519 void
    520 strict_bool_operand_binary_dot_arrow(void)
    521 {
    522 	struct bool_struct {
    523 		bool b;
    524 	};
    525 
    526 	/* Initialize and assign using boolean constants. */
    527 	bool b = __lint_false;
    528 	b = __lint_true;
    529 
    530 	/* Access a struct member using the '.' operator. */
    531 	struct bool_struct bs = { __lint_true };
    532 	b = bs.b;
    533 	bs.b = b;
    534 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
    535 	bs.b = 0;
    536 
    537 	/* Access a struct member using the '->' operator. */
    538 	struct bool_struct *bsp = &bs;
    539 	b = bsp->b;
    540 	bsp->b = b;
    541 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
    542 	bsp->b = 0;
    543 }
    544 
    545 int
    546 strict_bool_operand_binary(bool b, int i)
    547 {
    548 
    549 	/* The right-hand sides of these assignments are ok. */
    550 	b = !b;
    551 	b = b && b;
    552 	b = b || b;
    553 
    554 	/*
    555 	 * The right-hand sides of these assignments implicitly convert from
    556 	 * scalar to bool.
    557 	 */
    558 	/* expect+1: error: operand of '!' must be bool, not 'int' [330] */
    559 	b = !i;
    560 	/* expect+2: error: left operand of '&&' must be bool, not 'int' [331] */
    561 	/* expect+1: error: right operand of '&&' must be bool, not 'int' [332] */
    562 	b = i && i;
    563 	/* expect+2: error: left operand of '||' must be bool, not 'int' [331] */
    564 	/* expect+1: error: right operand of '||' must be bool, not 'int' [332] */
    565 	b = i || i;
    566 
    567 	/* expect+1: error: right operand of '&&' must be bool, not 'int' [332] */
    568 	b = b && 0;
    569 	/* expect+1: error: left operand of '&&' must be bool, not 'int' [331] */
    570 	b = 0 && b;
    571 	/* expect+1: error: right operand of '||' must be bool, not 'int' [332] */
    572 	b = b || 0;
    573 	/* expect+1: error: left operand of '||' must be bool, not 'int' [331] */
    574 	b = 0 || b;
    575 
    576 	return i;
    577 }
    578 
    579 void
    580 strict_bool_operand_unary_all(bool b)
    581 {
    582 	b = !b;
    583 	/* expect+1: error: operand of '~' must not be bool [335] */
    584 	b = ~b;
    585 	/* expect+1: error: operand of '++x' must not be bool [335] */
    586 	++b;
    587 	/* expect+1: error: operand of '--x' must not be bool [335] */
    588 	--b;
    589 	/* expect+1: error: operand of 'x++' must not be bool [335] */
    590 	b++;
    591 	/* expect+1: error: operand of 'x--' must not be bool [335] */
    592 	b--;
    593 	/* expect+1: error: operand of '+' must not be bool [335] */
    594 	b = +b;
    595 	/* expect+1: error: operand of '-' must not be bool [335] */
    596 	b = -b;
    597 }
    598 
    599 void
    600 strict_bool_operand_binary_all(bool b, unsigned u)
    601 {
    602 	/* expect+2: error: left operand of '*' must not be bool [336] */
    603 	/* expect+1: error: right operand of '*' must not be bool [337] */
    604 	b = b * b;
    605 	/* expect+2: error: left operand of '/' must not be bool [336] */
    606 	/* expect+1: error: right operand of '/' must not be bool [337] */
    607 	b = b / b;
    608 	/* expect+2: error: left operand of '%' must not be bool [336] */
    609 	/* expect+1: error: right operand of '%' must not be bool [337] */
    610 	b = b % b;
    611 	/* expect+2: error: left operand of '+' must not be bool [336] */
    612 	/* expect+1: error: right operand of '+' must not be bool [337] */
    613 	b = b + b;
    614 	/* expect+2: error: left operand of '-' must not be bool [336] */
    615 	/* expect+1: error: right operand of '-' must not be bool [337] */
    616 	b = b - b;
    617 	/* expect+2: error: left operand of '<<' must not be bool [336] */
    618 	/* expect+1: error: right operand of '<<' must not be bool [337] */
    619 	b = b << b;
    620 	/* expect+2: error: left operand of '>>' must not be bool [336] */
    621 	/* expect+1: error: right operand of '>>' must not be bool [337] */
    622 	b = b >> b;
    623 
    624 	/* expect+2: error: left operand of '<' must not be bool [336] */
    625 	/* expect+1: error: right operand of '<' must not be bool [337] */
    626 	b = b < b;
    627 	/* expect+2: error: left operand of '<=' must not be bool [336] */
    628 	/* expect+1: error: right operand of '<=' must not be bool [337] */
    629 	b = b <= b;
    630 	/* expect+2: error: left operand of '>' must not be bool [336] */
    631 	/* expect+1: error: right operand of '>' must not be bool [337] */
    632 	b = b > b;
    633 	/* expect+2: error: left operand of '>=' must not be bool [336] */
    634 	/* expect+1: error: right operand of '>=' must not be bool [337] */
    635 	b = b >= b;
    636 	b = b == b;
    637 	b = b != b;
    638 
    639 	b = b & b;
    640 	b = b ^ b;
    641 	b = b | b;
    642 	b = b && b;
    643 	b = b || b;
    644 	b = b ? b : b;
    645 
    646 	b = b;
    647 	/* expect+2: error: left operand of '*=' must not be bool [336] */
    648 	/* expect+1: error: right operand of '*=' must not be bool [337] */
    649 	b *= b;
    650 	/* expect+2: error: left operand of '/=' must not be bool [336] */
    651 	/* expect+1: error: right operand of '/=' must not be bool [337] */
    652 	b /= b;
    653 	/* expect+2: error: left operand of '%=' must not be bool [336] */
    654 	/* expect+1: error: right operand of '%=' must not be bool [337] */
    655 	b %= b;
    656 	/* expect+2: error: left operand of '+=' must not be bool [336] */
    657 	/* expect+1: error: right operand of '+=' must not be bool [337] */
    658 	b += b;
    659 	/* expect+2: error: left operand of '-=' must not be bool [336] */
    660 	/* expect+1: error: right operand of '-=' must not be bool [337] */
    661 	b -= b;
    662 	/* expect+2: error: left operand of '<<=' must not be bool [336] */
    663 	/* expect+1: error: right operand of '<<=' must not be bool [337] */
    664 	b <<= b;
    665 	/* expect+2: error: left operand of '>>=' must not be bool [336] */
    666 	/* expect+1: error: right operand of '>>=' must not be bool [337] */
    667 	b >>= b;
    668 	b &= b;
    669 	b ^= b;
    670 	b |= b;
    671 
    672 	/* Operations with mixed types. */
    673 	/* expect+1: error: left operand of '*' must not be bool [336] */
    674 	u = b * u;
    675 	/* expect+1: error: right operand of '*' must not be bool [337] */
    676 	u = u * b;
    677 	/* expect+1: error: left operand of '/' must not be bool [336] */
    678 	u = b / u;
    679 	/* expect+1: error: right operand of '/' must not be bool [337] */
    680 	u = u / b;
    681 	/* expect+1: error: left operand of '%' must not be bool [336] */
    682 	u = b % u;
    683 	/* expect+1: error: right operand of '%' must not be bool [337] */
    684 	u = u % b;
    685 	/* expect+1: error: left operand of '+' must not be bool [336] */
    686 	u = b + u;
    687 	/* expect+1: error: right operand of '+' must not be bool [337] */
    688 	u = u + b;
    689 	/* expect+1: error: left operand of '-' must not be bool [336] */
    690 	u = b - u;
    691 	/* expect+1: error: right operand of '-' must not be bool [337] */
    692 	u = u - b;
    693 	/* expect+1: error: left operand of '<<' must not be bool [336] */
    694 	u = b << u;
    695 	/* expect+1: error: right operand of '<<' must not be bool [337] */
    696 	u = u << b;
    697 	/* expect+1: error: left operand of '>>' must not be bool [336] */
    698 	u = b >> u;
    699 	/* expect+1: error: right operand of '>>' must not be bool [337] */
    700 	u = u >> b;
    701 	u = b ? u : u;
    702 	/* expect+1: error: operands of ':' have incompatible types '_Bool' and 'unsigned int' [107] */
    703 	u = b ? b : u;
    704 	/* expect+1: error: operands of ':' have incompatible types 'unsigned int' and '_Bool' [107] */
    705 	u = b ? u : b;
    706 }
    707 
    708 bool
    709 strict_bool_operand_binary_comma(bool b, int i)
    710 {
    711 	/* expect+1: warning: expression has null effect [129] */
    712 	b = (b, !b);
    713 	/* expect+1: warning: expression has null effect [129] */
    714 	i = (i, i + 1);
    715 	return b;
    716 }
    717 
    718 /*
    719  * strict-bool-operator-result
    720  */
    721 
    722 void
    723 strict_bool_operator_result(bool b)
    724 {
    725 	/* expect+1: error: operands of 'init' have incompatible types 'char' and '_Bool' [107] */
    726 	char c = b;
    727 	/* expect+1: error: operands of 'init' have incompatible types 'int' and '_Bool' [107] */
    728 	int i = b;
    729 	/* expect+1: error: operands of 'init' have incompatible types 'double' and '_Bool' [107] */
    730 	double d = b;
    731 	/* expect+1: error: operands of 'init' have incompatible types 'pointer' and '_Bool' [107] */
    732 	void *p = b;
    733 
    734 	/* The right-hand sides of these assignments are all ok. */
    735 	b = !b;
    736 	b = i == i;
    737 	b = i != i;
    738 	b = i < i;
    739 	b = i <= i;
    740 	b = i >= i;
    741 	b = i > i;
    742 	b = b && b;
    743 	b = b || b;
    744 
    745 	/*
    746 	 * The right-hand sides of these assignments are not ok, they
    747 	 * implicitly convert from bool to int.
    748 	 */
    749 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    750 	i = !b;
    751 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    752 	i = i == i;
    753 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    754 	i = i != i;
    755 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    756 	i = i < i;
    757 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    758 	i = i <= i;
    759 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    760 	i = i >= i;
    761 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    762 	i = i > i;
    763 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    764 	i = b && b;
    765 	/* expect+1: error: operands of '=' have incompatible types 'int' and '_Bool' [107] */
    766 	i = b || b;
    767 }
    768 
    769 
    770 /*
    771  * strict-bool-bitwise-and
    772  */
    773 
    774 enum Flags {
    775 	FLAG0 = 1 << 0,
    776 	FLAG1 = 1 << 1,
    777 	FLAG28 = 1 << 28
    778 };
    779 
    780 /* expect+2: warning: parameter 'flags' unused in function 'strict_bool_bitwise_and_enum' [231] */
    781 void
    782 strict_bool_bitwise_and_enum(enum Flags flags)
    783 {
    784 	bool b;
    785 
    786 	/*
    787 	 * FLAG0 has the value 1 and thus can be stored in a bool variable
    788 	 * without truncation.  Nevertheless this special case is not allowed
    789 	 * because it would be too confusing if FLAG0 would work and all the
    790 	 * other flags wouldn't.
    791 	 */
    792 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
    793 	b = flags & FLAG0;
    794 
    795 	/*
    796 	 * Assuming that FLAG1 is set in flags, a _Bool variable stores this
    797 	 * as 1, as defined by C99 6.3.1.2.  A uint8_t variable would store
    798 	 * it as 2, as that is the integer value of FLAG1.  Since FLAG1 fits
    799 	 * in a uint8_t, no truncation takes place.
    800 	 */
    801 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
    802 	b = flags & FLAG1;
    803 
    804 	/*
    805 	 * In a _Bool variable, FLAG28 is stored as 1, since it is unequal to
    806 	 * zero.  In a uint8_t, the stored value would be 0 since bit 28 is
    807 	 * out of range for a uint8_t and thus gets truncated.
    808 	 */
    809 	/* expect+1: error: operands of '=' have incompatible types '_Bool' and 'int' [107] */
    810 	b = flags & FLAG28;
    811 }
    812 
    813 /*
    814  * Demonstrate idiomatic code to query flags from an enum bit set.
    815  *
    816  * In all the controlling expressions in this function, the result of the
    817  * operator '&' is compared against 0.  This makes this pattern work, no
    818  * matter whether the bits are in the low-value range or in the high-value
    819  * range (such as FLAG28, which has the value 1073741824, which is more than
    820  * what would fit into an unsigned char).  Even if an enum could be extended
    821  * to larger types than int, this pattern would work.
    822  */
    823 bool
    824 query_flag_from_enum_bit_set(enum Flags flags)
    825 {
    826 	if (flags & FLAG0)
    827 		println("FLAG0 is set");
    828 
    829 	if ((flags & FLAG1) != 0)
    830 		println("FLAG1 is set");
    831 
    832 	if ((flags & (FLAG0 | FLAG1)) == (FLAG0 | FLAG1))
    833 		println("FLAG0 and FLAG1 are both set");
    834 
    835 	if (flags & FLAG0 && flags & FLAG1)
    836 		println("FLAG0 and FLAG1 are both set");
    837 
    838 	if ((flags & (FLAG0 | FLAG1)) != 0)
    839 		println("At least one of FLAG0 and FLAG1 is set");
    840 
    841 	if (flags & FLAG28)
    842 		println("FLAG28 is set");
    843 
    844 	/* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
    845 	bool b0 = flags & FLAG0;
    846 	/* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
    847 	bool b1 = flags & FLAG1;
    848 	/* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
    849 	bool b28 = flags & FLAG28;
    850 	return b0 || b1 || b28;
    851 }
    852 
    853 bool
    854 query_flag_from_int(int flags)
    855 {
    856 
    857 	if (flags & FLAG0)
    858 		println("FLAG0 is set");
    859 
    860 	if ((flags & FLAG1) != 0)
    861 		println("FLAG1 is set");
    862 
    863 	if ((flags & (FLAG0 | FLAG1)) == (FLAG0 | FLAG1))
    864 		println("FLAG0 and FLAG1 are both set");
    865 
    866 	if (flags & FLAG0 && flags & FLAG1)
    867 		println("FLAG0 and FLAG1 are both set");
    868 
    869 	if ((flags & (FLAG0 | FLAG1)) != 0)
    870 		println("At least one of FLAG0 and FLAG1 is set");
    871 
    872 	if (flags & FLAG28)
    873 		println("FLAG28 is set");
    874 
    875 	/* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
    876 	bool b0 = flags & FLAG0;
    877 	/* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
    878 	bool b1 = flags & FLAG1;
    879 	/* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
    880 	bool b28 = flags & FLAG28;
    881 	return b0 || b1 || b28;
    882 }
    883 
    884 
    885 void
    886 strict_bool_operator_eq_bool_int(void)
    887 {
    888 	/* expect+1: error: operands of '==' have incompatible types '_Bool' and 'int' [107] */
    889 	(void)(strict_bool_conversion_return_false() == 0);
    890 }
    891 
    892 void
    893 strict_bool_assign_bit_field_then_compare(void)
    894 {
    895 	struct s {
    896 		bool flag: 1;
    897 	};
    898 
    899 	struct s s = { __lint_false };
    900 
    901 	/* expect+1: warning: expression has null effect [129] */
    902 	(void)((s.flag = s.flag) != __lint_false);
    903 }
    904 
    905 void
    906 bool_as_array_index(bool cond)
    907 {
    908 	static const char *repr[] = { "no", "yes" };
    909 	/*
    910 	 * The '+' in the error message reveals that lint internally
    911 	 * translates 'arr[ind]' to '*(arr + ind)' in an early stage of
    912 	 * parsing.
    913 	 */
    914 	/* expect+1: error: right operand of '+' must not be bool [337] */
    915 	println(repr[cond]);
    916 	println(cond ? "yes" : "no");
    917 }
    918 
    919 void
    920 initialization(void)
    921 {
    922 	struct {
    923 		_Bool b;
    924 	} var[] = {
    925 	    { __lint_false },
    926 	    { __lint_true },
    927 	    /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
    928 	    { 0 },
    929 	    /* expect+1: error: operands of 'init' have incompatible types '_Bool' and 'int' [107] */
    930 	    { 1 },
    931 	};
    932 }
    933