fix remaining clippy errors after main.rs refactor

This commit is contained in:
Josh Junon 2024-03-01 15:49:33 +01:00
parent d9187c8891
commit 2b53662775
9 changed files with 45 additions and 82 deletions

View File

@ -67,8 +67,8 @@ pub mod gb {
}
#[inline]
pub fn context(&self) -> Option<&Context> {
Some(&self.context)
pub fn context(&self) -> &Context {
&self.context
}
pub(crate) fn into_owned(self) -> (ErrorKind, Context) {
@ -175,10 +175,7 @@ pub mod gb {
let r = app_level_io();
assert!(r.is_err());
let e = r.unwrap_err();
assert_eq!(
e.context().unwrap().vars.get("foo"),
Some(&"bar".to_string())
);
assert_eq!(e.context().vars.get("foo"), Some(&"bar".to_string()));
assert!(e.source().is_none());
assert!(e.to_string().starts_with("io.other-error:"));
}

View File

@ -25,10 +25,6 @@ impl<'repo> Remote<'repo> {
.map_err(Into::into)
}
pub fn url_as_str(&self) -> Result<Option<&str>> {
Ok(self.inner.url())
}
pub fn push(
&mut self,
refspec: &[&str],

View File

@ -555,7 +555,7 @@ pub fn target_to_base_branch(
.context("failed to get upstream commits")?
.iter()
.map(super::commit_to_remote_commit)
.collect::<Result<Vec<_>>>()?;
.collect::<Vec<_>>();
// get some recent commits
let recent_commits = project_repository
@ -563,7 +563,7 @@ pub fn target_to_base_branch(
.context("failed to get recent commits")?
.iter()
.map(super::commit_to_remote_commit)
.collect::<Result<Vec<_>>>()?;
.collect::<Vec<_>>();
let base = super::BaseBranch {
branch_name: format!("{}/{}", target.branch.remote(), target.branch.branch()),

View File

@ -142,15 +142,15 @@ impl Hunk {
self.timestamp_ms
}
pub fn contains(&self, line: &u32) -> bool {
self.start <= *line && self.end >= *line
pub fn contains(&self, line: u32) -> bool {
self.start <= line && self.end >= line
}
pub fn intersects(&self, another: &Hunk) -> bool {
self.contains(&another.start)
|| self.contains(&another.end)
|| another.contains(&self.start)
|| another.contains(&self.end)
self.contains(another.start)
|| self.contains(another.end)
|| another.contains(self.start)
|| another.contains(self.end)
}
}

View File

@ -9,14 +9,14 @@ pub fn hunk_with_context(
context_lines: usize,
file_lines_before: &[&str],
change_type: diff::ChangeType,
) -> Result<diff::Hunk> {
) -> diff::Hunk {
let diff_lines = hunk_diff
.lines()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>();
if diff_lines.is_empty() {
#[allow(clippy::cast_possible_truncation)]
return Ok(diff::Hunk {
return diff::Hunk {
diff: hunk_diff.to_owned(),
old_start: hunk_old_start_line as u32,
old_lines: 0,
@ -24,7 +24,7 @@ pub fn hunk_with_context(
new_lines: 0,
binary: is_binary,
change_type,
});
};
}
let new_file = hunk_old_start_line == 0;
@ -120,7 +120,8 @@ pub fn hunk_with_context(
binary: is_binary,
change_type,
};
Ok(hunk)
hunk
}
#[cfg(test)]
@ -140,8 +141,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
let expected = r#"@@ -5,7 +5,7 @@
[features]
@ -173,8 +173,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(
with_ctx.diff.replace("\n \n", "\n\n"),
r#"@@ -1,5 +1,5 @@
@ -206,8 +205,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(
with_ctx.diff.replace("\n \n", "\n\n"),
r#"@@ -1,4 +1,4 @@
@ -238,8 +236,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(
with_ctx.diff.replace("\n \n", "\n\n"),
r#"@@ -10,5 +10,5 @@
@ -274,8 +271,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(
with_ctx.diff.replace("\n \n", "\n\n"),
r#"@@ -5,7 +5,10 @@
@ -314,8 +310,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(
with_ctx.diff.replace("\n \n", "\n\n"),
r#"@@ -4,9 +4,7 @@
@ -348,8 +343,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(with_ctx.diff, "");
}
@ -379,8 +373,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(with_ctx.diff.replace("\n \n", "\n\n"), hunk_diff);
assert_eq!(with_ctx.old_start, 1);
assert_eq!(with_ctx.old_lines, 14);
@ -404,8 +397,7 @@ mod tests {
3,
&Vec::new(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(with_ctx.diff.replace("\n \n", "\n\n"), hunk_diff);
assert_eq!(with_ctx.old_start, 0);
assert_eq!(with_ctx.old_lines, 0);
@ -428,8 +420,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
let expected = r#"@@ -6,6 +6,9 @@
[features]
default = ["serde", "rusqlite"]
@ -463,8 +454,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
let expected = r#"@@ -6,6 +10,9 @@
[features]
default = ["serde", "rusqlite"]
@ -509,8 +499,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(with_ctx.diff.replace("\n \n", "\n\n"), expected);
assert_eq!(with_ctx.old_start, 4);
assert_eq!(with_ctx.old_lines, 9);
@ -544,8 +533,7 @@ mod tests {
3,
&file_lines(),
diff::ChangeType::Added,
)
.unwrap();
);
assert_eq!(with_ctx.diff.replace("\n \n", "\n\n"), expected);
assert_eq!(with_ctx.old_start, 4);
assert_eq!(with_ctx.old_lines, 9);
@ -567,8 +555,7 @@ mod tests {
3,
&file_lines_2(),
diff::ChangeType::Added,
)
.unwrap();
);
let expected = "@@ -8,8 +8,6 @@
.order(:created_at)
.page params[:page]
@ -599,8 +586,7 @@ mod tests {
3,
&file_lines_3(),
diff::ChangeType::Added,
)
.unwrap();
);
let expected = r#"@@ -1,4 +1,5 @@
alias(
name = "rdeps",

View File

@ -74,7 +74,7 @@ fn files_with_hunk_context(
.map(|hunk| {
if hunk.diff.is_empty() {
// noop on empty diff
Ok(hunk.clone())
hunk.clone()
} else {
context::hunk_with_context(
&hunk.diff,
@ -87,8 +87,7 @@ fn files_with_hunk_context(
)
}
})
.collect::<Result<Vec<diff::Hunk>>>()
.context("failed to add context to hunk")?;
.collect::<Vec<diff::Hunk>>();
}
Ok(files)
}

View File

@ -159,17 +159,17 @@ pub fn branch_to_remote_branch_data(
commits: ahead
.into_iter()
.map(|commit| commit_to_remote_commit(&commit))
.collect::<Result<Vec<_>>>()?,
.collect::<Vec<_>>(),
})
})
.transpose()
}
pub fn commit_to_remote_commit(commit: &git::Commit) -> Result<RemoteCommit> {
Ok(RemoteCommit {
pub fn commit_to_remote_commit(commit: &git::Commit) -> RemoteCommit {
RemoteCommit {
id: commit.id().to_string(),
description: commit.message().unwrap_or_default().to_string(),
created_at: commit.time().seconds().try_into().unwrap(),
author: commit.author().into(),
})
}
}

View File

@ -1007,7 +1007,7 @@ fn files_with_hunk_context(
.map(|hunk| {
if hunk.diff.is_empty() {
// noop on empty diff
Ok(hunk.clone())
hunk.clone()
} else {
let hunk_with_ctx = context::hunk_with_context(
&hunk.diff,
@ -1021,22 +1021,19 @@ fn files_with_hunk_context(
to_virtual_branch_hunk(hunk.clone(), hunk_with_ctx)
}
})
.collect::<Result<Vec<VirtualBranchHunk>>>()
.context("failed to add context to hunk")?;
.collect::<Vec<VirtualBranchHunk>>();
}
Ok(files)
}
fn to_virtual_branch_hunk(
mut hunk: VirtualBranchHunk,
diff_with_context: Result<diff::Hunk>,
) -> Result<VirtualBranchHunk> {
diff_with_context.map(|diff| {
hunk.diff = diff.diff;
hunk.start = diff.new_start;
hunk.end = diff.new_start + diff.new_lines;
hunk
})
diff_with_context: diff::Hunk,
) -> VirtualBranchHunk {
hunk.diff = diff_with_context.diff;
hunk.start = diff_with_context.new_start;
hunk.end = diff_with_context.new_start + diff_with_context.new_lines;
hunk
}
fn is_requires_force(

View File

@ -67,18 +67,6 @@ impl DirWriter {
})?
}
pub fn write_usize(&self, path: &str, contents: &usize) -> Result<(), std::io::Error> {
self.write_string(path, &contents.to_string())
}
pub fn write_u128(&self, path: &str, contents: &u128) -> Result<(), std::io::Error> {
self.write_string(path, &contents.to_string())
}
pub fn write_bool(&self, path: &str, contents: &bool) -> Result<(), std::io::Error> {
self.write_string(path, &contents.to_string())
}
pub fn write_string(&self, path: &str, contents: &str) -> Result<(), std::io::Error> {
self.write(path, contents)
}