惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
Y
Y Combinator Blog
I
InfoQ
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
IT之家
IT之家
H
Help Net Security
月光博客
月光博客
S
SegmentFault 最新的问题
B
Blog
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Vercel News
Vercel News
博客园 - 叶小钗
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) C++自定义的字面量 Codeforces Round 907 (Div. 2)
反复横跳的 Clang-Tidy(cert-dcl21-cpp)
Shiroha · 2023-12-04 · via Shiroha白羽的博客

今天早上有些发烧,就没去上班,下午稍微好点了之后就爬起来折腾会代码,写着写着就发现了一个奇怪的东西。把代码抽出核心部分类似如下的代码

1
2
3
4
5
struct A {
int v;

A operator++(int) noexcept { return A(v++); }
}

这里返回的值是一个自身为 A 的右值,理论上并不需要明确指明右值(即返回值写 A&&),因为返回值本身肯定是右值,在原来的栈中如果进行赋值操作的时候,会先尝试进行移动构造,如果没有提供移动构造的情况下才会尝试进行引用构造甚至是复制构造

但是这段代码,Clang-Tidy 却给我报了 Warning: Clang-Tidy: Overloaded 'operator++' returns a non-constant object instead of a constant object type

大致意思是说,operator++ 方法返回了一个非 const 的变量。看了下样例,大概意思是说,因为返回值是一个”临时”的类型(毕竟是一个右值,用完应该就要丢掉的)故需要 const 一下,避免会出现 (i++)++; 这种离谱的代码(后者的 ++ 是作用在返回的右值上的,实际上不会对原来的值生效)

一听,好像有点道理,自动修复一下

1
2
3
4
5
struct A {
int v;

const A operator++(int) noexcept { return A(v++); }
}

嗯,这样应该就行了吧。然后 Clang-Tidy 又给我报了个 Warning:Clang-Tidy: Return type 'const A' is 'const'-qualified at the top level, which may reduce code readability without improving const correctness

蛤?又告诉我不能带 const,因为这可能会导致不必要的代码理解?看了下例子,也非常好理解,因为返回值是一个右值,最终(指代通常情况下)都应该被移动构造,这样的话,实际上 const 仅仅是对右值进行了 const 标识,并没有什么用处

也就是说,加也不对,不加也不对……

在翻找了一堆资料后,发现了这两个 checker 的文档:

其中,在前者有这样一句:

It will be removed in clang-tidy version 19.

嗯,再一看自己的 clang-tidy,恰好是 18.0.0