Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
0373dc3a
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0373dc3a
编写于
1月 22, 2021
作者:
X
xFrednet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added documentation for adding a configuration to lints
* Fixed some spelling
上级
a905cf67
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
79 addition
and
2 deletion
+79
-2
doc/adding_lints.md
doc/adding_lints.md
+76
-0
doc/basics.md
doc/basics.md
+3
-2
未找到文件。
doc/adding_lints.md
浏览文件 @
0373dc3a
...
...
@@ -23,6 +23,7 @@ because that's clearly a non-descriptive name.
-
[
Running rustfmt
](
#running-rustfmt
)
-
[
Debugging
](
#debugging
)
-
[
PR Checklist
](
#pr-checklist
)
-
[
Adding configuration to a lint
](
#adding-configuration-to-a-lint
)
-
[
Cheatsheet
](
#cheatsheet
)
## Setup
...
...
@@ -526,6 +527,81 @@ Before submitting your PR make sure you followed all of the basic requirements:
-
\[
] Added lint documentation
-
\[
] Run
`cargo dev fmt`
## Adding configuration to a lint
Clippy supports the configuration of lints values using a
`clippy.toml`
file in the workspace
directory. Adding a configuration to a lint can be useful for thresholds or to constrain some
behavior that can be seen as a false positive for some users. Adding a configuration is done
in the following steps:
1.
Adding a new configuration entry to
[
clippy_lints::utils::conf
](
/clippy_lints/src/utils/conf.rs
)
like this:
```
rust
/// Lint: LINT_NAME. <The configuration field doc comment>
(
configuration_ident
,
"configuration_value"
:
Type
,
DefaultValue
),
```
The configuration value and identifier should usually be the same. The doc comment will be
automatically added to the lint documentation.
2.
Adding the configuration value to the lint impl struct:
1.
This first requires the definition of a lint impl struct. Lint impl structs are usually
generated with the
`declare_lint_pass!`
macro. This struct needs to be defined manually
to add some kind of metadata to it:
```
rust
// Generated struct definition
declare_lint_pass!
(
StructName
=>
[
LINT_NAME
]);
// New manual definition struct
#[derive(Copy,
Clone)]
pub
struct
StructName
{}
impl_lint_pass!
(
StructName
=>
[
LINT_NAME
]);
```
2.
Next add the configuration value and a corresponding creation method like this:
```
rust
#[derive(Copy,
Clone)]
pub
struct
StructName
{
configuration_ident
:
Type
,
}
// ...
impl
StructName
{
pub
fn
new
(
configuration_ident
:
Type
)
->
Self
{
Self
{
configuration_ident
,
}
}
}
```
3.
Passing the configuration value to the lint impl struct:
First find the struct construction in the [clippy_lints lib file](/clippy_lints/src/lib.rs).
Make sure that `clippy dev update_lints` added it beforehand. The configuration value is now
cloned or copied into a local value that is then passed to the impl struct like this:
```rust
// Default generated registration:
store.register_late_pass(|| box module::StructName);
// New registration with configuration value
let configuration_ident = conf.configuration_ident.clone();
store.register_late_pass(move || box module::StructName::new(configuration_ident));
```
Congratulations the work is almost done. The configuration value can now be accessed
in the linting code via `self.configuration_ident`.
4.
Adding tests:
1.
The default configured value can be tested like any normal lint in
[
`tests/ui`
](
/tests/ui
)
.
2.
The configuration itself will be tested separately in
[
`tests/ui-toml`
](
/tests/ui-toml
)
.
Simply add a new subfolder with a fitting name. This folder contains a
`clippy.toml`
file
with the configuration value and a rust file that should be linted by clippy. The test can
otherwise be written as usual.
## Cheatsheet
Here are some pointers to things you are likely going to need for every lint:
...
...
doc/basics.md
浏览文件 @
0373dc3a
...
...
@@ -11,6 +11,7 @@ the codebase take a look at [Adding Lints] or [Common Tools].
-
[
Get the Code
](
#get-the-code
)
-
[
Building and Testing
](
#building-and-testing
)
-
[
`cargo dev`
](
#cargo-dev
)
-
[
Common Abbreviations
](
#common-abbreviations
)
-
[
PR
](
#pr
)
## Get the Code
...
...
@@ -109,7 +110,7 @@ See <https://rustc-dev-guide.rust-lang.org/contributing.html#opening-a-pr>.
| TCX | Type context |
This is a concise list of abbreviations that can come up during clippy development. An extensive
genal list can be found in the
[
rustc-dev-guide glossary
][
glossary
]
. Always feel free to ask if
gen
er
al list can be found in the
[
rustc-dev-guide glossary
][
glossary
]
. Always feel free to ask if
an abbreviation or meaning is unclear to you.
[
glossary
]:
https://rustc-dev-guide.rust-lang.org/appendix/glossary.html
\ No newline at end of file
[
glossary
]:
https://rustc-dev-guide.rust-lang.org/appendix/glossary.html
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录