Home | History | Annotate | Line # | Download | only in lint1
      1 /*	$NetBSD: msg_220.c,v 1.11 2025/05/08 20:51:41 rillig Exp $	*/
      2 # 3 "msg_220.c"
      3 
      4 // Test for message: fallthrough on case statement [220]
      5 
      6 /* lint1-extra-flags: -h -X 351 */
      7 
      8 extern void
      9 println(const char *);
     10 
     11 void
     12 example(int n)
     13 {
     14 	switch (n) {
     15 	case 1:
     16 	case 3:
     17 	case 5:
     18 		println("odd");
     19 		/* expect+1: warning: fallthrough on case statement [220] */
     20 	case 2:
     21 	case 7:
     22 		println("prime");
     23 		/* expect+1: warning: fallthrough on default statement [284] */
     24 	default:
     25 		println("number");
     26 	}
     27 }
     28 
     29 void
     30 example1(int n)
     31 {
     32 	switch (n) {
     33 	case 1:
     34 	case 3:
     35 	case 5:
     36 		println("odd");
     37 		__attribute__((__fallthrough__));
     38 	case 2:
     39 	case 7:
     40 		println("prime");
     41 		__attribute__((__fallthrough__));
     42 	default:
     43 		println("number");
     44 	}
     45 }
     46 
     47 /* https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wimplicit-fallthrough */
     48 void
     49 annotation_comment_variations(int n)
     50 {
     51 	switch (n) {
     52 	case 0:
     53 		println("0");
     54 		/* FALLTHROUGH */
     55 	case 1:
     56 		println("1");
     57 		/* Seen in libarchive/archive_string.c, macro WRITE_UC. */
     58 		/* FALL THROUGH */
     59 		/* Lint warned before lex.c 1.79 from 2021-08-29. */
     60 	case 2:
     61 		println("2");
     62 		/* FALLS THROUGH */
     63 		/* expect+1: warning: fallthrough on case statement [220] */
     64 	case 3:
     65 		println("3");
     66 		/* intentionally falls through */
     67 		/* expect+1: warning: fallthrough on case statement [220] */
     68 	case 4:
     69 		println("4");
     70 		/* This is the Splint variant, which is seldom used. */
     71 		/* @fallthrough@ */
     72 		/* expect+1: warning: fallthrough on case statement [220] */
     73 	case 5:
     74 		println("5");
     75 		/* Seen in unbound/lookup3.c, function hashlittle. */
     76 		/* Lint warned before lex.c 1.80 from 2021-08-29. */
     77 		/* fallthrough */
     78 	case 6:
     79 		println("6");
     80 	}
     81 }
     82 
     83 
     84 void do_return(void);
     85 void noreturn(void)
     86     __attribute__((__noreturn__));
     87 void noreturn_attr_comma(void)
     88     __attribute__((__unused__, __noreturn__));
     89 void noreturn_attr_attr(void)
     90     __attribute__((__unused__))
     91     __attribute__((__noreturn__));
     92 
     93 void
     94 call_noreturn(void)
     95 {
     96 	switch (0) {
     97 	case 0:
     98 		do_return();
     99 		/* expect+1: warning: fallthrough on case statement [220] */
    100 	case 1:
    101 		noreturn();
    102 	case 2:
    103 		noreturn_attr_comma();
    104 	case 3:
    105 		noreturn_attr_attr();
    106 	case 4:
    107 		return;
    108 	}
    109 }
    110