我有一个大致如下的代码:
// src/bitboard.rs
#[derive(Copy, Clone, Debug)]
pub struct Bitboard {
value: u64
}
impl Bitboard {
pub const fn new(value: u64) -> Self {
Self { value }
}
pub const fn get_bit(&self, k: u64) -> u64 {
((1u64 << k) & self.value) >> k
}
}
// src/main.rs
pub mod bitboard;
use crate::bitboard::Bitboard;
fn main() {
let bitboard = Bitboard::new(0);
dbg!(bitboard);
}如果我像这样编译它,它的工作没有任何错误或警告。
但是,如果我将pub mod bitboard更改为mod bitboard,那么clippy就会给我以下警告:
warning: this argument (8 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
pub const fn get_bit(&self, k: u64) -> u64 {
^^^^^ help: consider passing by value instead: `self`
= note: `-W clippy::trivially-copy-pass-by-ref` implied by `-W clippy::pedantic`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref我明白克里皮要我做什么。但是,当我的模块被声明为公共时,我不明白为什么它不建议这样做。有什么想法吗?
P.S.:我使用的是锈蚀1.60.0,剪裁0.1.60。
编辑:我还应该补充一点,当我从模块中删除pub时,这并不是唯一的额外的链接。例如,我还有一个新的clippy::upper_case_acronyms和一个新的clippy::enum_variant_names。
编辑2:根据要求,我将包括更多的示例,以显示同样的行为发生在clippy::requested _acronyms和clippy::enum_variant_names上:
// src/fen.rs
pub struct FEN; // upper case acronyms happens here
pub enum FENValidationError { // enum variant names happens here
InvalidFieldsCount,
InvalidPiecePlacement,
InvalidRankSize,
}
// src/main.rs
mod fen; // same thing here. If this becomes `pub mod fen`, the two lint warnings above disappear.发布于 2022-05-29 02:17:29
因为像这样改变一个公共API是一个巨大的改变。
如果将参数更改为按值传递(如clippy建议),如果它是一个内部方法,则很容易,因为您可以控制使用它的所有代码。但是,如果参数是公开为公共API的一部分的函数或方法,则更改它将需要使用它的所有其他项目也进行更改,这通常是不能接受的,就像通过引用传递一个小的Copy值一样。
https://stackoverflow.com/questions/72420004
复制相似问题