Skip to content

Commit 99c268a

Browse files
authored
优化规则
ID_unsafeStringFunction:细化规则说明 ID_illForwardingReference:补充依据条目,添加链接 ID_redundantJump:修正规则说明,优化示例
1 parent eaf0859 commit 99c268a

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

c-cpp-rules.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,9 +1711,10 @@
17111711
"ID_illForwardingReference": {
17121712
"checkPoint": "转发引用只应作为 std::forward 的参数",
17131713
"level": "warning",
1714-
"comment": "不应混淆转发引用与右值引用,除作为 std::forward 的参数之外,不应对转发引用再有任何操作。",
1714+
"comment": "不应混淆“转发引用(forwarding references)”与右值引用,除作为 std::forward 的参数之外,不应对转发引用再有任何操作。",
17151715
"tag": "function",
17161716
"related": "ID_unsuitableForward",
1717+
"standard": "ISO/IEC 14882:2011 20.2.3(1),ISO/IEC 14882:2017 23.2.5(1)",
17171718
"reference": "C++ Core Guidelines F.19"
17181719
},
17191720
"ID_multiAllocation": {
@@ -1905,9 +1906,9 @@
19051906
"reference": "MISRA C++ 2008 4-10-1"
19061907
},
19071908
"ID_redundantJump": {
1908-
"checkPoint": "不应出现多余的跳转语句",
1909+
"checkPoint": "不应存在不受条件控制的跳转语句",
19091910
"level": "warning",
1910-
"comment": "无返回值函数的最后一条 return 语句、循环体的最后一条 continue 语句、goto 到下一条语句等是多余的,应当去除",
1911+
"comment": "不受条件控制的跳转语句不改变程序的执行路径,往往意味着逻辑错误或功能未实现 ",
19111912
"tag": "control"
19121913
},
19131914
"ID_paramPassedByValue": {

c-cpp-rules.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@
451451
- [R9.7.2 禁止 goto 语句向前跳转](#forbidgotoback)
452452
- [R9.7.3 禁用 goto 语句](#forbidgoto)
453453
- [R9.7.4 禁用 setjmp、longjmp](#forbidlongjmp)
454-
- [R9.7.5 不应出现多余的跳转语句](#redundantjump)
454+
- [R9.7.5 不应存在不受条件控制的跳转语句](#redundantjump)
455455
- [R9.7.6 避免使用跳转语句退出循环](#jumpoutloop)
456456
<br/>
457457

@@ -1119,7 +1119,7 @@ gets、strcpy、strcat、wcscpy、wcscat、
11191119
sprintf、vsprintf、swprintf、vswprintf、
11201120
scanf、sscanf、fscanf、vfscanf、vscanf、vsscanf
11211121
```
1122-
与这类函数相似的 API 同样受本规则约束,如
1122+
与这类函数相似的函数同样受本规则约束,如下列 Windows API
11231123
```
11241124
StrCpy、StrCpyA、StrCpyW、StrCat、StrCatA、StrCatW、
11251125
lstrcpy、lstrcpyA、lstrcpyW、lstrcat、lstrcatA、lstrcatW
@@ -10611,7 +10611,7 @@ ID_illForwardingReference&emsp;&emsp;&emsp;&emsp;&nbsp;:fire: function warning
1061110611

1061210612
<hr/>
1061310613

10614-
不应混淆转发引用与右值引用,除作为 std::forward 的参数之外,不应对转发引用再有任何操作。
10614+
不应混淆“[转发引用(forwarding references)](https://en.cppreference.com/w/cpp/language/reference#Forwarding_references)”与右值引用,除作为 std::forward 的参数之外,不应对转发引用再有任何操作。
1061510615

1061610616
转发引用是类型为 T&& 的参数,T 为函数模板类型,无论左值还是右值均可被这种参数接受,而且 const、volatile 等属性也会被忽略,由于含有不确定的状态,所以直接操作转发引用是不妥的,只应通过 std::forward<T> 交由合适的接口处理。
1061710617

@@ -10630,6 +10630,11 @@ void bar(T&& x) {
1063010630
ID_unsuitableForward
1063110631
<br/>
1063210632

10633+
#### 依据
10634+
ISO/IEC 14882:2011 20.2.3(1)
10635+
ISO/IEC 14882:2017 23.2.5(1)
10636+
<br/>
10637+
1063310638
#### 参考
1063410639
C++ Core Guidelines F.19
1063510640
<br/>
@@ -14596,32 +14601,33 @@ MISRA C++ 2008 17-0-5
1459614601
<br/>
1459714602
<br/>
1459814603

14599-
### <span id="redundantjump">▌R9.7.5 不应出现多余的跳转语句</span>
14604+
### <span id="redundantjump">▌R9.7.5 不应存在不受条件控制的跳转语句</span>
1460014605

1460114606
ID_redundantJump&emsp;&emsp;&emsp;&emsp;&nbsp;:fire: control warning
1460214607

1460314608
<hr/>
1460414609

14605-
无返回值函数的最后一条 return 语句、循环体的最后一条 continue 语句、goto 到下一条语句等是多余的,应当去除
14610+
不受条件控制的跳转语句不改变程序的执行路径,往往意味着逻辑错误或功能未实现
1460614611

1460714612
示例:
1460814613
```
1460914614
void foo() {
14615+
goto L; // Non-compliant, unconditional
14616+
.... // Dead code
14617+
L:
1461014618
....
14611-
return; // Redundant
1461214619
}
1461314620

1461414621
void bar() {
14615-
while (condition) {
14616-
....
14617-
continue; // Redundant
14618-
}
14622+
....
14623+
return; // Non-compliant, redundant
1461914624
}
1462014625

1462114626
void baz() {
14622-
goto L; // Redundant
14623-
L:
14624-
....
14627+
while (cond) {
14628+
....
14629+
continue; // Non-compliant, redundant
14630+
}
1462514631
}
1462614632
```
1462714633
<br/>

0 commit comments

Comments
 (0)