我有一个应用程序分成几个箱子。我想拒绝或允许所有箱子中的特定 lint。例如:
#![deny(clippy::print_stdout)]
Run Code Online (Sandbox Code Playgroud)
看来我必须将其添加到每个板条箱中的 lib.rs 中。
Cargo有一张票据允许以某种方式进行配置,但它已经开放了好几年,没有明确的结论。
是否有解决方法可以避免每个板条箱重复这些允许/拒绝/警告行?
我的一个想法是通过在工作区根目录include!
创建一个,然后在每个板条箱的 lib.rs 中添加clippy_config.rs
include!("../../clippy_config.rs");
Run Code Online (Sandbox Code Playgroud)
然而这失败了
error: an inner attribute is not permitted in this context
--> app/src/../../clippy_config.rs:1:1
|
1 | #![deny(clippy::print_stdout)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.
Run Code Online (Sandbox Code Playgroud)
由于同样的原因,我使用宏的另一个想法也不起作用。
除了编写外部脚本来修改 Rust 文件以自动执行复制之外,是否有一种简单的方法可以做到这一点?(正如描述 Embark Studio 设置的评论中所提到的)。
我有一个有很多板条箱的工作区。我只想运行cargo clippy
其中之一,而不是其任何依赖项。
我该如何实现这个目标?
运行时cargo clippy
,它会抱怨这样的代码:
pub fn from_bytes(data: [u8; 72]) -> Stuff {
let mut ts = [0u8; 8];
let mut cs = [0u8; 64];
for b in 0..8 {
ts[b] = data[b];
}
for bb in 0..64 {
cs[bb] = data[bb + 8];
}
}
Run Code Online (Sandbox Code Playgroud)
和
pub fn from_bytes(data: [u8; 72]) -> Stuff {
let mut ts = [0u8; 8];
let mut cs = [0u8; 64];
for b in 0..8 {
ts[b] = data[b];
}
for bb in 0..64 { …
Run Code Online (Sandbox Code Playgroud) 该代码来自我的操作系统。
#[global_allocator]
pub static ALLOCATOR: LockedHeap = LockedHeap::empty();
Run Code Online (Sandbox Code Playgroud)
Clippy 说这个函数有太多参数。
error: this function has too many arguments (4/3)
--> src/mem/allocator/heap.rs:14:1
|
14 | pub static ALLOCATOR: LockedHeap = LockedHeap::empty();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> src/lib.rs:14:9
|
14 | #![deny(clippy::all)]
| ^^^^^^^^^^^
= note: `#[deny(clippy::too_many_arguments)]` implied by `#[deny(clippy::all)]`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more …
Run Code Online (Sandbox Code Playgroud) 我注意到为了使一段代码不被归类为死代码,它必须可以从所有二进制文件中访问。例子:
Cargo.toml:
[[bin]]
name = "main_one"
path = "src/main_one.rs"
[[bin]]
name = "main_two"
path = "src/main_two.rs"
main_one.rs:
mod utils;
fn main() {
print!("Hello, ");
utils::function_in_question();
}
main_two.rs:
mod utils;
fn main() {
print!("Hello, ");
// utils::function_in_question();
}
utils.rs:
pub fn function_in_question() {
println!("world!");
}
Run Code Online (Sandbox Code Playgroud)
这报告function_in_question
为死代码,即使它可以从main_one.rs
. 取消注释可解决此问题。如果它仅存在于main_two.rs
.
虽然这种行为背后有一些基本原理,但让 VSCode 一直抱怨这一点是很容易的 + Clippy 的输出被这些警告发送垃圾邮件。是否有至少在全球范围内抑制死代码检测的解决方案?应避免使用货物工作空间重构整个项目。
我正在使用 prost 为 protobuf 生成 Rust 类。我希望 Clippy 忽略这些生成的文件,但我无法弄清楚如何让 Clippy 忽略它们。
在我的 lib.rs 文件中,我有
pub mod modes {
#[allow(clippy)]
include!(concat!(env!("OUT_DIR"), "/modes.rs"));
}
#[allow(clippy)]
pub mod vehicle_features {
include!(concat!(env!("OUT_DIR"), "/vehicle_features.rs"));
}
Run Code Online (Sandbox Code Playgroud)
但是,我仍然收到 models.rs 和vehicle_features.rs 文件的警告。如何在不修改文件的情况下忽略clippy中的这些模块/文件。
编辑:根据以下建议,我将代码更改为:
pub mod modes {
#![allow(clippy::all)]
include!(concat!(env!("OUT_DIR"), "/modes.rs"));
}
pub mod vehicle_features {
#![allow(clippy::all)]
include!(concat!(env!("OUT_DIR"), "/vehicle_features.rs"));
}
Run Code Online (Sandbox Code Playgroud)
这在跑步时有效cargo clippy
,但在跑步时不起作用cargo clippy -- -W unwrap_used
有人知道为什么吗?当我向 Clippy 添加额外的警告参数时,如何使其工作?
编辑2:
我在这里找到了答案:How to disable a Clippy lint for a single line / block?
“clippy:all 实际上并不允许所有 lints,而是允许正确性、可疑性、风格、复杂性、cargo …
通常可以通过以下方式打印字符串:println!("{:#?}", foo)
其中{:#?}
语法将打印值的漂亮值。但我知道也可以将变量直接内联在大括号之间的字符串中,而不是将其列为宏的第二个参数,如下所示:println!("{foo}")
。
我的问题是 - 我可以结合漂亮的打印语法和内联字符串中的变量吗?
我从Clippy 的文档中找到了简写语法,但我找不到(或理解)如何将其与漂亮打印结合起来(如果可能的话)。
如果变体仅在需要时..._or_else()
执行
// example
let value = option.unwrap_or_else(|| compute_value(argument));
// only executed if `option` is of enum variant Option::None
Run Code Online (Sandbox Code Playgroud)
那么有没有什么情况是..._or()
有优势的呢?
我的理解是,如果里面的结果..._or_else()
已经计算出来了,那么using..._or()
就可以毫无缺陷地使用。但这种情况有什么好处吗?
..._or_else()
我尝试过情况并发现了建议从 更改为 的Clippy 规则..._or()
,但我很难理解该规则的原因:
为什么这样不好?
在某些情况下,使用即时求值会更短、更简单。
已知问题
Deref 和 Index 有可能产生副作用,但不建议这样做。急切地评估>它们可以改变程序的语义。
Clippy 发出以下警告:
warning: very complex type used. Consider factoring parts into `type` definitions
--> src/regexp/mod.rs:845:56
|
845 | pub fn get_by_name<'b>(&'b self, name: &'b str) -> Vec<(&'b String, (usize, usize), (usize, usize))> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity
= note: `#[warn(clippy::type_complexity)]` on by default
Run Code Online (Sandbox Code Playgroud)
我同意这个警告。当我输入这段代码时,我不喜欢它,但我似乎不知道在哪里以及如何放置该type
语句。我查过这本书,按照 Clippy 给出的参考链接进行搜索,似乎无法找到有关使用来type
简化函数签名或如何处理参数生命周期的参考String
。定义一个新的结构来保存返回值是可行的,但除了简化函数定义之外,实际上没有任何理由需要这样的结构。
这是代码。您可以看到我通过使用现有的 Report 结构来修复它 - 它有一些额外的字段不在我的返回设计中,但该结构已经存在,并且实际上使额外的字段可用可能会更好。但谢谢你的回答,我应该知道这里是否使用它。
pub struct Report {
/// The string found matching the RE pattern
pub found: String,
/// The …
Run Code Online (Sandbox Code Playgroud) 我很难修复不必要的收集剪辑警告。
pub fn import_selection(packages: &mut Vec<PackageRow>) -> io::Result<()> {
let file = fs::File::open("uad_exported_selection.txt")?;
let reader = BufReader::new(file);
let imported_selection: Vec<String> = reader
.lines()
.map(|l| l.expect("Could not exported selection"))
.collect();
for (i,p) in packages.iter_mut().enumerate() {
if imported_selection.contains(&p.name) {
p.selected = true;
...
} else {
p.selected = false;
}
}
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
我尝试直接从迭代器使用any()
,它编译但似乎不起作用(它应该找到任何东西)
在这种情况下真的可以删除收集吗?