neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

clint_test.c (4183B)


      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
     16 void test_printf_format()
     17 {
     18  printf("%q", "test");  // Should trigger runtime/printf_format
     19 }
     20 
     21 // build/storage_class: Storage class not first
     22 const 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 
     29 void test_bool()
     30 {
     31  int flag = TRUE;  // Should trigger readability/bool
     32  if (flag == FALSE) {  // Should trigger readability/bool
     33    printf("false\n");
     34  }
     35  int maybe_val = MAYBE;  // Should trigger readability/bool
     36 }
     37 
     38 // readability/multiline_comment: Complex multi-line comment
     39 void test_multiline_comment()
     40 {
     41  /* This is a multi-line
     42     comment that spans
     43     multiple 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
     51 void test_increment()
     52 {
     53  int i = 0;
     54  ++i;  // Should trigger readability/increment
     55  for (int j = 0; j < 10; ++j) {  // Should trigger readability/increment
     56    printf("%d\n", j);
     57  }
     58 }
     59 
     60 // runtime/arrays: Variable-length arrays
     61 void test_arrays(int size)
     62 {
     63  int arr[size];  // Should trigger runtime/arrays
     64 }
     65 
     66 // runtime/int: Use C basic types instead of fixed-width
     67 void test_int_types()
     68 {
     69  short x = 1;        // Should trigger runtime/int
     70  long long y = 2;    // Should trigger runtime/int
     71 }
     72 
     73 // runtime/memset: memset with wrong arguments
     74 void test_memset()
     75 {
     76  char buf[100];
     77  memset(buf, sizeof(buf), 0);  // Should trigger runtime/memset
     78 }
     79 
     80 // runtime/printf: Use sprintf instead of snprintf
     81 void test_printf()
     82 {
     83  char buf[100];
     84  sprintf(buf, "test");  // Should trigger runtime/printf
     85 }
     86 
     87 // runtime/printf_format: %N$ formats
     88 void test_printf_format2()
     89 {
     90  printf("%1$d", 42);  // Should trigger runtime/printf_format
     91 }
     92 
     93 // runtime/threadsafe_fn: Use non-thread-safe functions
     94 void test_threading()
     95 {
     96  time_t t;
     97  char *time_str = ctime(&t);  // Should trigger runtime/threadsafe_fn
     98  asctime(localtime(&t));      // Should trigger runtime/threadsafe_fn
     99 }
    100 
    101 // runtime/deprecated: (This might be Neovim-specific)
    102 
    103 // whitespace/comments: Missing space after //
    104 void test_comments()
    105 {
    106  int 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
    122 void test_old_style_comment()
    123 {
    124  int x = 5; /* This is an old-style comment */  // Should trigger readability/old_style_comment
    125 }
    126 
    127 // Try to trigger more categories
    128 void test_more()
    129 {
    130  // Try strcpy and strncpy
    131  char dest[100];
    132  char src[] = "test";
    133  strcpy(dest, src);   // Should trigger runtime/printf
    134  strncpy(dest, src, sizeof(dest));  // Should trigger runtime/printf
    135 
    136  // Try malloc and free (should trigger runtime/memory_fn)
    137  int *ptr = malloc(sizeof(int));  // Should trigger runtime/memory_fn
    138  free(ptr);  // Should trigger runtime/memory_fn
    139 
    140  // Try getenv and setenv
    141  char *env = getenv("HOME");  // Should trigger runtime/os_fn
    142  setenv("TEST", "value", 1);   // Should trigger runtime/os_fn
    143 }
    144 
    145 int main()
    146 {
    147  test_printf_format();
    148  test_bool();
    149  test_multiline_comment();
    150  test_multiline_string();
    151  test_increment();
    152  test_arrays(10);
    153  test_int_types();
    154  test_memset();
    155  test_printf();
    156  test_printf_format2();
    157  test_threading();
    158  test_comments();
    159  test_old_style_comment();
    160  test_more();
    161 
    162  return 0;
    163 }