Search code examples
rustrust-clippy

Why are some clippy lints gone if my modules are public?


I have a code that looks approximately like this:

// 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);
}

If I compile it exactly like this, it works without any errors or warnings.

However, if I change my pub mod bitboard to mod bitboard, then clippy starts giving me this warning:

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

I understand what clippy is telling me to do. But I do not understand why it does not suggest this when my module is declared as public. Any ideas?

P.S.: I am using rustc 1.60.0, clippy 0.1.60.

Edit: I should also add that this is not the only extra lint that happens when I remove the pub from my modules. For instance, I also had 1 new clippy::upper_case_acronyms, and 1 new clippy::enum_variant_names.

Edit 2: As requested, I'm including more examples to show the same behaviour happening to clippy::upper-case_acronyms and 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.

Solution

  • Because changing a public API like that is a breaking change.

    Changing the argument to pass by value like clippy recommends is easy if its an internal method, since you have control over all of the code that uses it. But if the argument is on a function or method exposed as part of the public API, changing it would require that all the other projects that use it change too, which is usually unacceptable for something as minor as passing a small Copy value by reference.