Skip to content

Commit fdaa4a8

Browse files
authored
update (#105)
* V 0.3 * V0.3: modify README * update Version to 0.3 * add code example for P.SEC.01 * modify P.SEC.01 * 3.30 review * update
1 parent 42ef5bd commit fdaa4a8

File tree

163 files changed

+1024
-836
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+1024
-836
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
- [P.CMT.01](./safe-guides/code_style/comments/P.CMT.01.md)
3838
- [P.CMT.02](./safe-guides/code_style/comments/P.CMT.02.md)
3939
- [P.CMT.03](./safe-guides/code_style/comments/P.CMT.03.md)
40-
- [P.CMT.04](./safe-guides/code_style/comments/G.CMT.04.md)
41-
- [P.CMT.05](./safe-guides/code_style/comments/G.CMT.05.md)
40+
- [P.CMT.04](./safe-guides/code_style/comments/P.CMT.04.md)
41+
- [P.CMT.05](./safe-guides/code_style/comments/P.CMT.05.md)
4242
- [G.CMT.01](./safe-guides/code_style/comments/G.CMT.01.md)
4343
- [G.CMT.02](./safe-guides/code_style/comments/G.CMT.02.md)
4444
- [G.CMT.03](./safe-guides/code_style/comments/G.CMT.03.md)

src/safe-guides/Appendix/templates/clippy.toml.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,16 @@ Embark 也在跟踪和推动在 Cargo 中支持 Lint 配置的功能,相关 is
169169
- [Be able to disable/enable Clippy lints globally](https://github.com/EmbarkStudios/rust-ecosystem/issues/22)
170170
- [Support defining enabled and disabled lints in a configuration file](https://github.com/rust-lang/cargo/issues/5034)
171171
- [[Roadmap] Configuration file for lints](https://github.com/rust-lang/rust-clippy/issues/6625)
172+
173+
## 代码生成相关 clippy 配置
174+
175+
和 C 语言绑定代码生成,避免clippy 警告,相关配置可参考:
176+
177+
```rust
178+
// Generated by gir (https://github.com/gtk-rs/gir @ 5bbf6cb)
179+
// from ../gir-files (@ 8e47c67)
180+
// DO NOT EDIT
181+
182+
#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
183+
#![allow(clippy::approx_constant, clippy::type_complexity, clippy::unreadable_literal, clippy::upper_case_acronyms)]
184+
```

src/safe-guides/code_style/comments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
## 列表
99

1010
- [P.CMT.01 代码能做到自注释,文档要干练简洁](./comments/P.CMT.01.md)
11-
- [P.CMT.02 注释应该有一定宽度限制](./comments/P.CMT.02.md)
11+
- [P.CMT.02 注释应该有宽度限制](./comments/P.CMT.02.md)
1212
- [P.CMT.03 使用行注释而避免使用块注释](./comments/P.CMT.03.md)
1313
- [P.CMT.04 文件头注释包含版权说明](./comments/P.CMT.04.md)
1414
- [P.CMT.05 在注释中使用 FIXME 和 TODO 来帮助任务协作](./comments/P.CMT.05.md)

src/safe-guides/code_style/comments/G.CMT.01.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
## G.CMT.01 在 公开的返回`Result`类型返回的函数文档中增加 Error 注释
1+
## G.CMT.01 在公开的返回`Result`类型的函数文档中增加 Error 注释
22

33
**【级别】** 建议
44

55
**【描述】**
66

7-
在公开(pub)的返回`Result`类型函数文档中,建议增加 `# Error` 注释来解释什么场景下该函数会返回什么样的错误类型,方便用户处理错误。
7+
在公开(pub)的返回`Result`类型的函数文档中,建议增加 `# Error` 注释来解释什么场景下该函数会返回什么样的错误类型,方便用户处理错误。
88

9-
说明: 该规则通过 cargo clippy 来检测。默认不会警告
9+
说明: 该规则可以通过 cargo clippy 来检测,但默认不会警告
1010

1111
**【反例】**
1212

13+
1314
```rust
14-
# use std::io;
15+
#![warn(clippy::missing_errors_doc)]
16+
17+
use std::io;
18+
// 不符合: Clippy 会警告 "warning: docs for function returning `Result` missing `# Errors` section"
1519
pub fn read(filename: String) -> io::Result<String> {
1620
unimplemented!();
1721
}
@@ -20,7 +24,11 @@ pub fn read(filename: String) -> io::Result<String> {
2024
**【正例】**
2125

2226
```rust
23-
# use std::io;
27+
#![warn(clippy::missing_errors_doc)]
28+
29+
use std::io;
30+
// 符合:增加了规范的 Errors 文档注释
31+
2432
/// # Errors
2533
///
2634
/// Will return `Err` if `filename` does not exist or the user does not have

src/safe-guides/code_style/comments/G.CMT.02.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
**【反例】**
1212

1313
```rust
14+
#![warn(clippy::missing_panics_doc)]
15+
16+
// 不符合:没有添加 Panic 相关的文档注释,Clippy会报错 "warning: docs for function which may panic missing `# Panics` section"。
1417
pub fn divide_by(x: i32, y: i32) -> i32 {
1518
if y == 0 {
1619
panic!("Cannot divide by 0")
@@ -23,6 +26,9 @@ pub fn divide_by(x: i32, y: i32) -> i32 {
2326
**【正例】**
2427

2528
```rust
29+
#![warn(clippy::missing_panics_doc)]
30+
31+
// 符合:增加了规范的 Panic 注释
2632
/// # Panics
2733
///
2834
/// Will panic if y is 0
@@ -39,4 +45,6 @@ pub fn divide_by(x: i32, y: i32) -> i32 {
3945

4046
| lint name | Clippy 可检测 | Rustc 可检测 | Lint Group | 默认 level |
4147
| ------ | ---- | --------- | ------ | ------ |
42-
| [missing_panics_doc ](https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc ) | yes| no | Style | allow |
48+
| [missing_panics_doc ](https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc ) | yes| no | Style | allow |
49+
50+
默认为 `allow`,但是此规则需要设置`#![warn(clippy::missing_panics_doc)]`

src/safe-guides/code_style/comments/G.CMT.03.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Rust 代码风格中提倡使用**四个空格**代替tab,在文档注释中
1111
下面文档注释中使用了 tab。
1212

1313
```rust
14+
// 不符合:文档注释中使用了 tab 缩进
1415
///
1516
/// Struct to hold two strings:
1617
/// - first one
@@ -30,6 +31,7 @@ pub struct DoubleString {
3031
**【正例】**
3132

3233
```rust
34+
// 符合:文档注释中使用了四个空格缩进
3335
///
3436
/// Struct to hold two strings:
3537
/// - first one
@@ -51,5 +53,3 @@ pub struct DoubleString {
5153
| lint name | Clippy 可检测 | Rustc 可检测 | Lint Group | 默认 level |
5254
| ------------------------------------------------------------ | ------------- | ------------ | ---------- | ---------- |
5355
| [tabs_in_doc_comments ](https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments ) | yes | no | Style | warn |
54-
55-
---

src/safe-guides/code_style/comments/G.CMT.04.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/safe-guides/code_style/comments/G.CMT.05.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/safe-guides/code_style/comments/P.CMT.01.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
模块级文档,来自于 Rust 标准库`std::vec`
2323

2424
```rust
25+
// 符合
26+
2527
//! # The Rust core allocation and collections library
2628
//!
2729
//! This library provides smart pointers and collections for managing
@@ -42,20 +44,22 @@
4244
普通文档注释示例,来自标准库`Vec::new`方法:
4345

4446
```rust
45-
/// Constructs a new, empty `Vec<T>`.
46-
///
47-
/// The vector will not allocate until elements are pushed onto it.
48-
///
49-
/// # Examples
50-
///
51-
/// ```
52-
/// # #![allow(unused_mut)]
53-
/// let mut vec: Vec<i32> = Vec::new();
54-
/// ```
55-
#[inline]
56-
#[rustc_const_stable(feature = "const_vec_new", since = "1.39.0")]
57-
#[stable(feature = "rust1", since = "1.0.0")]
58-
pub const fn new() -> Self {
59-
Vec { buf: RawVec::NEW, len: 0 }
60-
}
47+
// 符合
48+
49+
/// Constructs a new, empty `Vec<T>`.
50+
///
51+
/// The vector will not allocate until elements are pushed onto it.
52+
///
53+
/// # Examples
54+
///
55+
/// ```
56+
/// # #![allow(unused_mut)]
57+
/// let mut vec: Vec<i32> = Vec::new();
58+
/// ```
59+
#[inline]
60+
#[rustc_const_stable(feature = "const_vec_new", since = "1.39.0")]
61+
#[stable(feature = "rust1", since = "1.0.0")]
62+
pub const fn new() -> Self {
63+
Vec { buf: RawVec::NEW, len: 0 }
64+
}
6165
```

src/safe-guides/code_style/comments/P.CMT.02.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
## P.CMT.02 注释应该有一定宽度限制
1+
## P.CMT.02 注释应该有宽度限制
22

33
**【描述】**
44

5-
每行注释的宽度不能过长,需要设置一定的宽度,有助于提升可读性`comment_width`可配合 `wrap_comments` 将超过宽度限制的注释自动分割为多行
5+
每行注释的宽度不能过长,需要设置一定的宽度,不超过120,有助于提升可读性。
66

7-
注意:`use_small_heuristics`配置项并不包括`comment_width`
7+
`rustfmt`中通过`comment_width`配合 `wrap_comments` 配置项,可将超过宽度限制的注释自动分割为多行。
8+
9+
注意:`rustfmt``use_small_heuristics`配置项并不包括`comment_width`
810

911
**【反例】**
1012

1113
```rust
14+
// 不符合
1215
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
1316
```
1417

@@ -19,6 +22,7 @@
1922
注意:这里 `wrap_comments`并未使用默认值,需要配置为 true。
2023

2124
```rust
25+
// 符合
2226
// Lorem ipsum dolor sit amet, consectetur adipiscing elit,
2327
// sed do eiusmod tempor incididunt ut labore et dolore
2428
// magna aliqua. Ut enim ad minim veniam, quis nostrud
@@ -28,10 +32,6 @@
2832

2933
**【rustfmt 配置】**
3034

31-
此规则 Clippy 不可检测,由 rustfmt 自动格式化。
32-
33-
rustfmt 配置:
34-
3535
| 对应选项 | 可选值 | 是否 stable | 说明 |
3636
| ------ | ---- | ---- | ---- |
3737
| [`comment_width`](https://rust-lang.github.io/rustfmt/?#comment_width) | 80(默认) | No| 指定一行注释允许的最大宽度 |

0 commit comments

Comments
 (0)