Tutorial part 6: fix-it hints

libgdiagnostics supports adding “fix-it hints” to a diagnostic: suggestions for the user on how to edit their code to fix a problem. These can be expressed as insertions, replacements, and removals of text.

For example, here we use diagnostic_add_fix_it_hint_replace() to add a replacement fix-it hint to a diagnostic:

On compiling and running the program, we should get output similar to:

test-fix-it-hint.c:19:13: error: unknown field 'colour'; did you mean 'color'
   19 |   return p->colour;
      |             ^~~~~~
      |             color

We can also add a call to diagnostic_manager_write_patch() to the program cleanup code:

diagnostic_manager_write_patch (diag_mgr, stderr);

This will write a patch to the stream (here stderr) giving the effect of all fix-it hints on all diagnostics emitted by the diagnostic_manager, giving something like:

@@ -16,7 +16,7 @@
 struct rgb
 get_color (struct object *p)
 {
-  return p->colour;
+  return p->color;
 }

See the guide to fix-it hints for more information, or go on to the next section of the tutorial.