Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/safe-guides/code_style/naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
- 布尔型变量或函数避免使用否定形式
- 尽量不要使用 Unicode 标识符。

【正例】

```rust
let first_name: &str = "John";
let last_name: &str = "Smith";
const ERROR_DIRECTORY_NOT_SUPPORTED: u32 = 336;
const ERROR_DRIVER_CANCEL_TIMEOUT: u32 = 594;
```

【反例】

```rust
let ming: &str = "John";
let xing: &str = "Smith";
const ERROR_NO_1: u32 = 336;
const ERROR_NO_2: u32 = 594;
```



## P.NAM.02 避免使用语言内置保留字、关键字、内置类型和`trait`等特殊名称
Expand Down
80 changes: 39 additions & 41 deletions src/safe-guides/coding_practice/consts.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,6 @@

---

## P.CNS.01 常量必须都要持有明确语义以便让人更好地理解

### 【描述】

常量的定义必须表达清楚相应语义,这样可以增强可读性。

【正例】

```rust
const ERROR_DIRECTORY_NOT_SUPPORTED: u32 = 336;
const ERROR_DRIVER_CANCEL_TIMEOUT: u32 = 594;
```

【反例】

```rust
const ERROR_NO_1: u32 = 336;
const ERROR_NO_2: u32 = 594;
```



## P.CNS.02 注意根据常量和静态变量的本质区别来选择何时使用常量或静态变量

### 【描述】

常量 vs 静态变量:

1. 常量会内联到使用它的地方。
2. 静态变量不会内联,它是全局的,且有一个引用地址。

有些场合要根据常量和静态变量的本质区别来选择合适的类型。

比如,当你想创建一个很大的常量数组时,应该考虑将其换成静态变量。因为常量会到处内联。

---


## G.CNS.01 对于科学计算中涉及浮点数近似值的常量要尽量使用预定义常量

### 【级别:建议】
Expand Down Expand Up @@ -199,9 +161,6 @@ static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
```





## G.CNS.05 对于函数或方法应尽可能地使用 `const fn`

### 【级别:建议】
Expand Down Expand Up @@ -232,3 +191,42 @@ fn new() -> Self {
}
```

## G.CNS.06 注意避免将量大的数据结构定义为常量

### 【级别:建议】

建议按此规范执行。

### 【Lint 检测】

| lint name | Clippy 可检测 | Rustc 可检测 | Lint Group | 是否可定制 |
| ------------------------------------------------------------ | ------------- | ------------ | ---------- | ----- |
| _ | no | no | _ | yes |

【定制化参考】
这条规则如果需要定制Lint,则需要找出每个定义的常量再判断其空间占用,或可直接排除基础类型以外的数据类型。

**【描述】**

常量会内联到使用它的地方而静态变量不会内联,它是全局的,且有一个引用地址。
当创建一个很大的常量数组时,应该考虑将其换成静态变量,因为常量会到处内联。


**【示例】**
【正例】
```rust
fn main() {
static MONTHS: [&str; 12] = ["January", "Feburary", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"];
}
```

【反例】
```rust
fn main() {
const MONTHS: [&str; 12] = ["January", "Feburary", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"];
}
```
30 changes: 27 additions & 3 deletions src/safe-guides/coding_practice/statics.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,38 @@

---

## P.STV.01 不要直接使用可变静态变量作为全局变量
## G.STV.01 不要直接使用可变静态变量作为全局变量

### 【级别:建议】

建议按此规范执行。

### 【Lint 检测】

| lint name | Clippy 可检测 | Rustc 可检测 | Lint Group | 是否可定制 |
| ------------------------------------------------------------ | ------------- | ------------ | ---------- | ----- |
| _ | no | no | _ | yes |

【定制化参考】
这条规则如果需要定制Lint,则可以先检查代码是否使用到FFI,然后再检测代码中是否有已定义为可变的静态变量(static mut),以及其是否用在用于调用外部函数上,若此条件不达标则发出告警。

### 【描述】

对可变静态变量进行全局修改是 Unsafe 的。在多线程应用中,修改静态变量会导致数据争用(data race),此未定义行为目前并不会被Clippy或Rustc检测出。

###
【反例】

```rust
static mut NUM_OF_APPLES: i32 = 0;

unsafe fn buy_apple() {
NUM_OF_APPLES += 1;
}

unsafe fn eat_apple() {
NUM_OF_APPLES -= 1;
}
```

【例外】

Expand All @@ -36,4 +61,3 @@ fn main() {
}
```