
What is Clazy?
Clazy is a specialized static analysis tool for C++, specifically tailored to projects that use the Qt framework. Developed by KDAB, Clazy is a Clang-based linter that checks your C++ code for common Qt-related anti-patterns, performance issues, bad practices, and even potential bugs — before you run or compile your application.
Clazy extends the capabilities of the LLVM/Clang compiler by adding Qt-specific checks that are not typically caught by standard static analysis tools or compilers. These checks help developers write faster, safer, and cleaner Qt code by catching common pitfalls such as inefficient signal-slot connections, misuse of containers, memory overhead in copy constructors, and more.
It’s an invaluable tool for developers working on large-scale C++/Qt applications, enabling them to improve maintainability and runtime performance without needing to manually audit every line of code.
What are the Major Use Cases of Clazy?
Clazy provides a deep level of insight into C++ Qt codebases and serves a variety of practical use cases across development teams:
1. Performance Optimization
- Detect inefficient use of
QString
,QList
, or repeated use of expensive temporary objects. - Suggests usage of
QStringLiteral
instead ofQString("text")
for memory efficiency.
2. Code Quality and Maintenance
- Enforces best practices like avoiding unnecessary detaching of implicitly shared classes or redundant
Q_OBJECT
macros.
3. Refactoring Large Codebases
- Helps identify outdated or deprecated patterns when migrating from older Qt versions to modern Qt5/Qt6 APIs.
4. Code Review Automation
- Integrates with CI tools to automatically reject pull requests that introduce unsafe or bad Qt coding patterns.
5. Educational Use for Junior Developers
- Acts as a learning tool by teaching developers about performance pitfalls and idiomatic Qt usage through actionable warnings.
6. Prevention of Subtle Bugs
- Catches issues that are hard to debug, such as missed
Q_PROPERTY
bindings or incorrect usage ofconnect()
signatures.
How Clazy Works (with Architecture)
Clazy is a plugin that integrates into the Clang compiler frontend, allowing it to analyze your C++ source files during compilation and emit custom diagnostics related to Qt usage.
High-Level Architecture:
[Source Code (.cpp/.h)]
↓
[Clang Frontend + Clazy Plugin]
↓
[AST Parsing & Semantic Analysis]
↓
[Clazy Checkers]
↓
[Warnings & Diagnostics for Qt-specific Issues]
- Clang Frontend: Parses your code into an Abstract Syntax Tree (AST).
- Clazy Plugin: Hooks into the AST traversal to apply additional rules and patterns specific to Qt.
- Clazy Checkers: A set of built-in modules that analyze patterns such as unnecessary memory allocation, poor copy semantics, and inefficient method calls.
- Diagnostics: Warnings or hints are emitted during compilation, helping you resolve issues proactively.
Clazy uses a command-line or compiler flag approach, making it easy to integrate into build pipelines or IDEs like Qt Creator or CLion.
What is the Basic Workflow of Clazy?
The Clazy workflow is developer-friendly and closely mirrors your existing CMake or qmake build workflow. Here’s a high-level breakdown:
- Write or Import Your Qt/C++ Code
- Create a Qt project or use Clazy with an existing one.
- Enable Clazy in Your Build Toolchain
- Pass Clazy as a plugin to Clang via
CXXFLAGS
.
- Pass Clazy as a plugin to Clang via
- Compile Your Project
- As the project compiles, Clazy runs its checkers against your code.
- Review Clazy Warnings
- Read the warnings Clazy produces and understand the Qt-specific suggestions.
- Fix or Suppress
- Improve your code based on suggestions or suppress checks if justified.
- Iterate and Integrate into CI
- Repeat the process, integrate into CI pipelines for automatic linting.
Step-by-Step Getting Started Guide for Clazy
Step 1: Install Clazy
On Ubuntu/Debian:
sudo apt install clazy
For Arch:
sudo pacman -S clazy
You can also build from source using:
git clone https://github.com/KDE/clazy.git
cd clazy
mkdir build && cd build
cmake ..
make -j$(nproc)
sudo make install
Step 2: Compile with Clazy Enabled
Use Clazy as a plugin to Clang when building your project:
CXX=clazy g++ -std=c++17 -fplugin=libClazyPlugin.so main.cpp -o myapp
Alternatively, for CMake-based projects:
export CXX=clazy
cmake .
make
Step 3: Run Clazy Checks
You can also run individual Clazy checks:
clazy-standalone main.cpp --checks=level0,level1
Clazy levels range from 0 (safe recommendations) to 3 (deep optimization hints).
Step 4: Read and Fix the Warnings
Sample output:
warning: Avoid using QLatin1String on temporary strings
Use these messages to update your code for better performance and correctness.
Step 5: IDE Integration (Optional)
In Qt Creator, go to:
Tools → Options → Kits → Compiler → Clang → Enable Clazy Plugin
For CLion, configure Clazy as part of your custom toolchain or through compile_commands.json
.
Step 6: Integrate into CI/CD (Optional)
Include Clazy in your CI pipelines using scripts or CMake targets that compile your code with Clazy and log/report all warnings.