|
| 1 | +/* |
| 2 | +Live Test Rust File for Tier 2 Native Fixer Validation |
| 3 | +
|
| 4 | +This file contains INTENTIONAL issues that should be fixed by various tiers: |
| 5 | +
|
| 6 | +Tier 1/2 (rustfmt): |
| 7 | +- Inconsistent indentation |
| 8 | +- Missing/extra whitespace |
| 9 | +- Bad brace placement |
| 10 | +- Long lines that should be wrapped |
| 11 | +- Bad formatting in match arms |
| 12 | +- Inconsistent spacing around operators |
| 13 | +
|
| 14 | +Tier 2 (clippy --fix): |
| 15 | +- needless_return: Explicit return where implicit would work |
| 16 | +- redundant_clone: Cloning when not necessary |
| 17 | +- let_and_return: Let binding immediately returned |
| 18 | +- useless_vec: Vec where array/slice would work |
| 19 | +- map_clone: Using .map(|x| x.clone()) instead of .cloned() |
| 20 | +- single_match: Single-arm match that could be if-let |
| 21 | +
|
| 22 | +Tier 3 (AI Required - complex clippy lints): |
| 23 | +- unwrap_used: Using .unwrap() on Option/Result (needs error handling) |
| 24 | +- expect_used: Using .expect() that needs proper error handling |
| 25 | +- missing_errors_doc: Missing documentation for errors |
| 26 | +- cognitive_complexity: Functions that are too complex |
| 27 | +- panic: Using panic! that needs proper error handling |
| 28 | +
|
| 29 | +DO NOT manually fix these issues - they are test fixtures. |
| 30 | +*/ |
| 31 | + |
| 32 | +#![allow(dead_code)] |
| 33 | +#![allow(unused_variables)] |
| 34 | +#![allow(unused_imports)] |
| 35 | + |
| 36 | +use std::collections::HashMap; |
| 37 | +use std::fs::File; |
| 38 | +use std::io::{self, Read, Write}; |
| 39 | +use std::path::Path; |
| 40 | + |
| 41 | +// Tier 1 (rustfmt): Bad formatting - inconsistent spacing |
| 42 | +fn badly_formatted_function(a:i32,b:i32,c:&str)->String{ |
| 43 | +let result=format!("{} + {} = {}",a,b,c); |
| 44 | +return result; |
| 45 | +} |
| 46 | + |
| 47 | +// Tier 1 (rustfmt): Missing spaces around operators |
| 48 | +fn calculate_sum(x:i32,y:i32)->i32{ |
| 49 | + return x+y*2-1; |
| 50 | +} |
| 51 | + |
| 52 | +// Tier 2 (clippy needless_return): Explicit return not needed |
| 53 | +fn needless_return_example(x: i32) -> i32 { |
| 54 | + if x > 0 { |
| 55 | + return x * 2; |
| 56 | + } else { |
| 57 | + return x * -1; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Tier 2 (clippy needless_return): Simple case |
| 62 | +fn simple_needless_return(value: String) -> String { |
| 63 | + return value.to_uppercase(); |
| 64 | +} |
| 65 | + |
| 66 | +// Tier 2 (clippy let_and_return): Let binding immediately returned |
| 67 | +fn let_and_return_example(x: i32, y: i32) -> i32 { |
| 68 | + let result = x + y; |
| 69 | + return result; |
| 70 | +} |
| 71 | + |
| 72 | +// Tier 2 (clippy redundant_clone): Cloning when not necessary |
| 73 | +fn redundant_clone_example(s: &String) -> String { |
| 74 | + let owned = s.clone(); |
| 75 | + let cloned_again = owned.clone(); // redundant_clone |
| 76 | + cloned_again |
| 77 | +} |
| 78 | + |
| 79 | +// Tier 2 (clippy redundant_clone): Another example |
| 80 | +fn process_string_redundant(input: String) -> String { |
| 81 | + let s = input.clone(); // redundant - input is already owned |
| 82 | + s.to_uppercase() |
| 83 | +} |
| 84 | + |
| 85 | +// Tier 1 (rustfmt): Bad struct formatting |
| 86 | +struct BadlyFormattedStruct{ |
| 87 | +name:String, |
| 88 | +value: i32, |
| 89 | +enabled: bool, |
| 90 | +} |
| 91 | + |
| 92 | +// Tier 1 (rustfmt): Bad impl formatting |
| 93 | +impl BadlyFormattedStruct{ |
| 94 | +fn new(name:String,value:i32)->Self{ |
| 95 | +Self{name:name,value:value,enabled:true} |
| 96 | +} |
| 97 | +fn get_name(&self)-> &str{ |
| 98 | +&self.name |
| 99 | +} |
| 100 | +} |
| 101 | + |
| 102 | +// Tier 2 (clippy useless_vec): Vec where slice would work |
| 103 | +fn useless_vec_example() { |
| 104 | + let _numbers = vec![1, 2, 3, 4, 5]; |
| 105 | + for n in vec![1, 2, 3].iter() { // useless_vec: could be [1, 2, 3].iter() |
| 106 | + println!("{}", n); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Tier 2 (clippy map_clone): Using map with clone instead of cloned |
| 111 | +fn map_clone_example(strings: &[String]) -> Vec<String> { |
| 112 | + strings.iter().map(|s| s.clone()).collect() // clippy: map_clone |
| 113 | +} |
| 114 | + |
| 115 | +// Tier 2 (clippy single_match): Single match arm that could be if-let |
| 116 | +fn single_match_example(option: Option<i32>) { |
| 117 | + match option { |
| 118 | + Some(value) => println!("Got value: {}", value), |
| 119 | + None => {}, |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// Tier 1 (rustfmt): Bad match formatting |
| 124 | +fn bad_match_formatting(x: i32) -> &'static str { |
| 125 | +match x{ |
| 126 | +0=>"zero", |
| 127 | +1=>"one", |
| 128 | +2=>"two", |
| 129 | +_=>"other", |
| 130 | +} |
| 131 | +} |
| 132 | + |
| 133 | +// Tier 3 (clippy unwrap_used): Using unwrap on Result - needs proper error handling |
| 134 | +fn unwrap_example(path: &str) -> String { |
| 135 | + let mut file = File::open(path).unwrap(); // Tier 3: unwrap_used |
| 136 | + let mut contents = String::new(); |
| 137 | + file.read_to_string(&mut contents).unwrap(); // Tier 3: unwrap_used |
| 138 | + contents |
| 139 | +} |
| 140 | + |
| 141 | +// Tier 3 (clippy expect_used): Using expect - needs proper error handling |
| 142 | +fn expect_example(data: &str) -> i32 { |
| 143 | + data.parse::<i32>().expect("Failed to parse integer") // Tier 3: expect_used |
| 144 | +} |
| 145 | + |
| 146 | +// Tier 3 (AI required): Complex function with multiple unwraps and panics |
| 147 | +fn complex_error_handling(path: &str, key: &str) -> String { |
| 148 | + let file = File::open(path).unwrap(); // Tier 3: unwrap |
| 149 | + let mut contents = String::new(); |
| 150 | + let mut reader = io::BufReader::new(file); |
| 151 | + |
| 152 | + // This is intentionally complex to require AI understanding |
| 153 | + let map: HashMap<String, String> = HashMap::new(); |
| 154 | + let value = map.get(key).unwrap(); // Tier 3: unwrap on Option |
| 155 | + |
| 156 | + if value.is_empty() { |
| 157 | + panic!("Value cannot be empty!"); // Tier 3: panic |
| 158 | + } |
| 159 | + |
| 160 | + value.clone() |
| 161 | +} |
| 162 | + |
| 163 | +// Tier 1 (rustfmt): Long line that should be wrapped |
| 164 | +fn very_long_function_name(very_long_parameter_name_one: String, very_long_parameter_name_two: String, very_long_parameter_name_three: String) -> String { return very_long_parameter_name_one + &very_long_parameter_name_two + &very_long_parameter_name_three; } |
| 165 | + |
| 166 | +// Tier 2 (clippy needless_return): In closure |
| 167 | +fn needless_return_in_closure() -> Vec<i32> { |
| 168 | + let numbers = vec![1, 2, 3, 4, 5]; |
| 169 | + numbers.iter().map(|x| { |
| 170 | + return x * 2; |
| 171 | + }).collect() |
| 172 | +} |
| 173 | + |
| 174 | +// Tier 1 (rustfmt): Bad if formatting |
| 175 | +fn bad_if_formatting(x:i32){if x>0{println!("Positive")}else{println!("Non-positive")}} |
| 176 | + |
| 177 | +// Tier 2 (clippy redundant_clone): With methods |
| 178 | +fn redundant_method_clone() { |
| 179 | + let s = String::from("hello"); |
| 180 | + let s2 = s.clone(); |
| 181 | + let s3 = s2.clone(); // redundant_clone - s2 not used after this |
| 182 | + println!("{}", s3); |
| 183 | +} |
| 184 | + |
| 185 | +// Tier 1 (rustfmt): Bad array/vec formatting |
| 186 | +fn create_vector() -> Vec<i32> { |
| 187 | + vec![1,2,3,4,5,6,7,8,9,10] |
| 188 | +} |
| 189 | + |
| 190 | +fn create_hashmap() -> HashMap<String, i32> { |
| 191 | + let mut map=HashMap::new(); |
| 192 | + map.insert("one".to_string(),1); |
| 193 | + map.insert("two".to_string(),2); |
| 194 | + map.insert("three".to_string(),3); |
| 195 | + return map; |
| 196 | +} |
| 197 | + |
| 198 | +// Tier 3 (AI required): Function with potential panic that needs Result return |
| 199 | +fn divide_numbers(a: i32, b: i32) -> i32 { |
| 200 | + if b == 0 { |
| 201 | + panic!("Division by zero!"); // Tier 3: should return Result<i32, Error> |
| 202 | + } |
| 203 | + a / b |
| 204 | +} |
| 205 | + |
| 206 | +// Tier 3 (AI required): Index access without bounds check |
| 207 | +fn get_element_unsafe(vec: &Vec<i32>, index: usize) -> i32 { |
| 208 | + vec[index] // Tier 3: could panic, should use .get() and handle None |
| 209 | +} |
| 210 | + |
| 211 | +// Tier 2 (clippy let_and_return): Multiple instances |
| 212 | +fn multiple_let_and_return(a: i32, b: i32) -> i32 { |
| 213 | + let sum = a + b; |
| 214 | + let doubled = sum * 2; |
| 215 | + let result = doubled + 1; |
| 216 | + return result; |
| 217 | +} |
| 218 | + |
| 219 | +// Tier 1 (rustfmt): Bad tuple formatting |
| 220 | +fn create_tuple() -> (i32,i32,String) { |
| 221 | + (1,2,"three".to_string()) |
| 222 | +} |
| 223 | + |
| 224 | +// Combined issues: formatting + clippy |
| 225 | +fn combined_issues(s:&String)->String{ |
| 226 | +let cloned=s.clone(); |
| 227 | +let result=cloned.to_uppercase(); |
| 228 | +return result; |
| 229 | +} |
| 230 | + |
| 231 | +// Tier 2 (clippy single_match): Another example |
| 232 | +fn another_single_match(result: Result<i32, &str>) { |
| 233 | + match result { |
| 234 | + Ok(v) => println!("Success: {}", v), |
| 235 | + Err(_) => {}, |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +// Tier 3 (AI required): Complex logic with multiple potential failure points |
| 240 | +fn process_config_file(path: &str) -> HashMap<String, String> { |
| 241 | + let content = std::fs::read_to_string(path).unwrap(); // Tier 3: unwrap |
| 242 | + let mut config = HashMap::new(); |
| 243 | + |
| 244 | + for line in content.lines() { |
| 245 | + if line.trim().is_empty() || line.starts_with('#') { |
| 246 | + continue; |
| 247 | + } |
| 248 | + let parts: Vec<&str> = line.splitn(2, '=').collect(); |
| 249 | + if parts.len() != 2 { |
| 250 | + panic!("Invalid config line: {}", line); // Tier 3: panic |
| 251 | + } |
| 252 | + config.insert(parts[0].trim().to_string(), parts[1].trim().to_string()); |
| 253 | + } |
| 254 | + |
| 255 | + config |
| 256 | +} |
| 257 | + |
| 258 | +fn main() { |
| 259 | + // Basic usage to prevent "declared but not used" warnings |
| 260 | + println!("{}",badly_formatted_function(1,2,"test")); |
| 261 | + println!("{}",calculate_sum(5,3)); |
| 262 | + println!("{}",needless_return_example(10)); |
| 263 | + println!("{}",simple_needless_return("hello".to_string())); |
| 264 | + println!("{}",let_and_return_example(3,4)); |
| 265 | + |
| 266 | + let s = String::from("test"); |
| 267 | + println!("{}",redundant_clone_example(&s)); |
| 268 | + println!("{}",process_string_redundant("input".to_string())); |
| 269 | + |
| 270 | + useless_vec_example(); |
| 271 | + let strings = vec!["a".to_string(),"b".to_string()]; |
| 272 | + println!("{:?}",map_clone_example(&strings)); |
| 273 | + single_match_example(Some(42)); |
| 274 | + println!("{}",bad_match_formatting(1)); |
| 275 | + |
| 276 | + println!("{:?}",needless_return_in_closure()); |
| 277 | + bad_if_formatting(5); |
| 278 | + redundant_method_clone(); |
| 279 | + println!("{:?}",create_vector()); |
| 280 | + println!("{:?}",create_hashmap()); |
| 281 | + println!("{}",multiple_let_and_return(1,2)); |
| 282 | + println!("{:?}",create_tuple()); |
| 283 | + println!("{}",combined_issues(&s)); |
| 284 | + another_single_match(Ok(10)); |
| 285 | +} |
0 commit comments