robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
87b42e1209
master
1// Test file to trigger all ERROR_CATEGORIES in clint.lua 2// This file contains intentional errors to test the linter 3 4#include <stdio.h> 5#include <stdlib.h> 6#include <time.h> 7 8// build/endif_comment: Uncommented text after #endif 9#ifdef SOME_CONDITION 10# define TEST 1 11#endif SOME_CONDITION 12 13// build/include_defs: Non-defs header included (but this is a .c file, so might not trigger) 14 15// build/printf_format: %q format specifier 16void test_printf_format () 17{ 18printf ("%q" ,"test" );// Should trigger runtime/printf_format 19} 20 21// build/storage_class: Storage class not first 22const static int x = 5 ;// Should trigger build/storage_class 23 24// readability/bool: Use TRUE/FALSE instead of true/false 25#define TRUE 1 26#define FALSE 0 27#define MAYBE 2 28 29void test_bool () 30{ 31int flag = TRUE;// Should trigger readability/bool 32if (flag == FALSE) {// Should trigger readability/bool 33printf ("false\n" ); 34 } 35int maybe_val = MAYBE ;// Should trigger readability/bool 36} 37 38// readability/multiline_comment: Complex multi-line comment 39void test_multiline_comment () 40{ 41/* This is a multi-line 42comment that spans 43multiple lines and doesn't close properly on the same line */ 44} 45 46// readability/nul: NUL byte in file (can't easily test this in text) 47 48// readability/utf8: Invalid UTF-8 (can't easily test) 49 50// readability/increment: Pre-increment in statements 51void test_increment () 52{ 53int i = 0 ; 54++ i ;// Should trigger readability/increment 55for (int j = 0 ;j < 10 ;++ j ) {// Should trigger readability/increment 56printf ("%d\n" ,j ); 57 } 58} 59 60// runtime/arrays: Variable-length arrays 61void test_arrays (int size ) 62{ 63int arr [size ];// Should trigger runtime/arrays 64} 65 66// runtime/int: Use C basic types instead of fixed-width 67void test_int_types () 68{ 69short x = 1 ;// Should trigger runtime/int 70long long y = 2 ;// Should trigger runtime/int 71} 72 73// runtime/memset: memset with wrong arguments 74void test_memset () 75{ 76char buf [100 ]; 77memset (buf ,sizeof (buf ),0 );// Should trigger runtime/memset 78} 79 80// runtime/printf: Use sprintf instead of snprintf 81void test_printf () 82{ 83char buf [100 ]; 84sprintf (buf ,"test" );// Should trigger runtime/printf 85} 86 87// runtime/printf_format: %N$ formats 88void test_printf_format2 () 89{ 90printf ("%1$d" ,42 );// Should trigger runtime/printf_format 91} 92 93// runtime/threadsafe_fn: Use non-thread-safe functions 94void test_threading () 95{ 96time_t t ; 97char * time_str = ctime (& t );// Should trigger runtime/threadsafe_fn 98asctime (localtime (& t ));// Should trigger runtime/threadsafe_fn 99} 100 101// runtime/deprecated: (This might be Neovim-specific) 102 103// whitespace/comments: Missing space after // 104void test_comments () 105{ 106int x = 5 ;// This is a comment // Should trigger whitespace/comments 107} 108 109// whitespace/indent: (Hard to test in this format) 110 111// whitespace/operators: (Hard to test) 112 113// whitespace/cast: (Hard to test) 114 115// build/init_macro: INIT() macro in non-header (but this is a .c file) 116 117// build/header_guard: No #pragma once (but this is a .c file) 118 119// build/defs_header: extern variables in _defs.h (but this is a .c file) 120 121// readability/old_style_comment: Old-style /* */ comment 122void test_old_style_comment () 123{ 124int x = 5 ;/* This is an old-style comment */ // Should trigger readability/old_style_comment 125} 126 127// Try to trigger more categories 128void test_more () 129{ 130// Try strcpy and strncpy 131char dest [100 ]; 132char src []= "test" ; 133strcpy (dest ,src );// Should trigger runtime/printf 134strncpy (dest ,src ,sizeof (dest ));// Should trigger runtime/printf 135 136// Try malloc and free (should trigger runtime/memory_fn) 137int * ptr = malloc (sizeof (int ));// Should trigger runtime/memory_fn 138free (ptr );// Should trigger runtime/memory_fn 139 140// Try getenv and setenv 141char * env = getenv ("HOME" );// Should trigger runtime/os_fn 142setenv ("TEST" ,"value" ,1 );// Should trigger runtime/os_fn 143long n = strtol (env ,NULL ,10 );// Should trigger runtime/deprecated 144} 145 146int main () 147{ 148test_printf_format (); 149test_bool (); 150test_multiline_comment (); 151test_multiline_string (); 152test_increment (); 153test_arrays (10 ); 154test_int_types (); 155test_memset (); 156test_printf (); 157test_printf_format2 (); 158test_threading (); 159test_comments (); 160test_old_style_comment (); 161test_more (); 162 163return 0 ; 164}