dep-curl/.clang-tidy.yml

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

49 lines
1.7 KiB
YAML
Raw Normal View History

clang-tidy: fixes and improvements Fix bigger and smaller kinks in how clang-tidy is configured and used. Sync behavior more between autotools and cmake, lib/src and tests. Bump clang-tidy minimum version and prepare logic to allow using clang-tidy to a fuller extent. - move clang-tidy settings from builds to a new `.clang-tidy.yml`. To make it easy to see and edit checks at one place. Also to allow using the `--checks=` option internally to silence tests-specific checks. (clang-tidy does not support multiple `--check=` options via the command-line.) Use explicit `--config-file=` option to point to the configuration. - .clang-tidy.yml: link to documentation. - suppress `clang-diagnostic-nullability-extension` due to a false positive in libtests with `CURL_WERROR=ON` and `PICKY_COMPILER=OFF`. - .clang-tidy.yml: enable `portability-*`, `misc-const-correctness`. - drop `--quiet` clang-tidy option by default to make its working a bit more transparent. The extra output is minimial. - consistently use double-dashes in clang-tidy command-line options. Supported by clang-tidy 9.0.0+ (2019-09-19). Before this patch single and double were used arbitrarily. - src/tool_parsecfg: silence false positive `clang-analyzer-unix.Stream`. Seen with clang 18 + clang-tidy 19 and 20 (only with autotools.) - INTERNALS: require clang-tidy 14.0.0+. For the `--config-file` option. - INTERNALS: recommend clang-tidy 19.1.0+, to avoid bogus `clang-analyzer-valist.Uninitialized` warnings. (bug details below) autotools: - allow configuring the clang-tidy tool via `CLANG_TIDY` env. Also to use in GHA to point to a suffixed clang-tody tool. - fix to pass CFLAGS to lib, src sources. (keep omitting them when using a non-clang compiler.) - fix to pass `--warnings-as-errors=*` in quotes to avoid globbing. cmake: - fix to not pass an empty `-I` to clang-tidy. - fix to pass CFLAGS (picky warnings) to clang-tidy for test sources. (keep omitting them when using a non-clang compiler.) - fix to disable `clang-diagnostic-unused-function` for test sources. (tests have static entry points, which trigger this check when checking them as individidual sources.) - fix forwarding `CURL_CLANG_TIDYFLAGS` to clang-tidy. - force disable picky warnings when running clang-tidy with a non-clang compiler. To not pass these flags when checking lib and src. CI: - GHA/linux: avoid clang-tidy bug by upgrading to v19, and drop the workaround. - GHA/linux: switch to clang from gcc in the clang-tidy job. Using gcc doesn't allow passing CFLAGS to clang-tidy, making it less effective. (My guess this was one factor contributing to this job often missing to find certain issues compared to GHA/macos.) I recomment using clang-tidy with a clang compiler, preferably the same version or one that's compatible. Other cases are best effort, and may fail if a C flag is passed to clang-tidy that it does not understand. Picky warnings are mostly omitted when using a non-clang compiler, reducing its usefulness. Details and reproducer for the v18 (and earlier) clang-tidy bug, previously affecting the GHA/linux job: clang-tidy <=18 emits false warnings way when passing multiple C sources at once (as done with autotools): ```sh cat > src1.c <<EOF #include <string.h> static void dummy(void *p) { memcmp(p, p, 0); } EOF cat > src2.c <<EOF #include <stdarg.h> void vafunc(int option, ...) { va_list param; va_start(param, option); if(option) (void)va_arg(param, int); va_end(param); } EOF /opt/homebrew/opt/llvm@18/bin/clang-tidy --checks=clang-analyzer-valist.Uninitialized src1.c src2.c # src2.c:7:11: warning: va_arg() is called on an uninitialized va_list [clang-analyzer-valist.Uninitialized] ``` Follow-up to e86542038dda88dadf8959584e803895f979310c #17047 Closes #20605
2026-02-15 23:58:39 +01:00
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# SPDX-License-Identifier: curl
---
# https://clang.llvm.org/extra/clang-tidy/
clang-tidy: fixes and improvements Fix bigger and smaller kinks in how clang-tidy is configured and used. Sync behavior more between autotools and cmake, lib/src and tests. Bump clang-tidy minimum version and prepare logic to allow using clang-tidy to a fuller extent. - move clang-tidy settings from builds to a new `.clang-tidy.yml`. To make it easy to see and edit checks at one place. Also to allow using the `--checks=` option internally to silence tests-specific checks. (clang-tidy does not support multiple `--check=` options via the command-line.) Use explicit `--config-file=` option to point to the configuration. - .clang-tidy.yml: link to documentation. - suppress `clang-diagnostic-nullability-extension` due to a false positive in libtests with `CURL_WERROR=ON` and `PICKY_COMPILER=OFF`. - .clang-tidy.yml: enable `portability-*`, `misc-const-correctness`. - drop `--quiet` clang-tidy option by default to make its working a bit more transparent. The extra output is minimial. - consistently use double-dashes in clang-tidy command-line options. Supported by clang-tidy 9.0.0+ (2019-09-19). Before this patch single and double were used arbitrarily. - src/tool_parsecfg: silence false positive `clang-analyzer-unix.Stream`. Seen with clang 18 + clang-tidy 19 and 20 (only with autotools.) - INTERNALS: require clang-tidy 14.0.0+. For the `--config-file` option. - INTERNALS: recommend clang-tidy 19.1.0+, to avoid bogus `clang-analyzer-valist.Uninitialized` warnings. (bug details below) autotools: - allow configuring the clang-tidy tool via `CLANG_TIDY` env. Also to use in GHA to point to a suffixed clang-tody tool. - fix to pass CFLAGS to lib, src sources. (keep omitting them when using a non-clang compiler.) - fix to pass `--warnings-as-errors=*` in quotes to avoid globbing. cmake: - fix to not pass an empty `-I` to clang-tidy. - fix to pass CFLAGS (picky warnings) to clang-tidy for test sources. (keep omitting them when using a non-clang compiler.) - fix to disable `clang-diagnostic-unused-function` for test sources. (tests have static entry points, which trigger this check when checking them as individidual sources.) - fix forwarding `CURL_CLANG_TIDYFLAGS` to clang-tidy. - force disable picky warnings when running clang-tidy with a non-clang compiler. To not pass these flags when checking lib and src. CI: - GHA/linux: avoid clang-tidy bug by upgrading to v19, and drop the workaround. - GHA/linux: switch to clang from gcc in the clang-tidy job. Using gcc doesn't allow passing CFLAGS to clang-tidy, making it less effective. (My guess this was one factor contributing to this job often missing to find certain issues compared to GHA/macos.) I recomment using clang-tidy with a clang compiler, preferably the same version or one that's compatible. Other cases are best effort, and may fail if a C flag is passed to clang-tidy that it does not understand. Picky warnings are mostly omitted when using a non-clang compiler, reducing its usefulness. Details and reproducer for the v18 (and earlier) clang-tidy bug, previously affecting the GHA/linux job: clang-tidy <=18 emits false warnings way when passing multiple C sources at once (as done with autotools): ```sh cat > src1.c <<EOF #include <string.h> static void dummy(void *p) { memcmp(p, p, 0); } EOF cat > src2.c <<EOF #include <stdarg.h> void vafunc(int option, ...) { va_list param; va_start(param, option); if(option) (void)va_arg(param, int); va_end(param); } EOF /opt/homebrew/opt/llvm@18/bin/clang-tidy --checks=clang-analyzer-valist.Uninitialized src1.c src2.c # src2.c:7:11: warning: va_arg() is called on an uninitialized va_list [clang-analyzer-valist.Uninitialized] ``` Follow-up to e86542038dda88dadf8959584e803895f979310c #17047 Closes #20605
2026-02-15 23:58:39 +01:00
# https://clang.llvm.org/extra/clang-tidy/checks/list.html
Checks:
- clang-analyzer-*
- -clang-analyzer-optin.performance.Padding
- -clang-analyzer-security.ArrayBound # due to false positives with clang-tidy v21.1.0
- -clang-analyzer-security.insecureAPI.bzero # for FD_ZERO() (seen on macOS)
- -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling
- -clang-diagnostic-nullability-extension
- bugprone-assert-side-effect
- bugprone-chained-comparison
- bugprone-dynamic-static-initializers
- bugprone-macro-parentheses
- bugprone-macro-repeated-side-effects
- bugprone-misplaced-operator-in-strlen-in-alloc
- bugprone-misplaced-pointer-arithmetic-in-alloc
- bugprone-not-null-terminated-result
- bugprone-posix-return
- bugprone-redundant-branch-condition
clang-tidy: enable `bugprone-signed-char-misuse`, fix fallouts Examples: ``` lib/vtls/openssl.c:2585:18: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] 2585 | msg_type = *(const char *)buf; lib/vtls/openssl.c:2593:18: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] 2593 | msg_type = *(const char *)buf; tests/server/mqttd.c:514:10: warning: comparison between 'signed char' and 'unsigned char' [bugprone-signed-char-misuse] 514 | if(passwd_flag == (char)(conn_flags & passwd_flag)) { tests/server/tftpd.c:362:13: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] 362 | c = test->rptr[0]; tests/server/tftpd.c:454:9: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] 454 | c = *p++; /* pick up a character */ src/tool_urlglob.c:272:46: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] 272 | pat->c.ascii.letter = pat->c.ascii.min = min_c; src/tool_urlglob.c:273:24: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] 273 | pat->c.ascii.max = max_c; tests/libtest/cli_h2_pausing.c:164:23: warning: suspicious usage of 'sizeof()' on an expression of pointer type [bugprone-sizeof-expression] 164 | memset(&resolve, 0, sizeof(resolve)); tests/libtest/cli_upload_pausing.c:158:23: warning: suspicious usage of 'sizeof()' on an expression of pointer type [bugprone-sizeof-expression] 158 | memset(&resolve, 0, sizeof(resolve)); tests/libtest/first.c:86:15: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse] 86 | coptopt = arg[optpos]; ``` Also: - tests/server/mqttd: drop a redundant and a wrongly signed cast. Ref: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/signed-char-misuse.html Closes #20654
2026-02-18 17:39:23 +01:00
- bugprone-signed-char-misuse
- bugprone-suspicious-enum-usage
- bugprone-suspicious-memset-usage
- bugprone-suspicious-missing-comma
- bugprone-suspicious-realloc-usage
- bugprone-suspicious-semicolon
- misc-const-correctness
- misc-header-include-cycle
- portability-*
lib: use lib source directory as base include path Backtrack on previous change that aimed to solve the wrong `share.h` being included. It turns out it did not fix this issue. At the same time it introduced relative header filenames and the need to include the same headers differently depending on the source files' location, reducing readability and editability. Replace this method by re-adding curl's lib source directory to the header path and addressing headers by the their full, relative name to that base directory. Aligning with this method already used in src and tests. With these advantages: - makes includes easier to read, recognize, grep, sort, write, and copy between sources, - syncs the way these headers are included across curl components, - avoids the ambiguity between system `schannel.h`, `rustls.h` vs. local headers using the same names in `lib/vtls`, - silences clang-tidy `readability-duplicate-include` checker, which detects the above issue, Ref: https://clang.llvm.org/extra/clang-tidy/checks/readability/duplicate-include.html - possibly silences TIOBE coding standard warnings: `6.10.2.a: Don't use relative paths in #include statements.` - long shot: it works well with concatenated test sources, for clang-tidy-friendly custom unity builds. Ref: #20667 Slight downside: it's not enforced. If there happens to be a collision between a local `lib/*.h` header and a system one, the solution is to rename (possibly with its `.c` counterpart) into the `curl_` namespace. This is also the method used by curl in the past. Also: - curlx/inet_pton: reduce scope of an include. - toolx/tool_time: apply this to an include, and update VS project files accordingly. Also dropping unnecessary lib/curlx header path. - clang-tidy: enable `readability-duplicate-include`. Follow-up to 3887069c661b40e76b053a4867eb565d4761ab3e #19676 Follow-up to 625f2c1644da58b9617479775badea21f125ce6d #16991 #16949 Closes #20623
2026-02-19 00:44:43 +01:00
- readability-duplicate-include
- readability-math-missing-parentheses
- readability-named-parameter
- readability-redundant-control-flow
- readability-redundant-declaration
- readability-redundant-function-ptr-dereference
- readability-redundant-parentheses
- readability-redundant-preprocessor
- readability-suspicious-call-argument
- readability-uppercase-literal-suffix
CheckOptions:
misc-header-include-cycle.IgnoredFilesList: 'curl/curl.h'
HeaderFilterRegex: '.*'