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

Commit df52ebe

Browse files
committed
Switch to replace-rs
1 parent 07cbb8e commit df52ebe

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ quick-error = "1.2.1"
2121
serde = "1.0"
2222
serde_json = "1.0"
2323
serde_derive = "1.0"
24+
replace-rs = { git = "https://github.com/killercup/replace-rs" }
25+
failure = "0.1.1"
2426

2527
[dev-dependencies]
2628
duct = "0.8.2"

src/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Rustc Diagnostic JSON Output
2-
//!
2+
//!
33
//! The following data types are copied from [rust-lang/rust](https://github.com/rust-lang/rust/blob/de78655bca47cac8e783dbb563e7e5c25c1fae40/src/libsyntax/json.rs)
44
55
#[derive(Deserialize, Debug)]
@@ -21,8 +21,8 @@ pub struct Diagnostic {
2121
#[derive(Deserialize, Debug)]
2222
pub struct DiagnosticSpan {
2323
pub file_name: String,
24-
byte_start: u32,
25-
byte_end: u32,
24+
pub byte_start: u32,
25+
pub byte_end: u32,
2626
/// 1-based.
2727
pub line_start: usize,
2828
pub line_end: usize,

src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#[macro_use]
22
extern crate serde_derive;
33
extern crate serde_json;
4+
extern crate failure;
5+
extern crate replace_rs as replace;
46

57
use std::collections::HashSet;
8+
use std::ops::Range;
9+
10+
use failure::Error;
611

712
pub mod diagnostics;
813
use diagnostics::{Diagnostic, DiagnosticSpan};
@@ -61,6 +66,7 @@ pub struct Solution {
6166
pub struct Snippet {
6267
pub file_name: String,
6368
pub line_range: LineRange,
69+
pub range: Range<usize>,
6470
/// leading surrounding text, text to replace, trailing surrounding text
6571
///
6672
/// This split is useful for higlighting the part that gets replaced
@@ -109,6 +115,7 @@ fn parse_snippet(span: &DiagnosticSpan) -> Snippet {
109115
column: span.column_end,
110116
},
111117
},
118+
range: (span.byte_start as usize)..(span.byte_end as usize),
112119
text: (lead, body, tail),
113120
}
114121
}
@@ -208,16 +215,22 @@ pub fn apply_suggestion(file_content: &mut String, suggestion: &Replacement) ->
208215
new_content
209216
}
210217

211-
pub fn apply_suggestions(code: &str, suggestions: &[Suggestion]) -> String {
212-
let mut fixed = code.to_string();
218+
pub fn apply_suggestions(code: &str, suggestions: &[Suggestion]) -> Result<String, Error> {
219+
use replace::Data;
220+
221+
let mut fixed = Data::new(code.as_bytes());
213222

214223
for sug in suggestions.iter().rev() {
215224
for sol in &sug.solutions {
216225
for r in &sol.replacements {
217-
fixed = apply_suggestion(&mut fixed, r);
226+
fixed.replace_range(
227+
r.snippet.range.start,
228+
r.snippet.range.end - 1,
229+
r.replacement.as_bytes(),
230+
)?;
218231
}
219232
}
220233
}
221234

222-
fixed
235+
Ok(String::from_utf8(fixed.to_vec())?)
223236
}

tests/everything.rs

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

16-
use rustfix::{Replacement, apply_suggestions};
16+
use rustfix::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!(
2121
"rustc", file,
2222
"--error-format=pretty-json", "-Zunstable-options", "--emit=metadata",
2323
"--crate-name=rustfix_test",
24+
"-Zsuggestion-applicability",
2425
"--out-dir", tmp.path()
2526
);
2627
let res = better_call_clippy
@@ -99,7 +100,7 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P) -> Result<(), Box<Error>> {
99100
"got unexpected suggestions from clippy",
100101
);
101102

102-
let fixed = apply_suggestions(&code, &suggestions);
103+
let fixed = apply_suggestions(&code, &suggestions)?;
103104

104105
if std::env::var("RUSTFIX_TEST_RECORD_FIXED_RUST").is_ok() {
105106
use std::io::Write;
@@ -108,7 +109,7 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P) -> Result<(), Box<Error>> {
108109
}
109110

110111
let expected_fixed = read_file(&fixed_file)?;
111-
assert_eq!(fixed.trim(), expected_fixed.trim(), "file doesn't look fixed");
112+
assert_eq!(fixed.trim(), expected_fixed.trim(), "file {} doesn't look fixed", file.display());
112113

113114
compiles_without_errors(&fixed_file)?;
114115

0 commit comments

Comments
 (0)