Home | History | Annotate | Line # | Download | only in lint1
msg_162.c revision 1.7
      1  1.7  rillig /*	$NetBSD: msg_162.c,v 1.7 2022/06/22 19:23:18 rillig Exp $	*/
      2  1.1  rillig # 3 "msg_162.c"
      3  1.1  rillig 
      4  1.7  rillig // Test for message: operator '%s' compares '%s' with '%s' [162]
      5  1.1  rillig 
      6  1.3  rillig /* lint1-extra-flags: -hp */
      7  1.3  rillig 
      8  1.3  rillig void
      9  1.3  rillig left_unsigned(unsigned int ui)
     10  1.3  rillig {
     11  1.3  rillig 	if (ui < -5.0) {
     12  1.3  rillig 	}
     13  1.3  rillig 
     14  1.7  rillig 	/* expect+1: warning: operator '<' compares 'unsigned int' with 'negative constant' [162] */
     15  1.3  rillig 	if (ui < -5) {
     16  1.3  rillig 	}
     17  1.3  rillig 
     18  1.7  rillig 	/* expect+1: warning: operator '<' compares 'unsigned int' with '0' [162] */
     19  1.3  rillig 	if (ui < 0) {
     20  1.3  rillig 	}
     21  1.3  rillig 
     22  1.7  rillig 	/* expect+1: warning: operator '>=' compares 'unsigned int' with '0' [162] */
     23  1.3  rillig 	if (ui >= 0) {
     24  1.3  rillig 	}
     25  1.3  rillig 
     26  1.6  rillig 	/* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */
     27  1.3  rillig 	if (ui <= 0) {
     28  1.3  rillig 	}
     29  1.3  rillig }
     30  1.3  rillig 
     31  1.3  rillig void
     32  1.3  rillig right_unsigned(unsigned int ui)
     33  1.3  rillig {
     34  1.3  rillig 
     35  1.3  rillig 	if (-5.0 > ui) {
     36  1.3  rillig 	}
     37  1.3  rillig 
     38  1.7  rillig 	/* expect+1: warning: operator '>' compares 'negative constant' with 'unsigned int' [162] */
     39  1.3  rillig 	if (-5 > ui) {
     40  1.3  rillig 	}
     41  1.3  rillig 
     42  1.7  rillig 	/* expect+1: warning: operator '>' compares '0' with 'unsigned int' [162] */
     43  1.3  rillig 	if (0 > ui) {
     44  1.3  rillig 	}
     45  1.3  rillig 
     46  1.7  rillig 	/* expect+1: warning: operator '<=' compares '0' with 'unsigned int' [162] */
     47  1.3  rillig 	if (0 <= ui) {
     48  1.3  rillig 	}
     49  1.3  rillig 
     50  1.6  rillig 	/* before 2021-09-05: comparison of 0 with unsigned int, op >= [162] */
     51  1.3  rillig 	if (0 >= ui) {
     52  1.3  rillig 	}
     53  1.3  rillig }
     54  1.4  rillig 
     55  1.4  rillig /*
     56  1.4  rillig  * Lint does not care about these comparisons, even though they are obviously
     57  1.4  rillig  * out of range.
     58  1.4  rillig  */
     59  1.4  rillig void
     60  1.4  rillig compare_signed_char(signed char sc)
     61  1.4  rillig {
     62  1.4  rillig 	if (sc == -129)
     63  1.4  rillig 		return;
     64  1.4  rillig 	if (sc == -128)
     65  1.4  rillig 		return;
     66  1.4  rillig 	if (sc == 127)
     67  1.4  rillig 		return;
     68  1.4  rillig 	if (sc == 128)
     69  1.4  rillig 		return;
     70  1.4  rillig }
     71  1.4  rillig 
     72  1.4  rillig void
     73  1.4  rillig compare_unsigned_char(unsigned char uc)
     74  1.4  rillig {
     75  1.7  rillig 	/* expect+1: warning: operator '==' compares 'unsigned char' with 'negative constant' [162] */
     76  1.4  rillig 	if (uc == -1)
     77  1.4  rillig 		return;
     78  1.4  rillig 	if (uc == 0)
     79  1.4  rillig 		return;
     80  1.4  rillig 	if (uc == 255)
     81  1.4  rillig 		return;
     82  1.4  rillig 	if (uc == 256)
     83  1.4  rillig 		return;
     84  1.4  rillig }
     85  1.5  rillig 
     86  1.5  rillig void take_bool(_Bool);
     87  1.5  rillig 
     88  1.5  rillig void
     89  1.5  rillig compare_operators(unsigned int x)
     90  1.5  rillig {
     91  1.7  rillig 	/* expect+1: warning: operator '<' compares 'unsigned int' with 'negative constant' [162] */
     92  1.5  rillig 	take_bool(x < -1);
     93  1.7  rillig 	/* expect+1: warning: operator '<' compares 'unsigned int' with '0' [162] */
     94  1.5  rillig 	take_bool(x < 0);
     95  1.5  rillig 	take_bool(x < 1);
     96  1.5  rillig 
     97  1.7  rillig 	/* expect+1: warning: operator '<=' compares 'unsigned int' with 'negative constant' [162] */
     98  1.5  rillig 	take_bool(x <= -1);
     99  1.5  rillig 	/*
    100  1.6  rillig 	 * Before tree.c 1.379 from 2021-09-05, lint warned about
    101  1.6  rillig 	 * 'unsigned <= 0' as well as '0 >= unsigned'.  In all cases where
    102  1.6  rillig 	 * the programmer knows whether the underlying data type is signed or
    103  1.6  rillig 	 * unsigned, it is clearer to express the same thought as
    104  1.6  rillig 	 * 'unsigned == 0', but that's a stylistic issue only.
    105  1.6  rillig 	 *
    106  1.6  rillig 	 * Removing this particular case of the warning is not expected to
    107  1.6  rillig 	 * miss any bugs.  The expression 'x <= 0' is equivalent to 'x < 1',
    108  1.6  rillig 	 * so lint should not warn about it, just as it doesn't warn about
    109  1.6  rillig 	 * the inverted condition, which is 'x > 0'.
    110  1.5  rillig 	 */
    111  1.6  rillig 	/* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */
    112  1.5  rillig 	take_bool(x <= 0);
    113  1.5  rillig 	take_bool(x <= 1);
    114  1.5  rillig 
    115  1.7  rillig 	/* expect+1: warning: operator '>' compares 'unsigned int' with 'negative constant' [162] */
    116  1.5  rillig 	take_bool(x > -1);
    117  1.5  rillig 	take_bool(x > 0);
    118  1.5  rillig 	take_bool(x > 1);
    119  1.5  rillig 
    120  1.7  rillig 	/* expect+1: warning: operator '>=' compares 'unsigned int' with 'negative constant' [162] */
    121  1.5  rillig 	take_bool(x >= -1);
    122  1.7  rillig 	/* expect+1: warning: operator '>=' compares 'unsigned int' with '0' [162] */
    123  1.5  rillig 	take_bool(x >= 0);
    124  1.5  rillig 	take_bool(x >= 1);
    125  1.5  rillig 
    126  1.7  rillig 	/* expect+1: warning: operator '==' compares 'unsigned int' with 'negative constant' [162] */
    127  1.5  rillig 	take_bool(x == -1);
    128  1.5  rillig 	take_bool(x == 0);
    129  1.5  rillig 	take_bool(x == 1);
    130  1.5  rillig 
    131  1.7  rillig 	/* expect+1: warning: operator '!=' compares 'unsigned int' with 'negative constant' [162] */
    132  1.5  rillig 	take_bool(x != -1);
    133  1.5  rillig 	take_bool(x != 0);
    134  1.5  rillig 	take_bool(x != 1);
    135  1.5  rillig }
    136