Skip to content

Lesson 9 of 9 · 23m

Warnings, Static Analysis, Tests, and the Quality Pipeline

Configure warnings as executable review, use clang-tidy selectively, test behavior across build modes, and make CI enforce the standards the team actually means.

A codebase does not become maintainable because everyone is careful. It becomes maintainable because care is encoded in repeatable checks. Compiler warnings, clang-tidy, tests, formatting, and CI are not separate chores; they are one quality pipeline. The danger is cargo-cult configuration: every warning enabled, every check copied from a blog, and thousands of diagnostics nobody respects. The pipeline should be strict about the mistakes your team actually makes.

Warnings are executable code review

Start with a strong baseline and treat warnings as errors in CI:

add_library(project_warnings INTERFACE)
target_compile_options(project_warnings INTERFACE
    $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:
        -Wall -Wextra -Wpedantic -Wconversion -Wshadow -Wnon-virtual-dtor>
    $<$<CXX_COMPILER_ID:MSVC>:/W4 /permissive->
)

-Werror belongs in continuous integration, but be careful forcing it on downstream consumers. Compiler versions add warnings; your library should not fail to build for users because a future compiler became more opinionated.

Read warnings as semantic clues. -Wconversion is noisy in old code but catches narrowing and integer boundary mistakes. -Wshadow catches reused names that hide state. -Wnon-virtual-dtor often reveals inheritance misuse rather than a missing keyword.

clang-tidy is a scalpel

A baseline .clang-tidy should name checks deliberately:

Checks: >
  bugprone-*,
  cppcoreguidelines-avoid-non-const-global-variables,
  modernize-use-nullptr,
  performance-move-const-arg,
  readability-identifier-naming,
  -modernize-use-trailing-return-type
WarningsAsErrors: 'bugprone-*'

Run it with a compile database:

cmake --preset linux-dev
clang-tidy -p build/linux-dev src/session.cpp

Do not enable every modernize-* check blindly. Some checks encode taste, not correctness. Static analysis is valuable when it detects lifetimes, suspicious copies, uninitialized variables, and error-handling mistakes — not when it enforces one blogger's aesthetic.

Tests need behavior, not implementation worship

Unit tests cover small invariants. Integration tests cover object lifetimes across boundaries. Sanitizer CI jobs execute the same tests through different detectors:

ctest --test-dir build/linux-dev --output-on-failure
ctest --test-dir build/linux-asan --output-on-failure
ctest --test-dir build/linux-ubsan --output-on-failure

A strong test says what externally visible behavior is expected:

TEST(SessionTest, DestructionDuringPendingWriteDoesNotUseAfterFree) {
    // exercise cancellation path under ASan
}

Do not assert private state merely because it is visible through friendship hacks. Tests that mirror implementation lines break during harmless refactoring and teach engineers to ignore the suite.

CI should encode the support matrix

At minimum define compiler versions, standard level, platforms, build types, and sanitizer jobs. Cache dependencies, but do not cache away failure reproduction. Keep logs and crash artifacts. If a job is allowed to fail, say why in the pipeline configuration; unexplained red is organizational debt.

Field notes

  • Formatting belongs in automation (clang-format), but format-only diffs should be separated from semantic changes when possible.
  • NDEBUG disables assert in release builds; never put required side effects inside assertions.
  • Static analysis findings need triage like bug reports: true positive, false positive with suppression reason, or design smell.
  • The best pipeline is boring. If engineers routinely bypass it locally, the configuration is too slow, too noisy, or both.

Was this lesson helpful?

Sign in to rate