Skip to content

Commit df89dbd

Browse files
authored
Merge pull request Rust-Coding-Guidelines#9 from Milittle/main
[Translate]:Code-Style/2.3 Code Comments:P.CMT.[03, 04. 05], G.GMT.[01, 02, 03]
2 parents 78c5a32 + f124b0e commit df89dbd

File tree

6 files changed

+277
-6
lines changed

6 files changed

+277
-6
lines changed
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
1-
# G.CMT.01 在公开的返回Result类型的函数文档中增加 Error 注释
1+
# G.CMT.01 Error comments need to added the documentation of functions that return `Result` types in the public
2+
3+
**[Level]** Suggestion
4+
5+
**[Description]**
6+
7+
In the public (pub) documentation of functions that return `Result` types, it is recommended to add `# Error` comments to explain what scenarios the function will return what kind of error type, so that users can easily handle errors.
8+
9+
Description: This rule can be detected by cargo clippy, but it does not warn by default.
10+
11+
**[Bad Case]**
12+
13+
```rust
14+
#![warn(clippy::missing_errors_doc)]
15+
16+
use std::io;
17+
// Bad: Clippy will be warned "warning: docs for function returning `Result` missing `# Errors` section"
18+
pub fn read(filename: String) -> io::Result<String> {
19+
unimplemented!();
20+
}
21+
```
22+
23+
**[Good Case]**
24+
25+
```rust
26+
#![warn(clippy::missing_errors_doc)]
27+
28+
use std::io;
29+
// Good:adding normatives Errors documentation comments
30+
31+
/// # Errors
32+
///
33+
/// Will return `Err` if `filename` does not exist or the user does not have
34+
/// permission to read it.
35+
pub fn read(filename: String) -> io::Result<String> {
36+
unimplemented!();
37+
}
38+
```
39+
40+
**[Lint detection]**
41+
42+
| Lint name | Clippy detectable | Rustc detectable | Lint Group | Default level |
43+
| ---------------------------------------------------------------------------------------------------- | ----------------- | ---------------- | ---------- | ------------- |
44+
| [missing_errors_doc ](https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc ) | yes | no | Style | allow |
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
# G.CMT.02 如果公开的API在某些情况下会发生Panic,则相应文档中需增加 Panic 注释
1+
# G.CMT.02 If a Panic occurs under certain circumstances in the public API, add a Panic comment to the corresponding document
2+
3+
**[Level]** Requirements
4+
5+
**[Description]**
6+
7+
In the public (pub) function documentation, it is recommended to add `# Panic` comments to explain under what conditions the function will Panic, so that users can pre-process it.
8+
9+
Description: This rule is detected by cargo clippy. It does not warn by default.
10+
11+
**[Bad Case]**
12+
13+
```rust
14+
#![warn(clippy::missing_panics_doc)]
15+
16+
// Bad:If do not add Panic relevant comments.`Clippy` will raise error: "warning: docs for function which may panic missing `# Panics` section"。
17+
pub fn divide_by(x: i32, y: i32) -> i32 {
18+
if y == 0 {
19+
panic!("Cannot divide by 0")
20+
} else {
21+
x / y
22+
}
23+
}
24+
```
25+
26+
**[Good Case]**
27+
28+
```rust
29+
#![warn(clippy::missing_panics_doc)]
30+
31+
// Good:Added Panic comments
32+
/// # Panics
33+
///
34+
/// Will panic if y is 0
35+
pub fn divide_by(x: i32, y: i32) -> i32 {
36+
if y == 0 {
37+
panic!("Cannot divide by 0")
38+
} else {
39+
x / y
40+
}
41+
}
42+
```
43+
44+
**[Lint detect]**
45+
46+
| Lint name | Clippy detectable | Rustc detectable | Lint Group | Default level |
47+
| ---------------------------------------------------------------------------------------------------- | ----------------- | ---------------- | ---------- | ------------- |
48+
| [missing_panics_doc ](https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc ) | yes | no | Style | allow |
49+
50+
Default is `allow`,But this pricipal need to be set `#![warn(clippy::missing_panics_doc)]`
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1-
# G.CMT.03 在文档注释中要使用空格代替 tab
1+
# G.CMT.03 Use spaces in document comments instead of tabs
2+
3+
**[Level]** Suggestion
4+
5+
**[Description]**
6+
7+
Rust code style promotes the use of **four spaces** instead of tabs, and **four spaces** should be used consistently in the documentation comments.
8+
9+
**[Bad Case]**
10+
11+
Tab is used in the following document comments.
12+
13+
```rust
14+
// Bad:Tab indentation is used in document comments
15+
///
16+
/// Struct to hold two strings:
17+
/// - first one
18+
/// - second one
19+
pub struct DoubleString {
20+
///
21+
/// - First String:
22+
/// - needs to be inside here
23+
first_string: String,
24+
///
25+
/// - Second String:
26+
/// - needs to be inside here
27+
second_string: String,
28+
}
29+
```
30+
31+
**[Good Case]**
32+
33+
```rust
34+
// Good:Four spaces indentation is used in document comments
35+
///
36+
/// Struct to hold two strings:
37+
/// - first one
38+
/// - second one
39+
pub struct DoubleString {
40+
///
41+
/// - First String:
42+
/// - needs to be inside here
43+
first_string: String,
44+
///
45+
/// - Second String:
46+
/// - needs to be inside here
47+
second_string: String,
48+
}
49+
```
50+
51+
**[Lint detect]**
52+
53+
| Lint name | Clippy detectable | Rustc detectable | Lint Group | Default level |
54+
| -------------------------------------------------------------------------------------------------------- | ----------------- | ---------------- | ---------- | ------------- |
55+
| [tabs_in_doc_comments ](https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments ) | yes | no | Style | warn |
56+
s
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1-
# P.CMT.03 使用行注释而避免使用块注释
1+
# P.CMT.03 Use line comments and avoid block comments
2+
3+
**[Description]**
4+
5+
Try to use line comments (`//` or `///`) rather than block comments. This is a convention in the Rust community.
6+
7+
For documentation comments, use `//! `, in other cases `///` is better.
8+
9+
Note: `#! [doc]` and `#[doc]` have a special role in simplifying document comments, and it is not necessary to force them into `//! ` or `//`.
10+
11+
**[Bad Case]**
12+
13+
```rust
14+
// Bad
15+
16+
/*
17+
* Wait for the main task to return, and set the process error code
18+
* appropriately.
19+
*/
20+
mod tests {
21+
//! This module contains tests
22+
23+
// ...
24+
}
25+
```
26+
27+
**[Good Case]**
28+
29+
When `normalize_comments = true`:
30+
31+
```rust
32+
// Good
33+
34+
// Wait for the main task to return, and set the process error code
35+
// appropriately.
36+
37+
// Good
38+
// When defining modules using the `mod` keyword,
39+
// it is better to use `///` on top of `mod`.
40+
41+
/// This module contains tests
42+
mod tests {
43+
// ...
44+
}
45+
46+
// Good
47+
#[doc = "Example item documentation"]
48+
pub enum Foo {}
49+
```
50+
51+
**【rustfmt configuration】**
52+
53+
| Parameter | Optional | Stable or not | Description |
54+
| -------------------------------------------------------------------------------------------- | -------------------------------- | ------------- | ------------------------------------------------------------------- |
55+
| [`normalize_comments`](https://rust-lang.github.io/rustfmt/?#normalize_comments) | false(default) true(suggestionn) | No | Convert the `/**/` comment to `/` |
56+
| [`normalize_doc_attributes`](https://rust-lang.github.io/rustfmt/?#normalize_doc_attributes) | false(default) | No | Convert the `#! [doc]` and `#[doc]` annotations to `//! ` and `///` |
Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1-
# P.CMT.04 文件头注释包含版权说明
1+
# P.CMT.04 File header comments include a copyright notice
2+
3+
**[Description]**
4+
5+
File header (i.e., module-level) comments should include the copyright notice first. If additional content needs to be added to the file header comments, it can be added under the copyright notice.
6+
7+
May include:
8+
9+
1. A description of the function of the document.
10+
2. Author.
11+
3. Creation date and last modification date.
12+
4. Notes.
13+
5. Open source license (e.g., Apache 2.0, BSD, LGPL, GPL).
14+
6. Other.
15+
16+
The format of the copyright notice is as follows:
17+
18+
- 中文版:`版权所有(c)XXX 技术有限公司 2015-2022`
19+
- English version: `Copyright (c) XXX Technologies Co. Ltd. 2015-2022. All rights reserved. Licensed under Apache-2.0.`
20+
21+
Its content can be adjusted to participate in the following detailed description:
22+
23+
- `2015-2022` can be modified as needed. 2015 is the year the file was first created and 2022 is the year the file was last modified. It is possible to write only one creation year, so that the copyright notice does not need to be changed if the file is modified frequently.
24+
- For internal use, there is no need to add `All rights reserved`.
25+
- `Licensed under Apache-2.0.`, if it is open source then you can add the license statement.
26+
27+
Caution when writing copyright notes:
28+
29+
- Copyright notes should be written from the top of the file header.
30+
- The header comment should contain the "copyright notice" first, followed by the rest of the content.
31+
- Optional content should be added as needed to avoid empty formatting without content.
32+
- Maintain uniform formatting, either by the project or by the larger context.
33+
- Maintain a neat layout, with line breaks aligned.
34+
35+
**[Good Case]**
36+
37+
```rust
38+
// Good
39+
// 版权所有(c)XXX 技术有限公司 2015-2022。
40+
41+
// Or
42+
43+
// Good
44+
// Copyright (c) XXX Technologies Co.Ltd. 2015-2022.
45+
// All rights reserved. Licensed under Apache-2.0.
46+
```
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
# P.CMT.05 在注释中使用 FIXME 和 TODO 来帮助任务协作
1+
# P.CMT.05 Use `FIXME` and `TODO` in comments to help with task collaboration
2+
3+
**[Description]**
4+
5+
Collaboration can be facilitated by turning on `FIXME` and `TODO` in the comments.
6+
7+
Note: This entry is not suitable for use with `rustfmt` related configuration items `report_fixme` and `report_todo`, which have been removed in `rustfmt` v2.0.
8+
9+
**[Good Case]**
10+
11+
```rust
12+
// Good
13+
// TODO(calebcartwright): consider enabling box_patterns feature gate
14+
fn annotation_type_for_level(level: Level) -> AnnotationType {
15+
match level {
16+
Level::Bug | Level::Fatal | Level::Error => AnnotationType::Error,
17+
Level::Warning => AnnotationType::Warning,
18+
Level::Note => AnnotationType::Note,
19+
Level::Help => AnnotationType::Help,
20+
// FIXME(#59346): Not sure how to map these two levels
21+
Level::Cancelled | Level::FailureNote => AnnotationType::Error,
22+
Level::Allow => panic!("Should not call with Allow"),
23+
}
24+
}
25+
```

0 commit comments

Comments
 (0)