Skip to content

Commit 66e9949

Browse files
committed
Fix CI errors: suppress educational code warnings
- Add module-level clippy allows for documentation style warnings - Suppress warnings for educational code patterns - Fix unused variable by prefixing with underscore - Allow vec_init_then_push and approx_constant for clarity in examples
1 parent acfc4e4 commit 66e9949

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

src/buffer_overflow_prevention.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn safe_array_access() {
4040
let array = [1, 2, 3, 4, 5];
4141

4242
// Compile-time known indices are checked
43-
let _first = array[0]; // OK
43+
let _first = array[0]; // OK
4444

4545
// Runtime bounds checking (panics on out-of-bounds)
4646
// let _invalid = array[10]; // Would panic!
@@ -123,15 +123,16 @@ mod tests {
123123

124124
#[test]
125125
#[should_panic]
126+
#[allow(unconditional_panic, clippy::out_of_bounds_indexing)]
126127
fn test_out_of_bounds_panic() {
127128
let array = [1, 2, 3];
128-
let _ = array[10]; // Panics (controlled failure, not undefined behavior)
129+
let _ = array[10]; // Panics (controlled failure, not undefined behavior)
129130
}
130131

131132
#[test]
132133
fn test_safe_get() {
133134
let array = [1, 2, 3];
134135
assert_eq!(array.get(1), Some(&2));
135-
assert_eq!(array.get(10), None); // Safe handling
136+
assert_eq!(array.get(10), None); // Safe handling
136137
}
137138
}

src/data_race_prevention.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ struct SendableData {
102102

103103
/// This type is NOT Send due to raw pointer
104104
struct NotSendable {
105-
ptr: *mut i32, // Raw pointers are not Send
105+
ptr: *mut i32, // Raw pointers are not Send
106106
}
107107

108108
/// Compiler enforces thread safety
@@ -178,7 +178,7 @@ pub fn scoped_threads_safe() {
178178
});
179179

180180
// All spawned threads are joined before scope ends
181-
data.push(4); // Safe to modify again
181+
data.push(4); // Safe to modify again
182182
}
183183

184184
#[cfg(test)]

src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@
2121
//! These examples align with 2024 CISA/FBI guidance recommending memory-safe
2222
//! languages for critical infrastructure to eliminate 70% of security vulnerabilities.
2323
24+
#![allow(clippy::empty_line_after_doc_comments)]
25+
#![allow(clippy::mixed_attributes_style)]
26+
#![allow(dead_code)]
27+
#![allow(clippy::vec_init_then_push)]
28+
#![allow(clippy::approx_constant)]
29+
#![allow(clippy::useless_vec)]
30+
2431
pub mod buffer_overflow_prevention;
25-
pub mod use_after_free_prevention;
2632
pub mod data_race_prevention;
33+
pub mod use_after_free_prevention;
2734

2835
/// Module demonstrating buffer overflow prevention
2936
pub mod buffer_overflow {
@@ -134,14 +141,14 @@ pub mod use_after_free {
134141
}
135142

136143
/// Module demonstrating data race prevention
144+
///
145+
/// Data race prevention through ownership and type system
146+
///
147+
/// Rust prevents data races at compile time through the type system.
137148
pub mod data_race {
138149
use std::sync::{Arc, Mutex};
139150
use std::thread;
140151

141-
//! Data race prevention through ownership and type system
142-
//!
143-
//! Rust prevents data races at compile time through the type system.
144-
145152
/// Arc and Mutex for safe concurrent access
146153
pub fn safe_concurrent_access() {
147154
let counter = Arc::new(Mutex::new(0));
@@ -376,12 +383,12 @@ pub mod uninitialized_memory {
376383
}
377384

378385
/// Module demonstrating memory leak prevention with RAII
386+
///
387+
/// Memory leak prevention through RAII (Resource Acquisition Is Initialization)
379388
pub mod memory_leak {
380389
use std::fs::File;
381390
use std::io::Write;
382391

383-
//! Memory leak prevention through RAII (Resource Acquisition Is Initialization)
384-
385392
/// RAII ensures resources are cleaned up
386393
pub fn raii_file_handling() {
387394
// File automatically closed when it goes out of scope

src/use_after_free_prevention.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@
3333
3434
/// Example 1: Ownership prevents use-after-free
3535
pub fn ownership_prevents_uaf() {
36-
let data = Box::new(42); // Heap allocation
37-
let value = *data; // Copy the value
36+
let data = Box::new(42); // Heap allocation
37+
let value = *data; // Copy the value
3838

39-
drop(data); // Explicitly free memory
39+
drop(data); // Explicitly free memory
4040

4141
// This would NOT compile:
4242
// println!("{}", *data); // Compile error: value moved!
4343

44-
println!("Copied value: {}", value); // Safe: we copied the value
44+
println!("Copied value: {}", value); // Safe: we copied the value
4545
}
4646

4747
/// Example 2: References have lifetimes
4848
pub fn lifetime_prevents_dangling_ref() {
49-
let reference;
49+
let _reference: Option<&String> = None;
5050

5151
{
52-
let data = String::from("temporary");
52+
let _data = String::from("temporary");
5353
// This would NOT compile:
5454
// reference = &data; // Compile error: `data` doesn't live long enough
5555
}
@@ -61,15 +61,15 @@ pub fn lifetime_prevents_dangling_ref() {
6161
pub fn borrowing_prevents_uaf() {
6262
let mut data = vec![1, 2, 3];
6363

64-
let reference = &data[0]; // Immutable borrow
64+
let reference = &data[0]; // Immutable borrow
6565

6666
// This would NOT compile:
6767
// data.clear(); // Compile error: cannot mutate while borrowed!
6868

6969
println!("First element: {}", reference);
7070
// reference is no longer used, borrow ends
7171

72-
data.clear(); // Now we can mutate
72+
data.clear(); // Now we can mutate
7373
println!("Vector cleared");
7474
}
7575

@@ -87,7 +87,7 @@ pub fn shared_ownership_safe() {
8787
// Memory is freed only when ALL Rc references are dropped
8888
drop(clone1);
8989
drop(clone2);
90-
drop(data); // Now memory is freed
90+
drop(data); // Now memory is freed
9191
}
9292

9393
/// Example 5: Real-world pattern - safe object lifecycle
@@ -116,7 +116,7 @@ pub fn safe_object_usage() {
116116
let data_ref = obj.get_data();
117117
println!("Data: {:?}", data_ref);
118118

119-
let owned_data = obj.consume(); // obj is moved here
119+
let owned_data = obj.consume(); // obj is moved here
120120

121121
// This would NOT compile:
122122
// obj.get_data(); // Compile error: value used after move!

0 commit comments

Comments
 (0)