Add clippy lints and format checks (#46)

The nightly rust toolchain is used because we are currently using unstable rustfmt settings.
This commit is contained in:
Ryan Johnson 2022-09-09 21:59:10 -07:00 committed by GitHub
parent 4920defe3e
commit fdedf74d29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 27 deletions

View file

@ -20,7 +20,13 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Build on ${{ matrix.os }}
run: cargo build --verbose
- uses: dtolnay/rust-toolchain@nightly
with:
components: clippy, rustfmt
- name: Validate formatting on ${{ matrix.os }}
run: cargo fmt --all -- --check
- name: Run clippy lints on ${{ matrix.os }}
run: cargo +stable clippy --verbose --no-deps --all-features
- name: Run tests on ${{ matrix.os }}
run: cargo test --verbose
run: cargo +stable test --verbose --all-features

View file

@ -547,7 +547,8 @@ impl<C: Config> Client<C> {
/// Sets the XP bar visible above hotbar and total experience.
///
/// # Arguments
/// * `bar` - Floating value in the range `0.0..=1.0` indicating progress on the XP bar.
/// * `bar` - Floating value in the range `0.0..=1.0` indicating progress on
/// the XP bar.
/// * `level` - Number above the XP bar.
/// * `total_xp` - TODO.
pub fn set_level(&mut self, bar: f32, level: i32, total_xp: i32) {
@ -562,7 +563,8 @@ impl<C: Config> Client<C> {
/// You can read more about hunger and saturation [here](https://minecraft.fandom.com/wiki/Food#Hunger_vs._Saturation).
///
/// # Arguments
/// * `health` - Float in range `0.0..=20.0`. Value `<=0` is legal and will kill the player.
/// * `health` - Float in range `0.0..=20.0`. Value `<=0` is legal and will
/// kill the player.
/// * `food` - Integer in range `0..=20`.
/// * `food_saturation` - Float in range `0.0..=5.0`.
pub fn set_health_and_food(&mut self, health: f32, food: i32, food_saturation: f32) {

View file

@ -652,13 +652,13 @@ impl From<EntitySpawnPacket> for S2cPlayPacket {
#[cfg(test)]
mod tests {
use std::num::NonZeroU32;
use uuid::Uuid;
use super::{Entities, EntityId, EntityKind};
use crate::{
config::Config,
slab_versioned::Key,
server::Server
};
use crate::config::Config;
use crate::server::Server;
use crate::slab_versioned::Key;
/// Created for the sole purpose of use during unit tests.
struct MockConfig;
@ -670,8 +670,10 @@ mod tests {
type ChunkState = ();
type PlayerListState = ();
fn max_connections(&self) -> usize { 10 }
fn update(&self, _server: &mut Server<Self>) { }
fn max_connections(&self) -> usize {
10
}
fn update(&self, _server: &mut Server<Self>) {}
}
#[test]
@ -680,7 +682,7 @@ mod tests {
let network_id: i32 = 8675309;
let entity_id = EntityId(Key::new(
202298,
NonZeroU32::new(network_id as u32).expect("Value given should never be zero!")
NonZeroU32::new(network_id as u32).expect("Value given should never be zero!"),
));
let uuid = Uuid::from_bytes([2; 16]);
assert!(entities.is_empty());
@ -697,7 +699,8 @@ mod tests {
let (player_id, player_entity) = entities.insert(EntityKind::Player, 1);
assert_eq!(player_entity.state, 1);
assert_eq!(entities.get(player_id).unwrap().state, 1);
let mut_player_entity = entities.get_mut(player_id)
let mut_player_entity = entities
.get_mut(player_id)
.expect("Failed to get mutable reference");
mut_player_entity.state = 100;
assert_eq!(entities.get(player_id).unwrap().state, 100);
@ -709,10 +712,12 @@ mod tests {
let mut entities: Entities<MockConfig> = Entities::new();
let uuid = Uuid::from_bytes([2; 16]);
assert!(entities.is_empty());
let (zombie_id, zombie_entity) = entities.insert_with_uuid(EntityKind::Zombie, uuid, 1)
let (zombie_id, zombie_entity) = entities
.insert_with_uuid(EntityKind::Zombie, uuid, 1)
.expect("Unexpected Uuid collision when inserting to an empty collection");
assert_eq!(zombie_entity.state, 1);
let maybe_zombie = entities.get_with_uuid(uuid)
let maybe_zombie = entities
.get_with_uuid(uuid)
.expect("Uuid lookup failed on item already added to this collection");
assert_eq!(zombie_id, maybe_zombie);
assert_eq!(entities.len(), 1);
@ -726,14 +731,18 @@ mod tests {
assert_eq!(boat_entity.state, 12);
let (cat_id, cat_entity) = entities.insert(EntityKind::Cat, 75);
assert_eq!(cat_entity.state, 75);
let maybe_boat_id = entities.get_with_network_id(boat_id.0.version.get() as i32)
let maybe_boat_id = entities
.get_with_network_id(boat_id.0.version.get() as i32)
.expect("Network id lookup failed on item already added to this collection");
let maybe_boat = entities.get(maybe_boat_id)
let maybe_boat = entities
.get(maybe_boat_id)
.expect("Failed to look up item already added to collection");
assert_eq!(maybe_boat.state, 12);
let maybe_cat_id = entities.get_with_network_id(cat_id.0.version.get() as i32)
let maybe_cat_id = entities
.get_with_network_id(cat_id.0.version.get() as i32)
.expect("Network id lookup failed on item already added to this collection");
let maybe_cat = entities.get(maybe_cat_id)
let maybe_cat = entities
.get(maybe_cat_id)
.expect("Failed to look up item already added to collection");
assert_eq!(maybe_cat.state, 75);
assert_eq!(entities.len(), 2);
@ -744,7 +753,8 @@ mod tests {
let mut entities: Entities<MockConfig> = Entities::new();
assert!(entities.is_empty());
let (player_id, _) = entities.insert(EntityKind::Player, 1);
let player_state = entities.remove(player_id)
let player_state = entities
.remove(player_id)
.expect("Failed to remove an item from the collection");
assert_eq!(player_state, 1);
}
@ -759,7 +769,7 @@ mod tests {
let (goat_id, _) = entities.insert(EntityKind::Goat, 120);
let (horse_id, _) = entities.insert(EntityKind::Horse, 30);
assert_eq!(entities.len(), 5);
entities.retain(|_id, entity| entity.state > 100 );
entities.retain(|_id, entity| entity.state > 100);
assert_eq!(entities.len(), 2);
assert!(entities.get(fox_id).is_some());
assert!(entities.get(goat_id).is_some());

View file

@ -1,6 +1,4 @@
//! Namespaced identifiers.
//!
//!
use std::borrow::Cow;
use std::io::Write;

View file

@ -1,6 +1,7 @@
use byteorder::ReadBytesExt;
use std::io::Write;
use byteorder::ReadBytesExt;
use crate::nbt::Compound;
use crate::protocol::{Decode, Encode, VarInt};
@ -58,9 +59,10 @@ impl Decode for Slot {
#[cfg(test)]
mod tests {
use super::*;
use serde_nbt::Value;
use super::*;
#[test]
fn slot_with_nbt() {
let mut buf: Vec<u8> = Vec::new();