QtCreator 可以通過 Clang-Tidy 和 CLazy 對你的代碼進行靜態檢查


QtCreator 可以通過 Clang-Tidy 和 CLazy 對你的代碼進行靜態檢查

打開你的工程,點擊Analyze -> Clang-Tidy and CLazy

選擇你想分析的 cpp, 然后可以點下方 Filter 旁邊的 Apply Fixits 按鈕修復

這里並不想對 static analyze 展開太多,想具體了解的可以看別人的文章,比如

Qt:在QtCreator中使用Clang-Tidy和Clazy檢查C++代碼質量 - Jason’s home - CSDN博客​blog.csdn.net圖標


今天想通過靜態檢查來談談如何提高 Qt 代碼質量

① Use multi-arg instead

【不要使用一連串的 arg().arg().arg() 了】

QString("%1 %2").arg(a).arg(b); // Bad
QString("%1 %2").arg(a, b); // one less temporary heap allocation

② parameter 'list' is passed by value and only copied once; consider moving it to avoid unnecessary copies

【多使用右值引用,可以通過 std::move 將參數轉化為右值引用】

ChartWidget::ChartWidget(QWidget *parent,QList<int> list) : QWidget(parent), l(list), delt(1.0)
{
} // Bad

ChartWidget::ChartWidget(QWidget *parent,QList<int> list) : QWidget(parent), l(std::move(list)), delt(1.0)
{
} // performance unnecessary value param

③ the parameter 'table_string' is copied for each invocation but only used as a const reference; consider making it a const reference

【定義函數時,多使用 const &】

void LogData::setupMatrixCam2Veh(QString table_string) // Bad
void LogData::setupMatrixCam2Veh(const QString& table_string)
// performance unnecessary value param

④ constructor does not initialize these fields

【不要忘記初始化類的變量,在頭文件的變量旁添加 {} 就可以了】

QToolButton *resetButton; // Bad
QToolButton *resetButton{}; // cppcoreguidelines pro type member init

⑤ use auto when initializing with new to avoid duplicating the type name

【多用auto關鍵字,盡量使用更現代化的方式來 new】

QHBoxLayout *hl = new QHBoxLayout; // Bad
auto hl = new QHBoxLayout; // modernize use auto

⑥ to be continued...

https://zhuanlan.zhihu.com/p/51791350


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM