• 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

  • 相关阅读:
    AntItemInventoryScene道具栏列表显示
    AntItemBaseScene的功能分析
    杀掉一直处于 正在终止 状态的并发请求
    oracle文件版本
    某个系统配置文件 用户层的SQL
    系统配置SQL profile
    (转)oracle 查看表所占用的空间大小
    Workflow Mailer Notifications设置
    System Hold, Fix Manager before resetting counters
    Linux mail 命令参数
  • 原文地址:https://www.cnblogs.com/findumars/p/10392895.html
Copyright © 2020-2023  润新知