Skip to content

Commit

Permalink
Warn rather than panic when hunk is malformed
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Feb 25, 2025
1 parent a86a296 commit f9fc948
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dylint/src/package_options/auto_correct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn auto_correct_revertible(

let repository = clippy_repository(opts.quiet)?;

let rewrites = collect_rewrites(old_channel, new_oid, &repository)?;
let rewrites = collect_rewrites(opts, old_channel, new_oid, &repository)?;

loop {
let mut rewriters = BTreeMap::new();
Expand Down
18 changes: 16 additions & 2 deletions dylint/src/package_options/auto_correct/rewrite/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{common, highlight::Highlight, short_id::ShortId, tokenization::tokenize_lines};
use crate::{error::warn, opts};
use anyhow::Result;
use dylint_internal::{
env,
Expand Down Expand Up @@ -142,6 +143,7 @@ fn subslice_position<T: PartialEq>(xs: &[T], ys: &[T]) -> Option<usize> {
}

pub fn collect_rewrites(
opts: &opts::Dylint,
old_channel: &str,
new_oid: Oid,
repository: &Repository,
Expand Down Expand Up @@ -182,7 +184,7 @@ pub fn collect_rewrites(
let mut rewrites = HashMap::new();
for (patch, oid) in patches_with_oids {
let rewrites_unflattened =
rewrites_from_patch(&patch, &mut n_insertions, &mut n_refactors)?;
rewrites_from_patch(opts, &patch, &mut n_insertions, &mut n_refactors)?;
for rewrite in rewrites_unflattened {
rewrites.entry(rewrite).or_insert(oid);
}
Expand All @@ -202,6 +204,7 @@ pub fn collect_rewrites(
// smoelius: You need a `Patch` to get a `DiffHunk`'s lines. So there would be no easy way to write
// a `hunks_from_patch` function. See, for example, `hunk_lines` below.
fn rewrites_from_patch(
opts: &opts::Dylint,
patch: &Patch<'_>,
n_insertions: &mut usize,
n_refactors: &mut usize,
Expand All @@ -210,7 +213,18 @@ fn rewrites_from_patch(
let n_hunks = patch.num_hunks();
for hunk_idx in 0..n_hunks {
let (hunk, line_count) = patch.hunk(hunk_idx)?;
debug_assert_eq!((hunk.old_lines() + hunk.new_lines()) as usize, line_count);
if (hunk.old_lines() + hunk.new_lines()) as usize != line_count {
warn(
opts,
&format!(
"Malformed hunk: old lines ({}) + new lines ({}) != line count ({})",
hunk.old_lines(),
hunk.new_lines(),
line_count
),
);
continue;
}
// smoelius: `hunk.old_lines()` must be non-zero for there to be something to rewrite.
if hunk.old_lines() == 0 {
*n_insertions += 1;
Expand Down

0 comments on commit f9fc948

Please sign in to comment.