Source src/render/suggestions.rs
1//! Build one validated, workspace-relative patch for a whole suggestion.2use crate::{3 diagnostic::model::{Replacement, Suggestion},4 source::{SourceFile, span_matches},5};6use std::{7 collections::BTreeMap,8 fmt::Write,9 path::{Component, Path},10 time::{Duration, Instant},11};1213const MAX_BYTES: usize = 4 * 1024 * 1024;1415fn patch_path(root: &Path, path: &Path, prefix: &str) -> Result<String, String> {16 let relative = path17 .strip_prefix(root)18 .map_err(|_| "Source is outside the workspace")?;19 if relative.as_os_str().is_empty()20 || relative21 .components()22 .any(|c| !matches!(c, Component::Normal(_)))23 {24 return Err("Source path cannot be represented safely in a patch".into());25 }26 let mut name = prefix.to_owned();27 for component in relative.components() {28 let s = component29 .as_os_str()30 .to_str()31 .ok_or("Source path is not UTF-8")?;32 if s.chars().any(char::is_control) || s.contains('\\') {33 return Err("Source path cannot be represented safely in a patch".into());34 }35 name.push('/');36 name.push_str(s);37 }38 // Git's quoted path syntax handles spaces, quotes and UTF-8 octal bytes.39 let mut quoted = String::from("\"");40 for byte in name.bytes() {41 match byte {42 b'"' => quoted.push_str("\\\""),43 0x20..=0x7e => quoted.push(char::from(byte)),44 _ => {45 write!(&mut quoted, "\\{byte:03o}").expect("write string");46 }47 }48 }49 quoted.push('"');50 Ok(quoted)51}5253pub(super) fn build_patch(54 suggestion: &Suggestion,55 sources: &BTreeMap<std::path::PathBuf, SourceFile>,56 root: &Path,57) -> Result<String, String> {58 if suggestion.replacements.is_empty() || suggestion.replacements.len() > 1024 {59 return Err("Suggestion has no replacements or exceeds the replacement limit".into());60 }61 let deadline = Instant::now() + Duration::from_secs(2);62 let mut files: BTreeMap<&Path, Vec<&Replacement>> = BTreeMap::new();63 for replacement in &suggestion.replacements {64 files65 .entry(&replacement.span.path)66 .or_default()67 .push(replacement);68 }69 let mut patch = String::new();70 let mut total = 0usize;71 for (path, mut edits) in files {72 let old_name = patch_path(root, path, "a")?;73 let new_name = patch_path(root, path, "b")?;74 let source = sources.get(path).ok_or("Source file is unavailable")?;75 let before = source.text.as_deref().ok_or_else(|| {76 source77 .reason78 .clone()79 .unwrap_or_else(|| "Source file is unavailable".into())80 })?;81 if before.contains('\0') || edits.iter().any(|edit| edit.text.contains('\0')) {82 return Err("Source or replacement contains binary NUL data".into());83 }84 total = total.saturating_add(before.len());85 for edit in &edits {86 total = total.saturating_add(edit.text.len());87 }88 if total > MAX_BYTES || before.bytes().filter(|&b| b == b'\n').count() > 100_000 {89 return Err("Suggestion exceeds the diff size limit".into());90 }91 edits.sort_by_key(|r| (r.span.byte_start, r.span.byte_end));92 let mut previous: Option<&Replacement> = None;93 for &edit in &edits {94 if Instant::now() >= deadline {95 return Err("Suggestion exceeded the diff time limit".into());96 }97 let original_lines = edit98 .span99 .line_end100 .checked_sub(edit.span.line_start)101 .and_then(|lines| lines.checked_add(1));102 if original_lines != Some(edit.span.text.len()) {103 return Err(104 "Diagnostic does not include the complete original source snippet".into(),105 );106 }107 if !span_matches(source, &edit.span) {108 return Err(109 "Source no longer matches the diagnostic coordinates or snippet".into(),110 );111 }112 if let Some(prev) = previous113 && (edit.span.byte_start < prev.span.byte_end114 || edit.span.byte_start == prev.span.byte_start)115 {116 return Err("Suggestion contains overlapping or ambiguous replacements".into());117 }118 previous = Some(edit);119 }120 let mut after = before.to_owned();121 for edit in edits.into_iter().rev() {122 after.replace_range(edit.span.byte_start..edit.span.byte_end, &edit.text);123 }124 if before == after {125 continue;126 }127 let diff = similar::TextDiff::configure()128 .deadline(deadline)129 .diff_lines(before, &after);130 patch.push_str(131 &diff132 .unified_diff()133 .context_radius(3)134 .header(&old_name, &new_name)135 .to_string(),136 );137 if Instant::now() >= deadline {138 return Err("Suggestion exceeded the diff time limit".into());139 }140 if patch.len() > MAX_BYTES {141 return Err("Suggestion exceeds the patch size limit".into());142 }143 }144 if patch.is_empty() {145 return Err("Suggestion does not change the source".into());146 }147 Ok(patch)148}149150#[cfg(test)]151mod tests {152 use super::*;153 use crate::diagnostic::model::{Replacement, SnippetLine, Span, Suggestion};154 use crate::source::SourceFile;155 use std::{collections::BTreeMap, path::Path};156157 fn replacement(path: &Path, source: &str, start: usize, end: usize, text: &str) -> Replacement {158 let coordinate = |offset: usize| {159 let prefix = &source[..offset];160 let line = prefix.bytes().filter(|&b| b == b'\n').count() + 1;161 let column = prefix.rsplit('\n').next().unwrap().chars().count() + 1;162 (line, column)163 };164 let (line_start, column_start) = coordinate(start);165 let (line_end, column_end) = coordinate(end);166 Replacement {167 span: Span {168 path: path.into(),169 byte_start: start,170 byte_end: end,171 line_start,172 line_end,173 column_start,174 column_end,175 primary: true,176 label: None,177 expansion: None,178 text: source179 .split('\n')180 .skip(line_start - 1)181 .take(line_end - line_start + 1)182 .map(|s| SnippetLine {183 text: s.trim_end_matches('\r').into(),184 highlight_start: 0,185 highlight_end: 0,186 })187 .collect(),188 },189 text: text.into(),190 applicability: "MachineApplicable".into(),191 }192 }193 fn suggestion(replacements: Vec<Replacement>) -> Suggestion {194 Suggestion {195 message: "fix".into(),196 child_path: vec![0],197 replacements,198 }199 }200 fn source(text: &str) -> SourceFile {201 SourceFile {202 display_path: "test.rs".into(),203 text: Some(text.into()),204 reason: None,205 }206 }207 fn apply(patch: &str, files: &[(&str, &str, &str)]) { warning: clippy::similar_names208 let temp = tempfile::tempdir().unwrap();209 for (name, before, _) in files {210 let path = temp.path().join(name); warning: clippy::similar_names211 std::fs::create_dir_all(path.parent().unwrap()).unwrap();212 std::fs::write(path, before).unwrap();213 }214 let patch_path = temp.path().join("changes.patch");215 std::fs::write(&patch_path, patch).unwrap();216 let output = std::process::Command::new("git")217 .current_dir(temp.path())218 .args(["-c", "core.autocrlf=false", "apply", "--whitespace=nowarn"])219 .arg(&patch_path)220 .output()221 .unwrap();222 assert!(223 output.status.success(),224 "{}\n{patch}",225 String::from_utf8_lossy(&output.stderr)226 );227 for (name, _, after) in files {228 assert_eq!(229 std::fs::read(temp.path().join(name)).unwrap(),230 after.as_bytes()231 );232 }233 }234 #[test]235 fn multipart_and_multiple_files_apply_as_one_deterministic_patch() {236 let root = Path::new("/workspace");237 let a = root.join("src/a.rs");238 let b = root.join("src/한 글.rs");239 let before = "assert!(!items.is_empty());\n";240 let sources = BTreeMap::from([(a.clone(), source(before)), (b.clone(), source("old\n"))]);241 let mut s = suggestion(vec![242 replacement(&b, "old\n", 0, 3, "new"),243 replacement(&a, before, 6, 6, "_ne"),244 replacement(&a, before, 8, 25, "items, []"),245 ]);246 let patch = build_patch(&s, &sources, root).unwrap();247 s.replacements.reverse();248 assert_eq!(patch, build_patch(&s, &sources, root).unwrap());249 apply(250 &patch,251 &[252 ("src/a.rs", before, "assert_ne!(items, []);\n"),253 ("src/한 글.rs", "old\n", "new\n"),254 ],255 );256 }257 #[test]258 fn crlf_unicode_insertion_deletion_and_missing_final_newline_apply_exactly() {259 for (before, start, end, after) in [260 ("한글\r\nold\r\n", 8, 11, "한글\r\nnew\r\n"),261 ("old", 0, 3, "new"),262 ("", 0, 0, "new"),263 ("old\n", 0, 4, ""),264 ] {265 let root = Path::new("/workspace");266 let path = root.join("test.rs"); warning: clippy::similar_names267 let new = if after.is_empty() { "" } else { "new" };268 let s = suggestion(vec![replacement(&path, before, start, end, new)]);269 let patch = build_patch(&s, &BTreeMap::from([(path, source(before))]), root).unwrap(); warning: clippy::similar_names270 apply(&patch, &[("test.rs", before, after)]);271 }272 }273 #[test]274 fn bad_member_disables_the_entire_suggestion() {275 let root = Path::new("/workspace");276 let path = root.join("a.rs");277 let sources = BTreeMap::from([(path.clone(), source("abcdef\n"))]);278 let a = replacement(&path, "abcdef\n", 1, 4, "X");279 let b = replacement(&path, "abcdef\n", 3, 5, "Y");280 assert!(build_patch(&suggestion(vec![a.clone(), b]), &sources, root).is_err());281 let insert = replacement(&path, "abcdef\n", 2, 2, "X");282 assert!(build_patch(&suggestion(vec![insert.clone(), insert]), &sources, root).is_err());283 let mut stale = a.clone();284 stale.span.text[0].text = "stale".into();285 assert!(build_patch(&suggestion(vec![stale]), &sources, root).is_err());286 let mut missing = a.clone();287 missing.span.path = root.join("missing.rs");288 assert!(build_patch(&suggestion(vec![a, missing]), &sources, root).is_err());289 for unsafe_path in [290 "/outside.rs",291 "/workspace/../outside.rs",292 "/workspace/a\n--- injected.rs",293 ] {294 let p = Path::new(unsafe_path);295 let s = suggestion(vec![replacement(p, "old", 0, 3, "new")]);296 assert!(build_patch(&s, &BTreeMap::from([(p.into(), source("old"))]), root).is_err());297 }298 }299 #[test]300 fn missing_or_partial_original_snippets_cannot_patch_stale_source() {301 let root = Path::new("/workspace");302 let path = root.join("a.rs");303 for (original, current, keep) in [304 ("abcdef\n", "abcXYZ\n", 0),305 ("first\nmiddle\nlast\n", "first\nedited\nlast\n", 1),306 ] {307 let mut edit = replacement(&path, original, 0, original.len(), "replacement\n");308 edit.span.text.truncate(keep);309 let result = build_patch(310 &suggestion(vec![edit]),311 &BTreeMap::from([(path.clone(), source(current))]),312 root,313 );314 assert!(315 result.is_err(),316 "unverified original must not yield a patch: {result:?}"317 );318 }319 }320 #[test]321 fn distant_edits_with_multiline_replacement_keep_hunk_offsets() {322 let root = Path::new("/workspace");323 let path = root.join("a.rs"); warning: clippy::similar_names324 let before = (0..30).map(|i| format!("line{i}\n")).collect::<String>(); warning: clippy::format_collect325 let first = before.find("line2\n").unwrap();326 let second = before.find("line25\n").unwrap();327 let s = suggestion(vec![328 replacement(&path, &before, first, first + 5, "new2\nextra"),329 replacement(&path, &before, second, second + 6, "new25"),330 ]);331 let after = before332 .replace("line2\n", "new2\nextra\n")333 .replace("line25\n", "new25\n");334 let patch = build_patch(&s, &BTreeMap::from([(path, source(&before))]), root).unwrap(); warning: clippy::similar_names335 assert!(patch.matches("@@").count() >= 4);336 apply(&patch, &[("a.rs", &before, &after)]);337 }338 #[test]339 fn no_op_binary_and_oversized_suggestions_have_no_patch() {340 let root = Path::new("/workspace");341 let path = root.join("a.rs");342 for text in ["old", "old\0data", &"a".repeat(MAX_BYTES + 1)] {343 let unchanged = suggestion(vec![replacement(&path, text, 0, 3, &text[..3])]);344 assert!(345 build_patch(346 &unchanged,347 &BTreeMap::from([(path.clone(), source(text))]),348 root349 )350 .is_err()351 );352 }353 let binary = "old\0data";354 let s = suggestion(vec![replacement(&path, binary, 0, 3, "new")]);355 assert!(build_patch(&s, &BTreeMap::from([(path.clone(), source(binary))]), root).is_err());356 }357}358Findings in this file§
warning clippy::similar_names §
warning clippy::similar_names §
Targets (1)
cargo-clippy-html (bin)
Primary: src/render/suggestions.rs:210:17
Primary: src/render/suggestions.rs:207:14
note: existing binding defined here
help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#similar_names
note: `-W clippy::similar-names` implied by `-W clippy::pedantic`
help: to override `-W clippy::pedantic` add `#[allow(clippy::similar_names)]`
Rendered diagnostic (text)
warning: binding's name is too similar to existing binding
--> src/render/suggestions.rs:210:17
|
210 | let path = temp.path().join(name);
| ^^^^
|
note: existing binding defined here
--> src/render/suggestions.rs:207:14
|
207 | fn apply(patch: &str, files: &[(&str, &str, &str)]) {
| ^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#similar_names
= note: `-W clippy::similar-names` implied by `-W clippy::pedantic`
= help: to override `-W clippy::pedantic` add `#[allow(clippy::similar_names)]`
warning clippy::similar_names §
warning clippy::similar_names §
Targets (1)
cargo-clippy-html (bin)
Primary: src/render/suggestions.rs:269:17
Primary: src/render/suggestions.rs:266:17
note: existing binding defined here
help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#similar_names
Rendered diagnostic (text)
warning: binding's name is too similar to existing binding
--> src/render/suggestions.rs:269:17
|
269 | let patch = build_patch(&s, &BTreeMap::from([(path, source(before))]), root).unwrap();
| ^^^^^
|
note: existing binding defined here
--> src/render/suggestions.rs:266:17
|
266 | let path = root.join("test.rs");
| ^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#similar_names
warning clippy::format_collect §
warning clippy::format_collect §
Targets (1)
cargo-clippy-html (bin)
Primary: src/render/suggestions.rs:324:22
Primary: src/render/suggestions.rs:324:30
Primary: src/render/suggestions.rs:324:38
help: call `fold` instead
help: ... and use the `write!` macro here
note: this can be written more efficiently by appending to a `String` directly
help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#format_collect
note: `-W clippy::format-collect` implied by `-W clippy::pedantic`
help: to override `-W clippy::pedantic` add `#[allow(clippy::format_collect)]`
Rendered diagnostic (text)
warning: use of `format!` to build up a string from an iterator
--> src/render/suggestions.rs:324:22
|
324 | let before = (0..30).map(|i| format!("line{i}\n")).collect::<String>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: call `fold` instead
--> src/render/suggestions.rs:324:30
|
324 | let before = (0..30).map(|i| format!("line{i}\n")).collect::<String>();
| ^^^
help: ... and use the `write!` macro here
--> src/render/suggestions.rs:324:38
|
324 | let before = (0..30).map(|i| format!("line{i}\n")).collect::<String>();
| ^^^^^^^^^^^^^^^^^^^^
= note: this can be written more efficiently by appending to a `String` directly
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#format_collect
= note: `-W clippy::format-collect` implied by `-W clippy::pedantic`
= help: to override `-W clippy::pedantic` add `#[allow(clippy::format_collect)]`
warning clippy::similar_names §
warning clippy::similar_names §
Targets (1)
cargo-clippy-html (bin)
Primary: src/render/suggestions.rs:334:13
Primary: src/render/suggestions.rs:323:13
note: existing binding defined here
help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#similar_names
Rendered diagnostic (text)
warning: binding's name is too similar to existing binding
--> src/render/suggestions.rs:334:13
|
334 | let patch = build_patch(&s, &BTreeMap::from([(path, source(&before))]), root).unwrap();
| ^^^^^
|
note: existing binding defined here
--> src/render/suggestions.rs:323:13
|
323 | let path = root.join("a.rs");
| ^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#similar_names