Skip to content

Commit

Permalink
Added extra lints
Browse files Browse the repository at this point in the history
  • Loading branch information
rm-dr committed Nov 9, 2024
1 parent 52cac98 commit 70de38c
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 8 deletions.
34 changes: 34 additions & 0 deletions copperd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,40 @@ needless_return = "allow"
new_without_default = "allow"
tabs_in_doc_comments = "allow"


# Extra
expect_used = "deny"
#unwrap_used = "deny"
#panic = "deny"
dbg_macro = "deny"
allow_attributes = "deny"
create_dir = "deny"
filetype_is_file = "deny"
integer_division = "deny"
lossy_float_literal = "deny"
map_err_ignore = "deny"
mutex_atomic = "deny"
needless_raw_strings = "deny"
print_stderr = "deny"
print_stdout = "deny"
str_to_string = "deny"
string_add = "deny"
string_to_string = "deny"
unimplemented = "deny"
use_debug = "deny"
verbose_file_reads = "deny"
#wildcard_enum_match_arm = "deny"

# Pedantic
large_types_passed_by_value = "deny"
match_on_vec_items = "deny"

# Cargo
wildcard_dependencies = "deny"
negative_feature_names = "deny"
redundant_feature_names = "deny"
#multiple_crate_versions = "deny"

