diff --git a/tests/everything.rs b/tests/everything.rs index 9cf0469..f5cb19d 100644 --- a/tests/everything.rs +++ b/tests/everything.rs @@ -39,7 +39,7 @@ fn compile_and_get_json_errors(file: &Path) -> Result> { use std::io::{Error, ErrorKind}; match res.status.code() { - Some(0) | Some(1) => Ok(stderr), + Some(0) | Some(1) | Some(101) => Ok(stderr), _ => Err(Box::new(Error::new( ErrorKind::Other, format!("failed with status {:?}: {}", res.status.code(), stderr), @@ -122,7 +122,7 @@ fn test_rustfix_with_file>(file: P) -> Result<(), Box> { let json_file = file.with_extension("json"); let fixed_file = file.with_extension("fixed.rs"); - debug!("{:?}", file); + debug!("next up: {:?}", file); let code = read_file(file)?; let errors = compile_and_get_json_errors(file)?; let suggestions = rustfix::get_suggestions_from_json(&errors, &HashSet::new()); @@ -143,7 +143,7 @@ fn test_rustfix_with_file>(file: P) -> Result<(), Box> { let mut fixed = code.clone(); - for sug in suggestions { + for sug in suggestions.into_iter().rev() { trace!("{:?}", sug); for sol in sug.solutions { trace!("{:?}", sol); @@ -155,6 +155,12 @@ fn test_rustfix_with_file>(file: P) -> Result<(), Box> { } } + if std::env::var("RUSTFIX_TEST_RECORD_FIXED_RUST").is_ok() { + use std::io::Write; + let mut recorded_rust = fs::File::create(&file.with_extension("recorded.rs"))?; + recorded_rust.write_all(fixed.as_bytes())?; + } + let expected_fixed = read_file(&fixed_file)?; assert_eq!(fixed.trim(), expected_fixed.trim(), "file doesn't look fixed"); @@ -170,7 +176,7 @@ fn get_fixture_files() -> Result, Box> { .filter(|p| p.is_file()) .filter(|p| { let x = p.to_string_lossy(); - x.ends_with(".rs") && !x.ends_with(".fixed.rs") + x.ends_with(".rs") && !x.ends_with(".fixed.rs") && !x.ends_with(".recorded.rs") }) .collect()) } diff --git a/tests/fixtures/.gitignore b/tests/fixtures/.gitignore index fd7a317..bfb599d 100644 --- a/tests/fixtures/.gitignore +++ b/tests/fixtures/.gitignore @@ -1 +1,2 @@ *.recorded.json +*.recorded.rs diff --git a/tests/fixtures/redundant_closure_call.fixed.rs b/tests/fixtures/redundant_closure_call.fixed.rs new file mode 100644 index 0000000..83b2de4 --- /dev/null +++ b/tests/fixtures/redundant_closure_call.fixed.rs @@ -0,0 +1,7 @@ +fn main() { + let a = 42; + + let b = 42; + + let c = "x"; +} diff --git a/tests/fixtures/redundant_closure_call.json b/tests/fixtures/redundant_closure_call.json new file mode 100644 index 0000000..2f48f63 --- /dev/null +++ b/tests/fixtures/redundant_closure_call.json @@ -0,0 +1,345 @@ +{ + "message": "Try not to call a closure in the expression where it is declared.", + "code": { + "code": "redundant_closure_call", + "explanation": null + }, + "level": "warning", + "spans": [ + { + "file_name": "./tests/fixtures/redundant_closure_call.rs", + "byte_start": 24, + "byte_end": 33, + "line_start": 2, + "line_end": 2, + "column_start": 13, + "column_end": 22, + "is_primary": true, + "text": [ + { + "text": " let a = (|| 42)();", + "highlight_start": 13, + "highlight_end": 22 + } + ], + "label": null, + "suggested_replacement": null, + "expansion": null + } + ], + "children": [ + { + "message": "#[warn(redundant_closure_call)] on by default", + "code": null, + "level": "note", + "spans": [], + "children": [], + "rendered": null + }, + { + "message": "Try doing something like: ", + "code": null, + "level": "help", + "spans": [ + { + "file_name": "./tests/fixtures/redundant_closure_call.rs", + "byte_start": 24, + "byte_end": 33, + "line_start": 2, + "line_end": 2, + "column_start": 13, + "column_end": 22, + "is_primary": true, + "text": [ + { + "text": " let a = (|| 42)();", + "highlight_start": 13, + "highlight_end": 22 + } + ], + "label": null, + "suggested_replacement": "42", + "expansion": null + } + ], + "children": [], + "rendered": null + } + ], + "rendered": "warning: Try not to call a closure in the expression where it is declared.\n --> ./tests/fixtures/redundant_closure_call.rs:2:13\n |\n2 | let a = (|| 42)();\n | ^^^^^^^^^ help: Try doing something like: : `42`\n |\n = note: #[warn(redundant_closure_call)] on by default\n\n" +} +{ + "message": "Try not to call a closure in the expression where it is declared.", + "code": { + "code": "redundant_closure_call", + "explanation": null + }, + "level": "warning", + "spans": [ + { + "file_name": "./tests/fixtures/redundant_closure_call.rs", + "byte_start": 48, + "byte_end": 70, + "line_start": 4, + "line_end": 6, + "column_start": 13, + "column_end": 8, + "is_primary": true, + "text": [ + { + "text": " let b = (||", + "highlight_start": 13, + "highlight_end": 16 + }, + { + "text": " 42", + "highlight_start": 1, + "highlight_end": 11 + }, + { + "text": " )();", + "highlight_start": 1, + "highlight_end": 8 + } + ], + "label": null, + "suggested_replacement": null, + "expansion": null + } + ], + "children": [ + { + "message": "Try doing something like: ", + "code": null, + "level": "help", + "spans": [ + { + "file_name": "./tests/fixtures/redundant_closure_call.rs", + "byte_start": 48, + "byte_end": 70, + "line_start": 4, + "line_end": 6, + "column_start": 13, + "column_end": 8, + "is_primary": true, + "text": [ + { + "text": " let b = (||", + "highlight_start": 13, + "highlight_end": 16 + }, + { + "text": " 42", + "highlight_start": 1, + "highlight_end": 11 + }, + { + "text": " )();", + "highlight_start": 1, + "highlight_end": 8 + } + ], + "label": null, + "suggested_replacement": "42", + "expansion": null + } + ], + "children": [], + "rendered": null + } + ], + "rendered": "warning: Try not to call a closure in the expression where it is declared.\n --> ./tests/fixtures/redundant_closure_call.rs:4:13\n |\n4 | let b = (||\n | _____________^\n5 | | 42\n6 | | )();\n | |_______^ help: Try doing something like: : `42`\n\n" +} +{ + "message": "Try not to call a closure in the expression where it is declared.", + "code": { + "code": "redundant_closure_call", + "explanation": null + }, + "level": "warning", + "spans": [ + { + "file_name": "./tests/fixtures/redundant_closure_call.rs", + "byte_start": 85, + "byte_end": 95, + "line_start": 8, + "line_end": 8, + "column_start": 13, + "column_end": 23, + "is_primary": true, + "text": [ + { + "text": " let c = (|| \"x\")();", + "highlight_start": 13, + "highlight_end": 23 + } + ], + "label": null, + "suggested_replacement": null, + "expansion": null + } + ], + "children": [ + { + "message": "Try doing something like: ", + "code": null, + "level": "help", + "spans": [ + { + "file_name": "./tests/fixtures/redundant_closure_call.rs", + "byte_start": 85, + "byte_end": 95, + "line_start": 8, + "line_end": 8, + "column_start": 13, + "column_end": 23, + "is_primary": true, + "text": [ + { + "text": " let c = (|| \"x\")();", + "highlight_start": 13, + "highlight_end": 23 + } + ], + "label": null, + "suggested_replacement": "\"x\"", + "expansion": null + } + ], + "children": [], + "rendered": null + } + ], + "rendered": "warning: Try not to call a closure in the expression where it is declared.\n --> ./tests/fixtures/redundant_closure_call.rs:8:13\n |\n8 | let c = (|| \"x\")();\n | ^^^^^^^^^^ help: Try doing something like: : `\"x\"`\n\n" +} +{ + "message": "unused variable: `a`", + "code": { + "code": "unused_variables", + "explanation": null + }, + "level": "warning", + "spans": [ + { + "file_name": "./tests/fixtures/redundant_closure_call.rs", + "byte_start": 20, + "byte_end": 21, + "line_start": 2, + "line_end": 2, + "column_start": 9, + "column_end": 10, + "is_primary": true, + "text": [ + { + "text": " let a = (|| 42)();", + "highlight_start": 9, + "highlight_end": 10 + } + ], + "label": null, + "suggested_replacement": null, + "expansion": null + } + ], + "children": [ + { + "message": "#[warn(unused_variables)] on by default", + "code": null, + "level": "note", + "spans": [], + "children": [], + "rendered": null + }, + { + "message": "to avoid this warning, consider using `_a` instead", + "code": null, + "level": "note", + "spans": [], + "children": [], + "rendered": null + } + ], + "rendered": "warning: unused variable: `a`\n --> ./tests/fixtures/redundant_closure_call.rs:2:9\n |\n2 | let a = (|| 42)();\n | ^\n |\n = note: #[warn(unused_variables)] on by default\n = note: to avoid this warning, consider using `_a` instead\n\n" +} +{ + "message": "unused variable: `b`", + "code": { + "code": "unused_variables", + "explanation": null + }, + "level": "warning", + "spans": [ + { + "file_name": "./tests/fixtures/redundant_closure_call.rs", + "byte_start": 44, + "byte_end": 45, + "line_start": 4, + "line_end": 4, + "column_start": 9, + "column_end": 10, + "is_primary": true, + "text": [ + { + "text": " let b = (||", + "highlight_start": 9, + "highlight_end": 10 + } + ], + "label": null, + "suggested_replacement": null, + "expansion": null + } + ], + "children": [ + { + "message": "to avoid this warning, consider using `_b` instead", + "code": null, + "level": "note", + "spans": [], + "children": [], + "rendered": null + } + ], + "rendered": "warning: unused variable: `b`\n --> ./tests/fixtures/redundant_closure_call.rs:4:9\n |\n4 | let b = (||\n | ^\n |\n = note: to avoid this warning, consider using `_b` instead\n\n" +} +{ + "message": "unused variable: `c`", + "code": { + "code": "unused_variables", + "explanation": null + }, + "level": "warning", + "spans": [ + { + "file_name": "./tests/fixtures/redundant_closure_call.rs", + "byte_start": 81, + "byte_end": 82, + "line_start": 8, + "line_end": 8, + "column_start": 9, + "column_end": 10, + "is_primary": true, + "text": [ + { + "text": " let c = (|| \"x\")();", + "highlight_start": 9, + "highlight_end": 10 + } + ], + "label": null, + "suggested_replacement": null, + "expansion": null + } + ], + "children": [ + { + "message": "to avoid this warning, consider using `_c` instead", + "code": null, + "level": "note", + "spans": [], + "children": [], + "rendered": null + } + ], + "rendered": "warning: unused variable: `c`\n --> ./tests/fixtures/redundant_closure_call.rs:8:9\n |\n8 | let c = (|| \"x\")();\n | ^\n |\n = note: to avoid this warning, consider using `_c` instead\n\n" +} diff --git a/tests/fixtures/redundant_closure_call.rs b/tests/fixtures/redundant_closure_call.rs new file mode 100644 index 0000000..fc0e740 --- /dev/null +++ b/tests/fixtures/redundant_closure_call.rs @@ -0,0 +1,9 @@ +fn main() { + let a = (|| 42)(); + + let b = (|| + 42 + )(); + + let c = (|| "x")(); +}