Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit 820dda2

Browse files
authored
Merge pull request #63 from rust-lang-nursery/apply_suggestion
Apply suggestion
2 parents 1cbe64c + a5123f0 commit 820dda2

19 files changed

+62
-899
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ language: rust
22
rust:
33
- nightly
44
- stable
5-
before_script:
6-
- if [ $TRAVIS_RUST_VERSION == nightly ]; then cargo install clippy --git https://github.com/rust-lang-nursery/rust-clippy.git --force; fi
75
script:
86
- cargo build
97
- if [ $TRAVIS_RUST_VERSION == nightly ]; then cargo test -- --nocapture ; fi

src/lib.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern crate serde_derive;
33
extern crate serde_json;
44

55
use std::collections::HashSet;
6+
use std::error::Error;
67

78
pub mod diagnostics;
89
use diagnostics::{Diagnostic, DiagnosticSpan};
@@ -165,3 +166,59 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(diagnostic: &Diagnostic,
165166
})
166167
}
167168
}
169+
170+
pub fn apply_suggestion(file_content: &mut String, suggestion: &Replacement) -> String {
171+
use std::cmp::max;
172+
173+
let mut new_content = String::new();
174+
175+
// Add the lines before the section we want to replace
176+
new_content.push_str(&file_content.lines()
177+
.take(max(suggestion.snippet.line_range.start.line - 1, 0) as usize)
178+
.collect::<Vec<_>>()
179+
.join("\n"));
180+
new_content.push_str("\n");
181+
182+
// Parts of line before replacement
183+
new_content.push_str(&file_content.lines()
184+
.nth(suggestion.snippet.line_range.start.line - 1)
185+
.unwrap_or("")
186+
.chars()
187+
.take(suggestion.snippet.line_range.start.column - 1)
188+
.collect::<String>());
189+
190+
// Insert new content! Finally!
191+
new_content.push_str(&suggestion.replacement);
192+
193+
// Parts of line after replacement
194+
new_content.push_str(&file_content.lines()
195+
.nth(suggestion.snippet.line_range.end.line - 1)
196+
.unwrap_or("")
197+
.chars()
198+
.skip(suggestion.snippet.line_range.end.column - 1)
199+
.collect::<String>());
200+
201+
// Add the lines after the section we want to replace
202+
new_content.push_str("\n");
203+
new_content.push_str(&file_content.lines()
204+
.skip(suggestion.snippet.line_range.end.line as usize)
205+
.collect::<Vec<_>>()
206+
.join("\n"));
207+
new_content.push_str("\n");
208+
209+
new_content
210+
}
211+
212+
pub fn apply_suggestions(code: &str, suggestions: &[Suggestion]) -> String {
213+
let mut fixed = code.to_string();
214+
215+
for sug in suggestions.iter().rev() {
216+
for sol in &sug.solutions {
217+
for r in &sol.replacements {
218+
fixed = apply_suggestion(&mut fixed, r);
219+
}
220+
}
221+
}
222+
223+
fixed
224+
}

