|
| 1 | +#![deny(clippy::unwrap_used)] |
1 | 2 | //! This module holds the functionality related to running clang-format and/or |
2 | 3 | //! clang-tidy. |
3 | 4 |
|
@@ -60,10 +61,12 @@ impl ClangTool { |
60 | 61 | pub fn get_exe_path(&self, version: &RequestedVersion) -> Result<PathBuf> { |
61 | 62 | let name = self.as_str(); |
62 | 63 | match version { |
63 | | - RequestedVersion::Path(path_buf) => { |
64 | | - which_in(name, Some(path_buf), current_dir().unwrap()) |
65 | | - .map_err(|_| anyhow!("Could not find {self} by path")) |
66 | | - } |
| 64 | + RequestedVersion::Path(path_buf) => which_in( |
| 65 | + name, |
| 66 | + Some(path_buf), |
| 67 | + current_dir().with_context(|| "Failed to access current working directory.")?, |
| 68 | + ) |
| 69 | + .map_err(|_| anyhow!("Could not find {self} by path")), |
67 | 70 | // Thus, we should use whatever is installed and added to $PATH. |
68 | 71 | RequestedVersion::SystemDefault | RequestedVersion::NoValue => { |
69 | 72 | which(name).map_err(|_| anyhow!("Could not find clang tool by name")) |
@@ -118,12 +121,17 @@ impl ClangTool { |
118 | 121 | fn capture_version(clang_tool: &PathBuf) -> Result<String> { |
119 | 122 | let output = Command::new(clang_tool).arg("--version").output()?; |
120 | 123 | let stdout = String::from_utf8_lossy(&output.stdout); |
121 | | - let version_pattern = Regex::new(r"(?i)version[^\d]*([\d.]+)").unwrap(); |
| 124 | + let version_pattern = Regex::new(r"(?i)version[^\d]*([\d.]+)") |
| 125 | + .with_context(|| "Failed to allocate RegExp pattern (for clang version parsing) ")?; |
122 | 126 | let captures = version_pattern.captures(&stdout).ok_or(anyhow!( |
123 | 127 | "Failed to find version number in `{} --version` output", |
124 | 128 | clang_tool.to_string_lossy() |
125 | 129 | ))?; |
126 | | - Ok(captures.get(1).unwrap().as_str().to_string()) |
| 130 | + Ok(captures |
| 131 | + .get(1) |
| 132 | + .ok_or(anyhow!("Failed to get version capture group"))? |
| 133 | + .as_str() |
| 134 | + .to_string()) |
127 | 135 | } |
128 | 136 | } |
129 | 137 |
|
@@ -434,53 +442,54 @@ pub trait MakeSuggestions { |
434 | 442 | })?; |
435 | 443 | hunks_in_patch += 1; |
436 | 444 | let hunk_range = file_obj.is_hunk_in_diff(&hunk); |
437 | | - if hunk_range.is_none() { |
438 | | - continue; |
439 | | - } |
440 | | - let (start_line, end_line) = hunk_range.unwrap(); |
441 | | - let mut suggestion = String::new(); |
442 | | - let suggestion_help = self.get_suggestion_help(start_line, end_line); |
443 | | - let mut removed = vec![]; |
444 | | - for line_index in 0..line_count { |
445 | | - let diff_line = patch |
446 | | - .line_in_hunk(hunk_id, line_index) |
447 | | - .with_context(|| format!("Failed to get line {line_index} in a hunk {hunk_id} of patch for {file_name}"))?; |
448 | | - let line = String::from_utf8(diff_line.content().to_owned()) |
449 | | - .with_context(|| format!("Failed to convert line {line_index} buffer to string in hunk {hunk_id} of patch for {file_name}"))?; |
450 | | - if ['+', ' '].contains(&diff_line.origin()) { |
451 | | - suggestion.push_str(line.as_str()); |
452 | | - } else { |
453 | | - removed.push( |
454 | | - diff_line |
455 | | - .old_lineno() |
456 | | - .expect("Removed line should have a line number"), |
457 | | - ); |
| 445 | + match hunk_range { |
| 446 | + None => continue, |
| 447 | + Some((start_line, end_line)) => { |
| 448 | + let mut suggestion = String::new(); |
| 449 | + let suggestion_help = self.get_suggestion_help(start_line, end_line); |
| 450 | + let mut removed = vec![]; |
| 451 | + for line_index in 0..line_count { |
| 452 | + let diff_line = patch |
| 453 | + .line_in_hunk(hunk_id, line_index) |
| 454 | + .with_context(|| format!("Failed to get line {line_index} in a hunk {hunk_id} of patch for {file_name}"))?; |
| 455 | + let line = String::from_utf8(diff_line.content().to_owned()) |
| 456 | + .with_context(|| format!("Failed to convert line {line_index} buffer to string in hunk {hunk_id} of patch for {file_name}"))?; |
| 457 | + if ['+', ' '].contains(&diff_line.origin()) { |
| 458 | + suggestion.push_str(line.as_str()); |
| 459 | + } else { |
| 460 | + removed.push( |
| 461 | + diff_line |
| 462 | + .old_lineno() |
| 463 | + .expect("Removed line should have a line number"), |
| 464 | + ); |
| 465 | + } |
| 466 | + } |
| 467 | + if suggestion.is_empty() && !removed.is_empty() { |
| 468 | + suggestion.push_str( |
| 469 | + format!( |
| 470 | + "Please remove the line(s)\n- {}", |
| 471 | + removed |
| 472 | + .iter() |
| 473 | + .map(|l| l.to_string()) |
| 474 | + .collect::<Vec<String>>() |
| 475 | + .join("\n- ") |
| 476 | + ) |
| 477 | + .as_str(), |
| 478 | + ) |
| 479 | + } else { |
| 480 | + suggestion = format!("```suggestion\n{suggestion}```"); |
| 481 | + } |
| 482 | + let comment = Suggestion { |
| 483 | + line_start: start_line, |
| 484 | + line_end: end_line, |
| 485 | + suggestion: format!("{suggestion_help}\n{suggestion}"), |
| 486 | + path: file_name.clone(), |
| 487 | + }; |
| 488 | + if !review_comments.is_comment_in_suggestions(&comment) { |
| 489 | + review_comments.comments.push(comment); |
| 490 | + } |
458 | 491 | } |
459 | 492 | } |
460 | | - if suggestion.is_empty() && !removed.is_empty() { |
461 | | - suggestion.push_str( |
462 | | - format!( |
463 | | - "Please remove the line(s)\n- {}", |
464 | | - removed |
465 | | - .iter() |
466 | | - .map(|l| l.to_string()) |
467 | | - .collect::<Vec<String>>() |
468 | | - .join("\n- ") |
469 | | - ) |
470 | | - .as_str(), |
471 | | - ) |
472 | | - } else { |
473 | | - suggestion = format!("```suggestion\n{suggestion}```"); |
474 | | - } |
475 | | - let comment = Suggestion { |
476 | | - line_start: start_line, |
477 | | - line_end: end_line, |
478 | | - suggestion: format!("{suggestion_help}\n{suggestion}"), |
479 | | - path: file_name.clone(), |
480 | | - }; |
481 | | - if !review_comments.is_comment_in_suggestions(&comment) { |
482 | | - review_comments.comments.push(comment); |
483 | | - } |
484 | 493 | } |
485 | 494 | review_comments.tool_total[is_tidy_tool] = |
486 | 495 | Some(review_comments.tool_total[is_tidy_tool].unwrap_or_default() + hunks_in_patch); |
|
0 commit comments