是否可以cargo clippy使用选项运行,以便自动修复警告?
从帮助消息来看,目前似乎不支持此选项。
我有一个带有 return 语句的宏,如下所示:
macro_rules! return_fail {
( $res:expr ) => {
match $res {
Ok(val) => val,
Err(e) => {
eprintln!(
"An error: on {}:{} {}; aborting current function.",
file!(),
line!(),
e
);
return;
}
}
};
}
fn bad(flag: bool) -> Result<(), String> {
if flag {
Ok(())
} else {
Err("u r idiot".to_string())
}
}
fn main() {
return_fail!(bad(true));
return_fail!(bad(false));
}
Run Code Online (Sandbox Code Playgroud)
当我在函数中间使用它时,这个宏工作正常,但是当我在函数末尾使用它时,我收到来自 Clippy 的警告:
warning: unneeded `return` statement
--> src/main.rs:12:17
|
12 | return;
| ^^^^^^^ help: …Run Code Online (Sandbox Code Playgroud) 我第一次尝试运行clippy(我知道......我现在真的应该这样做呃?)我面临一些错误.
我试图lint的项目取决于Piston,它编译并成功运行.但是,当我按照自述文件中的描述运行clippy时:
rustup run nightly cargo clippy
Run Code Online (Sandbox Code Playgroud)
看起来它开始尝试构建Piston并报告这样的错误:
error[E0433]: failed to resolve. Use of undeclared type or module `gfx`
--> /Users/Simon/.cargo/registry/src/github.com- 1ecc6299db9ec823/piston2d-gfx_graphics-0.31.2/src/back_end.rs:31:10
|
31 | pos: gfx::VertexBuffer<PositionFormat>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use of undeclared type or module `gfx`
error[E0433]: failed to resolve. Use of undeclared type or module `gfx`
--> /Users/Simon/.cargo/registry/src/github.com- 1ecc6299db9ec823/piston2d-gfx_graphics-0.31.2/src/back_end.rs:32:12
|
32 | color: gfx::VertexBuffer<ColorFormat>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use of undeclared type or module `gfx`
error[E0433]: failed to resolve. Use of undeclared type or module `gfx`
--> …Run Code Online (Sandbox Code Playgroud) 我得到一些看起来像这样的Clippy棉绒:
warning: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
--> src/helpers/mod.rs:29:32
|
29 | pub fn to_vec_sorted<U, F>(self, mapper: F) -> Vec<U>
| ^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention
Run Code Online (Sandbox Code Playgroud)
我处理这个皮棉没有问题,我选择了它是因为它不显示任何专有代码。假设我有一个很好的理由说明为什么需要这样命名函数,并且Clippy已集成到我的CI中,所以我需要零个Clippy错误/警告。
有没有一种方法可以禁用特定行或代码块的Clippy棉绒,类似于@SuppressWarnings("whatever")Java?我觉得一定有,但是在文档中找不到任何这样做的示例。
我正在检查代码中的Clippy调查结果,发现学究规则needless_pass_by_value可能是假阳性。
它说:
警告:此参数按值传递,但未在函数主体中使用
帮助:考虑参考:
&Arc<Mutex<MyStruct>>
由于克隆Arc只是参考计数,因此移动Arc并不是一个坏主意。在质量和性能方面,发送参考而不是价值真的有什么区别Arc吗?
#![warn(clippy::pedantic)]
use std::sync::{Arc, Mutex};
fn main() {
let my_struct = MyStruct { value: 3 };
let arc = Arc::new(Mutex::new(my_struct));
arc_taker(arc.clone());
}
fn arc_taker(prm: Arc<Mutex<MyStruct>>) {
prm.lock().unwrap().do_something();
}
struct MyStruct {
value: i32,
}
impl MyStruct {
fn do_something(&self) {
println!("self.value: {}", self.value);
}
}
Run Code Online (Sandbox Code Playgroud)
如何传递引用Arc<A>以便以下代码成功编译?
use std::sync::Arc;
trait A {
fn send(&self);
}
struct B;
impl A for B {
fn send(&self) {
println!("SENT");
}
}
fn ss(a: &Arc<A>) {
let aa = a.clone();
aa.send();
}
fn main() {
let a = Arc::new(B);
ss(&a);
}
Run Code Online (Sandbox Code Playgroud)
(游乐场)
如果我省略了引用,它编译好了,但Clippy警告我,在这种情况下没有任何意义.
Compiling playground v0.0.1 (file:///playground)
warning: this argument is passed by value, but not consumed in the function body
--> src/main.rs:13:10
|
13 | fn ss(a: Arc<A>) {
| ^^^^^^ …Run Code Online (Sandbox Code Playgroud)