src/main.rs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -398,44 +398,8 @@ fn indent(size: u32, s: &str) -> String {
398398
/// This function is as stupid as possible. Make sure you call for the replacemnts in one file in
399399
/// reverse order to not mess up the lines for replacements further down the road.
400400
fn apply_suggestion(suggestion: &Replacement) -> Result<(), ProgramError> {
401-
use std::cmp::max;
402-
403-
let file_content = try!(read_file_to_string(&suggestion.snippet.file_name));
404-
let mut new_content = String::new();
405-
406-
// Add the lines before the section we want to replace
407-
new_content.push_str(&file_content.lines()
408-
.take(max(suggestion.snippet.line_range.start.line - 1, 0) as usize)
409-
.collect::<Vec<_>>()
410-
.join("\n"));
411-
new_content.push_str("\n");
412-
413-
// Parts of line before replacement
414-
new_content.push_str(&file_content.lines()
415-
.nth(suggestion.snippet.line_range.start.line - 1)
416-
.unwrap_or("")
417-
.chars()
418-
.take(suggestion.snippet.line_range.start.column - 1)
419-
.collect::<String>());
420-
421-
// Insert new content! Finally!
422-
new_content.push_str(&suggestion.replacement);
423-
424-
// Parts of line after replacement
425-
new_content.push_str(&file_content.lines()
426-
.nth(suggestion.snippet.line_range.end.line - 1)
427-
.unwrap_or("")
428-
.chars()
429-
.skip(suggestion.snippet.line_range.end.column - 1)
430-
.collect::<String>());
431-
432-
// Add the lines after the section we want to replace
433-
new_content.push_str("\n");
434-
new_content.push_str(&file_content.lines()
435-
.skip(suggestion.snippet.line_range.end.line as usize)
436-
.collect::<Vec<_>>()
437-
.join("\n"));
438-
new_content.push_str("\n");
401+
let mut file_content = try!(read_file_to_string(&suggestion.snippet.file_name));
402+
let new_content = rustfix::apply_suggestion(&mut file_content, suggestion);
439403

440404
let mut file = try!(File::create(&suggestion.snippet.file_name));
441405
let new_content = new_content.as_bytes();

tests/everything.rs

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use std::collections::HashSet;
1313
use std::process::Output;
1414
use tempdir::TempDir;
1515

16-
use rustfix::Replacement;
16+
use rustfix::{Replacement, apply_suggestions};
1717

1818
fn compile(file: &Path) -> Result<Output, Box<Error>> {
1919
let tmp = TempDir::new("rustfix-tests")?;
2020
let better_call_clippy = cmd!(
21-
"clippy-driver", "rustc", file,
21+
"rustc", file,
2222
"--error-format=pretty-json", "-Zunstable-options", "--emit=metadata",
2323
"--crate-name=rustfix_test",
2424
"--out-dir", tmp.path()
@@ -75,48 +75,6 @@ fn read_file(path: &Path) -> Result<String, Box<Error>> {
7575
Ok(buffer)
7676
}
7777

78-
fn apply_suggestion(file_content: &mut String, suggestion: &Replacement) -> Result<String, Box<Error>> {
79-
use std::cmp::max;
80-
81-
let mut new_content = String::new();
82-
83-
// Add the lines before the section we want to replace
84-
new_content.push_str(&file_content.lines()
85-
.take(max(suggestion.snippet.line_range.start.line - 1, 0) as usize)
86-
.collect::<Vec<_>>()
87-
.join("\n"));
88-
new_content.push_str("\n");
89-
90-
// Parts of line before replacement
91-
new_content.push_str(&file_content.lines()
92-
.nth(suggestion.snippet.line_range.start.line - 1)
93-
.unwrap_or("")
94-
.chars()
95-
.take(suggestion.snippet.line_range.start.column - 1)
96-
.collect::<String>());
97-
98-
// Insert new content! Finally!
99-
new_content.push_str(&suggestion.replacement);
100-
101-
// Parts of line after replacement
102-
new_content.push_str(&file_content.lines()
103-
.nth(suggestion.snippet.line_range.end.line - 1)
104-
.unwrap_or("")
105-
.chars()
106-
.skip(suggestion.snippet.line_range.end.column - 1)
107-
.collect::<String>());
108-
109-
// Add the lines after the section we want to replace
110-
new_content.push_str("\n");
111-
new_content.push_str(&file_content.lines()
112-
.skip(suggestion.snippet.line_range.end.line as usize)
113-
.collect::<Vec<_>>()
114-
.join("\n"));
115-
new_content.push_str("\n");
116-
117-
Ok(new_content)
118-
}
119-
12078
fn test_rustfix_with_file<P: AsRef<Path>>(file: P) -> Result<(), Box<Error>> {
12179
let file: &Path = file.as_ref();
12280
let json_file = file.with_extension("json");
@@ -141,19 +99,7 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P) -> Result<(), Box<Error>> {
14199
"got unexpected suggestions from clippy",
142100
);
143101

144-
let mut fixed = code.clone();
145-
146-
for sug in suggestions.into_iter().rev() {
147-
trace!("{:?}", sug);
148-
for sol in sug.solutions {
149-
trace!("{:?}", sol);
150-
for r in sol.replacements {
151-
debug!("replaced.");
152-
trace!("{:?}", r);
153-
fixed = apply_suggestion(&mut fixed, &r)?;
154-
}
155-
}
156-
}
102+
let fixed = apply_suggestions(&code, &suggestions);
157103

158104
if std::env::var("RUSTFIX_TEST_RECORD_FIXED_RUST").is_ok() {
159105
use std::io::Write;

tests/fixtures/const_static_lifetime.fixed.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/fixtures/const_static_lifetime.json

Lines changed: 0 additions & 111 deletions
This file was deleted.

tests/fixtures/const_static_lifetime.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/fixtures/explicit_iter_loop.fixed.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)