[workspace.dependencies]
# Shared libraries
copper-util = { path = "lib/util" }
Expand Down
2 changes: 1 addition & 1 deletion copperd/bin/edged/src/api/storage/upload_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(super) async fn upload_part<Client: DatabaseClient, Itemdb: ItemdbClient>(
// Parse multipart data
let mut data: Option<Bytes> = None;
while let Some(field) = multipart.next_field().await.unwrap() {
let name = field.name().unwrap().to_string();
let name = field.name().unwrap().to_owned();

match &name[..] {
"part_data" => {
Expand Down
2 changes: 2 additions & 0 deletions copperd/bin/edged/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ async fn make_app(config: Arc<EdgedConfig>, s3_client: Arc<S3Client>) -> Router
async fn main() {
let config_res = match load_env::<EdgedConfig>() {
Ok(x) => x,

#[expect(clippy::print_stdout)]
Err(err) => {
println!("Error while loading .env: {err}");
std::process::exit(1);
Expand Down
2 changes: 2 additions & 0 deletions copperd/bin/piper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ mod pipeline;
async fn main() {
let config_res = match load_env::<PiperConfig>() {
Ok(x) => x,

#[expect(clippy::print_stdout)]
Err(err) => {
println!("Error while loading .env: {err}");
std::process::exit(1);
Expand Down
5 changes: 2 additions & 3 deletions copperd/lib/migrate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum MigrationError {

/// We could not deserialize a migration record
#[error("could not deserialize migration")]
MalformedMigrationRecord,
MalformedMigrationRecord(#[from] serde_json::Error),
}

/// A migration entry in the database,
Expand Down Expand Up @@ -105,8 +105,7 @@ impl<'a> Migrator<'a> {

let mut ap_migs: BTreeMap<SmartString<LazyCompact>, MigrationRecord> = BTreeMap::new();
for row in res {
let r: MigrationRecord = serde_json::from_str(row.get("val"))
.map_err(|_| MigrationError::MalformedMigrationRecord)?;
let r: MigrationRecord = serde_json::from_str(row.get("val"))?;
ap_migs.insert(r.name.clone(), r);
}

Expand Down
1 change: 1 addition & 0 deletions copperd/lib/util/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl<T> LoadedEnv<T> {

/// Load the configuration type `T` from the current environment,
/// including the `.env` if it exists.
#[expect(clippy::wildcard_enum_match_arm)]
pub fn load_env<T: DeserializeOwned>() -> Result<LoadedEnv<T>, EnvLoadError> {
let env_path = match dotenvy::dotenv() {
Ok(path) => Some(path),
Expand Down
15 changes: 14 additions & 1 deletion copperd/nodes/audiofile/src/common/vorbiscomment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,21 @@ impl VorbisComment {
let mut block = [0u8; 4];

let vendor = {
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| VorbisCommentDecodeError::MalformedData)?;

let length = u32::from_le_bytes(block);
let mut text = vec![0u8; length.try_into().unwrap()];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut text)
.map_err(|_| VorbisCommentDecodeError::MalformedData)?;

String::from_utf8(text)?
};

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| VorbisCommentDecodeError::MalformedData)?;
let n_comments: usize = u32::from_le_bytes(block).try_into().unwrap();
Expand All @@ -152,12 +155,14 @@ impl VorbisComment {
let mut pictures = Vec::new();
for _ in 0..n_comments {
let comment = {
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| VorbisCommentDecodeError::MalformedData)?;

let length = u32::from_le_bytes(block);
let mut text = vec![0u8; length.try_into().unwrap()];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut text)
.map_err(|_| VorbisCommentDecodeError::MalformedData)?;

Expand All @@ -172,6 +177,7 @@ impl VorbisComment {

if !val.is_empty() {
if var.to_uppercase() == "METADATA_BLOCK_PICTURE" {
#[expect(clippy::map_err_ignore)]
pictures.push(
FlacPictureBlock::decode(
&base64::prelude::BASE64_STANDARD
Expand Down Expand Up @@ -248,7 +254,11 @@ impl VorbisComment {
x -= x % 3;
x += 3;
}
sum += 4 * (x / 3);

#[expect(clippy::integer_division)]
{
sum += 4 * (x / 3);
}

// Add "METADATA_BLOCK_PICTURE="
sum += 23;
Expand Down Expand Up @@ -298,8 +308,11 @@ impl VorbisComment {

for p in &self.pictures {
let mut pic_data = Vec::new();

#[expect(clippy::map_err_ignore)]
p.encode(false, false, &mut pic_data)
.map_err(|_| VorbisCommentEncodeError::PictureEncodeError)?;

let pic_string = format!(
"METADATA_BLOCK_PICTURE={}",
&base64::prelude::BASE64_STANDARD.encode(&pic_data)
Expand Down
3 changes: 3 additions & 0 deletions copperd/nodes/audiofile/src/flac/blocks/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ impl FlacMetablockDecode for FlacApplicationBlock {
let mut d = Cursor::new(data);

let mut block = [0u8; 4];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;

let application_id = u32::from_be_bytes(block);

let data = {
Expand Down
17 changes: 14 additions & 3 deletions copperd/nodes/audiofile/src/flac/blocks/picture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,21 @@ impl FlacMetablockDecode for FlacPictureBlock {
// This is re-used whenever we need to read four bytes
let mut block = [0u8; 4];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;
let picture_type = PictureType::from_idx(u32::from_be_bytes(block))?;

// Image format
let mime = {
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;

let mime_length = u32::from_be_bytes(block).try_into().unwrap();
let mut mime = vec![0u8; mime_length];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut mime)
.map_err(|_| FlacDecodeError::MalformedBlock)?;

Expand All @@ -75,46 +78,54 @@ impl FlacMetablockDecode for FlacPictureBlock {

// Image description
let description = {
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;

let desc_length = u32::from_be_bytes(block).try_into().unwrap();
let mut desc = vec![0u8; desc_length];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut desc)
.map_err(|_| FlacDecodeError::MalformedBlock)?;

String::from_utf8(desc)?
};

// Image width
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;
let width = u32::from_be_bytes(block);

// Image height
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;
let height = u32::from_be_bytes(block);

// Image bit depth
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;
let depth = u32::from_be_bytes(block);

// Color count for indexed images
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;
let color_count = u32::from_be_bytes(block);

// Image data length
let img_data = {
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;

let data_length = u32::from_be_bytes(block).try_into().unwrap();
let mut img_data = vec![0u8; data_length];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut img_data)
.map_err(|_| FlacDecodeError::MalformedBlock)?;

Expand All @@ -138,8 +149,8 @@ impl FlacMetablockEncode for FlacPictureBlock {
fn get_len(&self) -> u32 {
(4 + (4 + self.mime.to_string().len())
+ (4 + self.description.len())
+ 4 + 4 + 4 + 4
+ (4 + self.img_data.len()))
+ 4 + 4 + 4
+ 4 + (4 + self.img_data.len()))
.try_into()
.unwrap()
}
Expand Down Expand Up @@ -167,7 +178,7 @@ impl FlacMetablockEncode for FlacPictureBlock {
drop(mime);

target.write_all(&u32::try_from(self.description.len()).unwrap().to_be_bytes())?;
target.write_all(self.description.to_string().as_bytes())?;
target.write_all(self.description.as_bytes())?;

target.write_all(&self.width.to_be_bytes())?;
target.write_all(&self.height.to_be_bytes())?;
Expand Down
15 changes: 15 additions & 0 deletions copperd/nodes/audiofile/src/flac/blocks/streaminfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,48 @@ impl FlacMetablockDecode for FlacStreaminfoBlock {

let min_block_size = {
let mut block = [0u8; 4];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block[2..])
.map_err(|_| FlacDecodeError::MalformedBlock)?;

u32::from_be_bytes(block)
};

let max_block_size = {
let mut block = [0u8; 4];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block[2..])
.map_err(|_| FlacDecodeError::MalformedBlock)?;

u32::from_be_bytes(block)
};

let min_frame_size = {
let mut block = [0u8; 4];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block[1..])
.map_err(|_| FlacDecodeError::MalformedBlock)?;

u32::from_be_bytes(block)
};

let max_frame_size = {
let mut block = [0u8; 4];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block[1..])
.map_err(|_| FlacDecodeError::MalformedBlock)?;

u32::from_be_bytes(block)
};

let (sample_rate, channels, bits_per_sample, total_samples) = {
let mut block = [0u8; 8];

#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;

Expand Down Expand Up @@ -117,6 +131,7 @@ impl FlacMetablockDecode for FlacStreaminfoBlock {

let md5_signature = {
let mut block = [0u8; 16];
#[expect(clippy::map_err_ignore)]
d.read_exact(&mut block)
.map_err(|_| FlacDecodeError::MalformedBlock)?;
block
Expand Down

0 comments on commit 70de38c

Please sign in to comment.