Skip to content

Commit

Permalink
优化规则
Browse files Browse the repository at this point in the history
ID_unsafeStringFunction:细化规则说明
ID_illForwardingReference:补充依据条目,添加链接
ID_redundantJump:修正规则说明,优化示例
  • Loading branch information
brotherbeer authored Apr 7, 2023
1 parent eaf0859 commit 99c268a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
7 changes: 4 additions & 3 deletions c-cpp-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -1711,9 +1711,10 @@
"ID_illForwardingReference": {
"checkPoint": "转发引用只应作为 std::forward 的参数",
"level": "warning",
"comment": "不应混淆转发引用与右值引用,除作为 std::forward 的参数之外,不应对转发引用再有任何操作。",
"comment": "不应混淆“转发引用(forwarding references)”与右值引用,除作为 std::forward 的参数之外,不应对转发引用再有任何操作。",
"tag": "function",
"related": "ID_unsuitableForward",
"standard": "ISO/IEC 14882:2011 20.2.3(1),ISO/IEC 14882:2017 23.2.5(1)",
"reference": "C++ Core Guidelines F.19"
},
"ID_multiAllocation": {
Expand Down Expand Up @@ -1905,9 +1906,9 @@
"reference": "MISRA C++ 2008 4-10-1"
},
"ID_redundantJump": {
"checkPoint": "不应出现多余的跳转语句",
"checkPoint": "不应存在不受条件控制的跳转语句",
"level": "warning",
"comment": "无返回值函数的最后一条 return 语句、循环体的最后一条 continue 语句、goto 到下一条语句等是多余的,应当去除",
"comment": "不受条件控制的跳转语句不改变程序的执行路径,往往意味着逻辑错误或功能未实现 ",
"tag": "control"
},
"ID_paramPassedByValue": {
Expand Down
32 changes: 19 additions & 13 deletions c-cpp-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@
- [R9.7.2 禁止 goto 语句向前跳转](#forbidgotoback)
- [R9.7.3 禁用 goto 语句](#forbidgoto)
- [R9.7.4 禁用 setjmp、longjmp](#forbidlongjmp)
- [R9.7.5 不应出现多余的跳转语句](#redundantjump)
- [R9.7.5 不应存在不受条件控制的跳转语句](#redundantjump)
- [R9.7.6 避免使用跳转语句退出循环](#jumpoutloop)
<br/>

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

<hr/>

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

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

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

#### 依据
ISO/IEC 14882:2011 20.2.3(1)
ISO/IEC 14882:2017 23.2.5(1)
<br/>

#### 参考
C++ Core Guidelines F.19
<br/>
Expand Down Expand Up @@ -14596,32 +14601,33 @@ MISRA C++ 2008 17-0-5
<br/>
<br/>

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

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

<hr/>

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

示例:
```
void foo() {
goto L; // Non-compliant, unconditional
.... // Dead code
L:
....
return; // Redundant
}

void bar() {
while (condition) {
....
continue; // Redundant
}
....
return; // Non-compliant, redundant
}

void baz() {
goto L; // Redundant
L:
....
while (cond) {
....
continue; // Non-compliant, redundant
}
}
```
<br/>
Expand Down

0 comments on commit 99c268a

Please sign in to comment.