Rust Code Smells
Common code smells in Rust and how to fix them.
Unwrap/Expect Abuse
1// β Bad
2let value = some_option.unwrap();
3let result = some_result.expect("failed");
4
5// β
Good
6let value = some_option.ok_or(Error::MissingValue)?;
7let result = some_result?;
Clone Instead of Borrow
1// β Bad
2fn process(data: Vec<String>) {
3 for item in data.clone() {
4 println!("{}", item);
5 }
6}
7
8// β
Good
9fn process(data: &[String]) {
10 for item in data {
11 println!("{}", item);
12 }
13}
Not Using Iterators
1// β Bad
2let mut result = Vec::new();
3for i in 0..items.len() {
4 if items[i] > 10 {
5 result.push(items[i] * 2);
6 }
7}
8
9// β
Good
10let result: Vec<_> = items.iter()
11 .filter(|&&x| x > 10)
12 .map(|&x| x * 2)
13 .collect();
Manual String Building
1// β Bad
2let mut s = String::new();
3s.push_str("Hello");
4s.push_str(", ");
5s.push_str("world");
6
7// β
Good
8let s = format!("Hello, {}", "world");
Not Using Match
1// β Bad
2if result.is_ok() {
3 let value = result.unwrap();
4 process(value);
5} else {
6 handle_error(result.unwrap_err());
7}
8
9// β
Good
10match result {
11 Ok(value) => process(value),
12 Err(e) => handle_error(e),
13}
Related Snippets
- C/C++ Code Smells
Common code smells in C/C++ and how to fix them. Memory Leaks 1// β Bad 2void β¦ - C/C++ Secure Coding
Secure coding practices for C/C++ applications. Buffer Overflow Prevention 1// β β¦ - C/C++ Vulnerability Checks
Tools for checking vulnerabilities in C/C++ code. Valgrind 1# Install 2sudo apt β¦ - Common Antipatterns
Common software antipatterns to avoid across all languages and architectures. β¦ - Common Code Smells
Common code smells to watch for during code reviews with examples and fixes. β¦ - Developer Pre-Submission Checklist
Comprehensive checklist for developers before submitting a pull request. Code β¦ - Go Code Smells
Common code smells in Go and how to fix them. Ignoring Errors 1// β Bad 2result, β¦ - Go Secure Coding
Secure coding practices for Go applications. SQL Injection Prevention 1// β β¦ - Go Vulnerability Checks
Tools for checking vulnerabilities in Go code. Govulncheck 1# Install 2go β¦ - Haskell Code Smells
Common code smells in Haskell and how to fix them. Partial Functions 1-- β Bad: β¦ - Haskell Secure Coding
Secure coding practices for Haskell applications. SQL Injection Prevention 1-- β β¦ - Haskell Vulnerability Checks
Tools for checking vulnerabilities in Haskell code. Cabal Outdated 1# Check β¦ - Python Code Smells
Common code smells in Python and how to fix them. Mutable Default Arguments 1# β β¦ - Python Secure Coding
Secure coding practices for Python applications. SQL Injection Prevention 1# β β¦ - Python Vulnerability Checks
Tools for checking vulnerabilities in Python code. Safety - Dependency Scanner β¦ - Reviewer Checklist
Comprehensive checklist for code reviewers to ensure thorough and constructive β¦ - Rust Secure Coding
Secure coding practices for Rust applications. SQL Injection Prevention 1// β β¦ - Rust Vulnerability Checks
Tools for checking vulnerabilities in Rust code. Cargo Audit 1# Install 2cargo β¦ - TypeScript Code Smells
Common code smells in TypeScript and how to fix them. Using any 1// β Bad β¦ - TypeScript Secure Coding
Secure coding practices for TypeScript applications. XSS Prevention 1// β β¦ - TypeScript Vulnerability Checks
Tools for checking vulnerabilities in TypeScript/JavaScript code. npm audit 1# β¦