trailing_whitespace.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* Trailing whitespace test. Almost embarrassing, but trivial. */
  2. #include "ccanlint.h"
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. static char *report_on_trailing_whitespace(const char *line)
  6. {
  7. if (!strends(line, " ") && !strends(line, "\t"))
  8. return NULL;
  9. if (strlen(line) > 20)
  10. return talloc_asprintf(line, "...'%s'",
  11. line + strlen(line) - 20);
  12. return talloc_asprintf(line, "'%s'", line);
  13. }
  14. static void *check_trailing_whitespace(struct manifest *m)
  15. {
  16. char *report;
  17. report = report_on_lines(&m->c_files, report_on_trailing_whitespace,
  18. NULL);
  19. report = report_on_lines(&m->h_files, report_on_trailing_whitespace,
  20. report);
  21. return report;
  22. }
  23. static const char *describe_trailing_whitespace(struct manifest *m,
  24. void *check_result)
  25. {
  26. return talloc_asprintf(check_result,
  27. "Some source files have trailing whitespace:\n"
  28. "%s", (char *)check_result);
  29. }
  30. struct ccanlint trailing_whitespace = {
  31. .name = "Lines with unnecessary trailing whitespace",
  32. .total_score = 1,
  33. .check = check_trailing_whitespace,
  34. .describe = describe_trailing_whitespace,
  35. };