Loading...
Loading...
Code quality guide for Rust. USE WHEN: writing Rust code, reviewing code, or ensuring code quality.
npx skill4agent add masayuki-kono/agent-skills rust-code-quality-guideastry_from()map_err()unwrap_or(default)map_err()format!{var}#[allow(clippy::...)]unwrap_or(MAX_VALUE)// ❌ DANGEROUS - Silent failure with wrong value
let offset = u16::try_from(offset_value).unwrap_or(u16::MAX);
// ❌ DANGEROUS - Silent failure with wrong value (negative to zero)
let value_u8 = u8::try_from(value.max(0))
.map_err(|_| "value exceeds u8::MAX")?;
// ❌ DANGEROUS - Silent failure with wrong value
let value = u8::try_from(negative_value).unwrap_or(0);
// ✅ SAFE - Explicit error handling
let offset = u16::try_from(offset_value)
.map_err(|_| format!("offset {offset_value} exceeds u16::MAX"))?;
// ✅ SAFE - Explicit error handling (negative values cause error)
let value_u8 = u8::try_from(value)
.map_err(|_| format!("value {value} must be between 0 and 255"))?;unwrap_or(MAX_VALUE)max(0)unwrap_or(0)map_errmatchformat!// ❌ BAD - Causes clippy::uninlined_format_args warning
let message = format!("Error: {} occurred at line {}", error, line);
// ✅ GOOD - Use inline format arguments
let message = format!("Error: {error} occurred at line {line}");{variable}format!// ❌ BAD - Generic error message without context
return Err("Invalid count".into());
// ✅ GOOD - Include the actual parameter value
return Err(format!("Invalid count: {count} (must be 1-474)").into());
// ✅ GOOD - Include multiple parameters for complex validation
return Err(format!("Range exceeds maximum: {start}-{end} (max 99)").into());try_from()as// ❌ BAD - Silent truncation with as casting
let value = large_number as u8;
// ✅ GOOD - Explicit error handling with try_from
let value = u8::try_from(large_number)
.map_err(|_| format!("Value {large_number} exceeds u8::MAX"))?;astry_from()try_from()as#[allow(clippy::...)]// ❌ BAD - Suppressing warnings in production code
#[allow(clippy::too_many_lines)]
pub async fn process_request(...) {
// 100+ lines of code
}
// ✅ GOOD - Refactor the function to be smaller
pub async fn process_request(...) {
// Call smaller helper functions
handle_validation(...).await?;
}
async fn handle_validation(...) {
// Smaller, focused function
}#[allow(...)]unwrap_usedsignificant_drop_tightening