You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何clang-tidy的modernize-use-emplace检查未捕获该警告?

问题:clang-tidy modernize-use-emplace检查的差异问题

代码示例

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<pair<int, int>> a;
    a.push_back(make_pair(1, 2)); //caught

    vector<vector<pair<int, int>>> b(1);
    b[0].push_back(make_pair(1, 2)); //not caught

    return 0;
}

执行命令

clang-tidy -config="{Checks: 'modernize-use-emplace'}" a.cpp

检查输出

a.cpp:6:4: warning: use emplace_back instead of push_back [modernize-use-emplace]
        a.push_back(make_pair(1, 2)); //caught
          ^~~~~~~~~~~~~~~~~~~     ~
          emplace_back

clang-tidy版本信息

LLVM (http://llvm.org/):
  LLVM version 14.0.6
  Optimized build.
  Default target: x86_64-pc-linux-gnu
  Host CPU: skylake

原因分析

这是因为你使用的clang-tidy 14.0.6版本中,modernize-use-emplace检查器对嵌套容器的元素访问存在类型推导局限。

直接调用a.push_back时,a的类型是明确的vector<pair<int, int>>,检查器能清晰识别push_back的参数是临时构造的pair对象,符合替换为emplace_back的条件。

但对于b[0].push_back,检查器无法正确追踪operator[]返回值的完整类型信息——虽然b[0]本质是vector<pair<int, int>>类型的左值,但旧版本检查器没有完成这层类型关联,导致漏判了该场景下的push_back调用。

这个问题属于该检查器的已知缺陷,后续更新的clang-tidy版本已经修复了这个类型推导问题,升级后就能正确捕获这类嵌套容器的push_back调用。

内容的提问来源于stack exchange,提问作者Luke Videckis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 04:45:32