1
Fork 0

Use rustfix' suggestion fixing API

Uses branch from <https://github.com/rust-lang-nursery/rustfix/pull/63>
until we publish a new release.
This commit is contained in:
Pascal Hertleif 2018-05-02 00:32:31 +02:00 committed by Alex Crichton
parent fd6aa149bc
commit c02aedfcaf
4 changed files with 6 additions and 74 deletions

View file

@ -13,7 +13,7 @@ regex = "0.2"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
rustfix = { git = "https://github.com/rust-lang-nursery/rustfix" } rustfix = { git = "https://github.com/rust-lang-nursery/rustfix", branch = "apply_suggestion" }
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
libc = "0.2" libc = "0.2"

View file

@ -1,70 +0,0 @@
use rustfix::{get_suggestions_from_json, Replacement};
use std::collections::HashSet;
use std::error::Error;
pub fn run_rustfix(code: &str, json: &str) -> String {
let suggestions = get_suggestions_from_json(&json, &HashSet::new())
.expect("could not load suggestions");
let mut fixed = code.to_string();
for sug in suggestions.into_iter().rev() {
for sol in sug.solutions {
for r in sol.replacements {
fixed = apply_suggestion(&mut fixed, &r)
.expect("could not apply suggestion");
}
}
}
fixed
}
fn apply_suggestion(
file_content: &mut String,
suggestion: &Replacement,
) -> Result<String, Box<Error>> {
use std::cmp::max;
let mut new_content = String::new();
// Add the lines before the section we want to replace
new_content.push_str(&file_content
.lines()
.take(max(suggestion.snippet.line_range.start.line - 1, 0) as usize)
.collect::<Vec<_>>()
.join("\n"));
new_content.push_str("\n");
// Parts of line before replacement
new_content.push_str(&file_content
.lines()
.nth(suggestion.snippet.line_range.start.line - 1)
.unwrap_or("")
.chars()
.take(suggestion.snippet.line_range.start.column - 1)
.collect::<String>());
// Insert new content! Finally!
new_content.push_str(&suggestion.replacement);
// Parts of line after replacement
new_content.push_str(&file_content
.lines()
.nth(suggestion.snippet.line_range.end.line - 1)
.unwrap_or("")
.chars()
.skip(suggestion.snippet.line_range.end.column - 1)
.collect::<String>());
// Add the lines after the section we want to replace
new_content.push_str("\n");
new_content.push_str(&file_content
.lines()
.skip(suggestion.snippet.line_range.end.line as usize)
.collect::<Vec<_>>()
.join("\n"));
new_content.push_str("\n");
Ok(new_content)
}

View file

@ -53,7 +53,6 @@ pub mod common;
pub mod errors; pub mod errors;
mod raise_fd_limit; mod raise_fd_limit;
mod read2; mod read2;
mod autofix;
fn main() { fn main() {
env_logger::init(); env_logger::init();

View file

@ -35,7 +35,6 @@ use std::path::{Path, PathBuf};
use std::process::{Child, Command, ExitStatus, Output, Stdio}; use std::process::{Child, Command, ExitStatus, Output, Stdio};
use std::str; use std::str;
use autofix::run_rustfix;
use extract_gdb_version; use extract_gdb_version;
/// The name of the environment variable that holds dynamic library locations. /// The name of the environment variable that holds dynamic library locations.
@ -2607,10 +2606,14 @@ impl<'test> TestCx<'test> {
let fixture_path = expected_output_path(&self.testpaths, None, &None, UI_FIXED); let fixture_path = expected_output_path(&self.testpaths, None, &None, UI_FIXED);
if fixture_path.exists() { if fixture_path.exists() {
use std::collections::HashSet;
use rustfix::{apply_suggestions, get_suggestions_from_json};
let unfixed_code = self.load_expected_output_from_path(&self.testpaths.file) let unfixed_code = self.load_expected_output_from_path(&self.testpaths.file)
.unwrap(); .unwrap();
let expected_fixed = self.load_expected_output_from_path(&fixture_path).unwrap(); let expected_fixed = self.load_expected_output_from_path(&fixture_path).unwrap();
let fixed_code = run_rustfix(&unfixed_code, &proc_res.stderr); let suggestions = get_suggestions_from_json(&proc_res.stderr, &HashSet::new()).unwrap();
let fixed_code = apply_suggestions(&unfixed_code, &suggestions);
let errors = self.compare_output("rs.fixed", &fixed_code, &expected_fixed); let errors = self.compare_output("rs.fixed", &fixed_code, &expected_fixed);
if errors > 0 { if errors > 0 {
panic!("rustfix produced different fixed file!"); panic!("rustfix produced different fixed file!");